首页 > 解决方案 > 如何打开 Openshift - 端口 9111(grpc 应用程序)

问题描述

我有 grpc 应用程序(客户端和服务器),它在 localhost 上运行良好,但我在 MiniShift 中有问题。每个应用程序在不同的 pod 中运行。问题是客户端无法连接到服务器:

java.net.ConnectException: finishConnect(..) failed: Connection refused
at io.grpc.netty.shaded.io.netty.channel.unix.Errors.throwConnectException(Errors.java:124) ~[grpc-netty-shaded-1.28.0.jar!/:1.28.0]
at io.grpc.netty.shaded.io.netty.channel.unix.Socket.finishConnect(Socket.java:243) ~[grpc-netty-shaded-1.28.0.jar!/:1.28.0]
at io.grpc.netty.shaded.io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.doFinishConnect(AbstractEpollChannel.java:672) ~[grpc-netty-shaded-1.28.0.jar!/:1.28.0]
at io.grpc.netty.shaded.io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.finishConnect(AbstractEpollChannel.java:649) ~[grpc-netty-shaded-1.28.0.jar!/:1.28.0]
at io.grpc.netty.shaded.io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.epollOutReady(AbstractEpollChannel.java:529) ~[grpc-netty-shaded-1.28.0.jar!/:1.28.0]
at io.grpc.netty.shaded.io.netty.channel.epoll.EpollEventLoop.processReady(EpollEventLoop.java:465) ~[grpc-netty-shaded-1.28.0.jar!/:1.28.0]
at io.grpc.netty.shaded.io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:378) ~[grpc-netty-shaded-1.28.0.jar!/:1.28.0]
at io.grpc.netty.shaded.io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[grpc-netty-shaded-1.28.0.jar!/:1.28.0]
at io.grpc.netty.shaded.io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[grpc-netty-shaded-1.28.0.jar!/:1.28.0]
at io.grpc.netty.shaded.io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[grpc-netty-shaded-1.28.0.jar!/:1.28.0]

在 java.lang.Thread.run(Thread.java:748) [na:1.8.0_242]

我尝试了 pod 之间的连接,但只打开了 80 和 443 端口。其余端口返回拒绝连接。

如何打开端口 9111(运行我的 grpc java 应用程序)?谢谢

标签: javaopenshiftgrpcminishift

解决方案


我需要创建一个NodePort类型的服务。

NodePorts 是一种服务类型,它在每台集群机器上打开一个端口,并且可以将流量(TCP/UDP)重定向到您的应用程序。为此,您需要使用system:admin的管理权限或授予您选择的用户...例如:

oc adm policy add-cluster-role-to-user cluster-admin matko

不幸的是,Openshift 基于 Kubernetes,Kubernetes 默认只允许节点端口在30000-32767范围内。

您的应用程序仍会在 9111 中听到,但要从集群外部访问,我们只有我上面写的端口。

找到应用服务并编辑:

oc edit svc mysql

然后,将Type更改为NodePort并选择一个端口 - 或获取一个随机端口:

apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    name: mysql
spec:
  type: NodePort
  ports:
    - port: 3036
      nodePort: 30036
      name: http
  selector:
    name: mysql

这是为了解释一次,永远不要在互联网上公开数据库。


推荐阅读