Communicate between pods in kubernetes

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


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)


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.

Tags:

Kubernetes