Communicate between pods in kubernetes

12/27/2018

I have something like this:

           POD-1
             |
 -------------------------
 ?|?        ?|?        ?|?
service-1 service-2 service-3

How do I communicate from a server inside a pod, to other servers in pods behind services?

-- Liron Navon
kubernetes

4 Answers

9/17/2019

The pods on the other namespace in the same cluster can be reached with just the svc-name.namespace-name

SVC Name: foo

Namespace Name: bar

eg: foo.bar

-- Sandy
Source: StackOverflow

12/27/2018

You need to have services for the pod that you want to access. You can just use internal endpoints of the corresponding services of the pod.

As example let's think there is a mysql pod and service corresponding to it as mysql-svc of type ClusterIP exposing port 3306 as below.

apiVersion: v1
kind: Service
metadata:
  name: mysql-svc
spec:
  ports:
  - name: db-port
    protocol: "TCP"
    port: 3306
    targetPort: 3306
  selector:
    app: mysql

And there is a separate pod of python application which uses that mysql. yo can access that mysql server inside pod using mysql://mysql-svc:3306/dbName which is the internal endpoint of mysql-svc

And if your pods are in two different namespaces (mysql in dev namespace and python app in qa namespace) you can use mysql-svc.dev.svc.cluster.local instead.

-- Hansika Madushan Weerasena
Source: StackOverflow

12/27/2018

If you have another service with pods, you can simply access by using the cluster internal DNS:

For service foo in namespace bar the url is foo.bar.svc.cluster.local. The last part cluster.local can change based on how you deployed the cluster. kops lets you specify different values for it.

When communicating within the same namespace, you don't even need bar you can just do http://foo/ or foo:port with different protocols (like mongo/rabbit/postgrest etc)

-- codebreach
Source: StackOverflow

12/27/2018

If you want to communicate internally within cluster without exposing to internet HTTP/HTTPS i would suggest you to deploy an nginx pod with nodeport and then provide your config in this nginx pod to route your request based on paths for each service. Refer: https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/#exposing-the-service

To access from internet refer https://kubernetes.io/docs/concepts/services-networking/ingress/

Alternatively you could just run each service on different nodeport as explained in https://kubernetes.io/docs/tasks/access-application-cluster/service-access-application-cluster/#creating-a-service-for-an-application-running-in-two-pods

-- Ahmed Ulde
Source: StackOverflow