CyberKeeda In Social Media

Kubernetes Inter-pod communication within a cluster.

 

In this post, what are the ways through which we can configure our pods to communicate with each other within the same Kubernetes cluster.

In order to understand the same, we have create a lab scenario where we have two pods running inside the same cluster.


We will focus on two namespace 
  • default
  • web-apps
Let's see what are the pods running in both namespaces.
  • Pods running under default namespace.


  • Pods running under web-apps namespace.


What's the application - So we have our application pod named as "genache-cli-deploymnet" running under default namespace, within this lab environment we will know how we can establish communication between microservices like my-shell and weapp-shell to genache-cli-core.

Here are the different ways..

Using Pod's IP.

Every pod gets an IP from the defined CIDR range, which can be used to communicate directly from each other, irrespective of namespaces.
Thus a simple pattern of http://<pod-ip-address>:<container-port-number>

So as per our lab environment, we will try to establish a connection to genache-cli running with IP Address as curl 10.1.1.160 and on Port 8545

> kubectl get pods -o wide

NAME                                     READY   STATUS    RESTARTS         AGE   IP           NODE             NOMINATED NODE   READINESS GATES
genache-cli-deployment-8f48b88fb-dqnkx   1/1     Running   20 (2d10h ago)   30d   10.1.1.160   docker-desktop   <none>           <none>
my-shell                                 1/1     Running   0                37m   10.1.1.162   docker-desktop   <none>           <none>
Output from my-shell running on web-apps namespace
root@webapp-shell:/# curl http://10.1.1.160:8545/
400 Bad Request 

Output from webapp-shell running on default namespace.

root@my-shell:/# curl http://10.1.1.160:8545/
400 Bad Request


Create Service for Pod to Pod Communication for same namespace.

For the above genache-cli deployment, we have created a service by below details.
  • Name - genache-cli-service
  • Service type - ClusterIP
  • Service Port - 8545

Now, we would like to establish the connectivity from pod name my-shell to genachi-cli pod via genache-cli-service. 
  • Using Environment variables to fetch Service Name and Service Port.
    • Login to my-shell pod and execute command env
        
    • Here we are interested on genache cli service details that is host and port, we will filter it out using grep for the same above command.

    • So we can fetch our details and use the pattern as below to establish a connection between pods.
  http://<SERVICE_NAME>_SERVICE_HOST}:${<SERVICE_NAME>_SERVICE_PORT}
  • Here in this case, we can replace it with the following as per our lab environment.
    • SERVICE_NAME --  GENACHE_CLI_SERVICE
Now, in case if we want to connect it using environment variables from our pods.

From pod "my-shell"
root@my-shell:/# curl http://${GENACHE_CLI_SERVICE_SERVICE_HOST}:${GENACHE_CLI_SERVICE_SERVICE_PORT}
400 Bad Request

 From pod "webapps-shell"
root@webapps-shell:/# curl http://${GENACHE_CLI_SERVICE_SERVICE_HOST}:${GENACHE_CLI_SERVICE_SERVICE_PORT}
curl: (3) URL using bad/illegal format or missing URL

  • Using Service Names.
We can directly establish a connection using the format as http://<service-name>:<service-port>
In our lab environment, we can replace the same by http://genache-cli-service:8545

Now, let's try to establish a connection using the same from our pods.

From pod "my-shell"
root@my-shell:/# curl http://genache-cli-service:8545
400 Bad Request

 From pod "webapps-shell"
root@webapps-shell:/# curl http://genache-cli-service:8545
curl: (6) Could not resolve host: genache-cli-service

Conclusion : 

  • Service name can be used to establish connection within same namespace.
  • Service name should be used instead of POD IPs directly.

Service Communication for different namespace.

In order to establish the connection between pods separated by namespaces, we can use the name based service on cluster.
Use the below naming standards to establish communication between services across different name space.
<service-name>.<namespace-name>.svc.cluster.local
For our lab environment, we will replace it with our environment as
genache-cli-service.default.svc.cluster.local

Now, let's try to establish a connection using the same from our pods.

From pod "my-shell"
root@my-shell:/# curl http://genache-cli-service.default.svc.cluster.local:8545
400 Bad Request

 From pod "webapps-shell"
root@webapps-shell:/# curl http://genache-cli-service.default.svc.cluster.local:8545
400 Bad Request

No comments:

Post a Comment

Designed By Jackuna