[英]Connect to gRPC server running on different pod
I have a gRPC client and server running on my local and I'm able to communicate over them via HTTP2 through localhost.我有一个 gRPC 客户端和服务器在我的本地运行,我可以通过 localhost 通过 HTTP2 通过它们进行通信。 But when the apps are deployed in dev environment using Kubernetes and Docker, I'm not able to call the gRPC server from client app.
但是,当使用 Kubernetes 和 Docker 在开发环境中部署应用程序时,我无法从客户端应用程序调用 gRPC 服务器。
I connect to the client using this code:我使用以下代码连接到客户端:
ManagedChannel managedChannel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();
In DEV: Here is my application.yml from server app.在 DEV 中:这是我来自服务器应用程序的 application.yml。
grpc:
port: 6565
enabled: true
I have the gRPC server running on port 6565, so I have changed the following on client application.yml as well我的 gRPC 服务器在端口 6565 上运行,所以我也在客户端 application.yml 上更改了以下内容
taxgRPC:
host: bpstax-ws.g-dev4.svc.rnq.k8s.copart.com
port: 6565
When my client calls the server, the server doesn't get any incoming requests and client fails giving the following exception:当我的客户端调用服务器时,服务器没有收到任何传入请求,客户端失败并给出以下异常:
Caused by: io.grpc.netty.shaded.io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: bpstax-ws.g-dev4.svc.rnq.k8s.copart.com/10.148.2.194:6565
When I log into the particular dev pod, I can see the port listening to the gRPC server.当我登录到特定的开发 pod 时,我可以看到正在侦听 gRPC 服务器的端口。
[dedas@rndcks401 ~]$ kubectl exec -ti bpstax-g-dev4-v029-74mj2 -n g-dev4 -- bash
bash-4.1$ netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:6565 0.0.0.0:* LISTEN 1/java
tcp 0 0 0.0.0.0:38957 0.0.0.0:* LISTEN 1/java
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1/java
tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 1/java
I need some help on how to connect to this gRPC server.我需要一些关于如何连接到这个 gRPC 服务器的帮助。 Please let me know if any clarifications/code is required.
如果需要任何说明/代码,请告诉我。
I guess you have to provide a correct host to your gRPC client.我想您必须为您的 gRPC 客户端提供正确的主机。 Within your k8s env the best way to discover you server pod's ip address is though headless service .
在您的 k8s 环境中,发现服务器 pod 的 ip 地址的最佳方法是headless service 。
The example definition:示例定义:
apiVersion: v1
kind: Service
metadata:
labels:
app: server
name: server-headless
namespace: <YOUR_NAMESPACE>
spec:
clusterIP: None
ports:
- name: tcp
port: 6565
protocol: TCP
targetPort: 6565
selector:
app: <SERVER_POD_LABEL>
type: ClusterIP
Having the headless service for your server app you can use service's FQDN as a host.为您的服务器应用程序提供无头服务,您可以使用服务的 FQDN 作为主机。 This way you can be sure the service's FQDN is always resolved to the correct pod's ip/s.
这样您就可以确保服务的 FQDN 始终解析为正确的 pod 的 ip/s。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.