Grpc connection in kubernetes is not working =>UNAVAILABLE: Network closed for unknown reason
- Your deployment yamls are lacking the service part to route the traffic to the pods appropriately.
- Also, the deployments are not passing the Environment Variables as specified in the
docker-compose.yml
. Here are the fixed yamls. deployment_server.yml
:
apiVersion: apps/v1kind: Deploymentmetadata: labels: app.kubernetes.io/name: grpc-server name: grpc-serverspec: replicas: 1 selector: matchLabels: app.kubernetes.io/name: grpc-server template: metadata: labels: app.kubernetes.io/name: grpc-server spec: containers: - image: poc_grpc-server:latest imagePullPolicy: Never name: grpc-server ports: - containerPort: 8081 env: - name: GRPC_SERVER_PORT value: "8081"---apiVersion: v1kind: Servicemetadata: name: grpc-server-svcspec: selector: app.kubernetes.io/name: grpc-server ports: - protocol: TCP port: 8081
deployment_client.yml
:
apiVersion: apps/v1kind: Deploymentmetadata: labels: app.kubernetes.io/name: grpc-client name: grpc-clientspec: replicas: 1 selector: matchLabels: app.kubernetes.io/name: grpc-client template: metadata: labels: app.kubernetes.io/name: grpc-client spec: containers: - image: poc_grpc-client imagePullPolicy: Never name: grpc-client ports: - containerPort: 8080 env: - name: GRPC_SERVER_ADDRESS value: "grpc-server-svc" - name: GRPC_SERVER_PORT value: "8081"---apiVersion: v1kind: Servicemetadata: name: grpc-client-svcspec: selector: app.kubernetes.io/name: grpc-client ports: - protocol: TCP port: 8080 type: NodePort
Highlights:
- created
grpc-server-svc
service asClusterIP
to be available only inside the cluster serving the GRPC Server. - created
grpc-client-svc
service toNodePort
to demonstrate receiving curl requests from outside the cluster. - Added the
env
section: Note that I setGRPC_SERVER_ADDRESS
togrpc-server-svc
since we are not in docker environment anymorehost.docker.internal
is no longer an option we are now targetting the service mentioned above. - I'm using
imagePullPolicy
set toNever
only for this example, since I'm using my local docker registry.
Reproduction:
- After building the images:
$ docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEpoc_grpc-client latest 7f6d886a1612 24 minutes ago 660MBpoc_grpc-server latest d46bf9481d1c 24 minutes ago 658MB
- Deployed the fixed yamls as above:
$ kubectl apply -f deployment_server.yml deployment.apps/grpc-server createdservice/grpc-server-svc created$ kubectl apply -f deployment_client.yml deployment.apps/grpc-client createdservice/grpc-client-svc created$ kubectl get allNAME READY STATUS RESTARTS AGEpod/grpc-client-6ffcf6b6c8-846s5 1/1 Running 0 3spod/grpc-server-5d7fd9cb89-dkqlb 1/1 Running 0 7sNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEservice/grpc-client-svc NodePort 10.99.58.76 <none> 8080:32224/TCP 3sservice/grpc-server-svc ClusterIP 10.96.67.139 <none> 8081/TCP 7sservice/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3h36mNAME READY UP-TO-DATE AVAILABLE AGEdeployment.apps/grpc-client 1/1 1 1 3sdeployment.apps/grpc-server 1/1 1 1 7sNAME DESIRED CURRENT READY AGEreplicaset.apps/grpc-client-6ffcf6b6c8 1 1 1 3sreplicaset.apps/grpc-server-5d7fd9cb89 1 1 1 7s
- Now I'll curl the client, since I'm running on minikube (1 node cluster) the IP of the node is the IP of the cluster, I'll pair the IP with the NodePort assigned.
$ kubectl cluster-infoKubernetes master is running at https://172.17.0.4$ curl http://172.17.0.4:32224Client is running!!!$ curl http://172.17.0.4:32224/hello?firstName=CedricHello Cedric
If you have any questions, let me know in the comments!