Can a pod directly call another pod?

10/9/2019

If I have 2 pods is there a way for them to talk to each other without any other resource created and used?

The question goes for the both situations - if they are in the same namespace or in different ones.

-- LIvanov
kubernetes
pod

2 Answers

10/9/2019

Yes, they can!

Assuming you don't have any network policies restricting the calls, it just need to know its DNS name, this is how it works:

  • the cluster must have DNS enabled
  • if the pods are manually create on the same namespace(not via deployment), you just need make a call to the podname that act as the host.
    • POD1 running on namespace NS1 exposing the container port 31333
    • POD2 running on namespace NS1
    • POD2 call POD1 via http://POD1:31333
  • if the pods are on different namespaces, you need to include the namespace to the host.
    • POD1 running on namespace NS1 exposing the container port 31333
    • POD2 running on namespace NS2
    • POD2 call POD1 via http://POD1.NS1:31333
  • if the pod is being created by a deployment, it's name is dynamic, is hard to predic, in this case, you need a service to expose the pods to others by using a common name(the service)
    • DEPLOYMENT1 deployed to namespace NS1 will create a pod with following format deploymentname-hash(example: DEPLOYMENT1-f82hsh)
    • DEPLOYMENT1-f82hsh is the pod created by the deployment and is running on namespace NS1, created exposing the container port 31333
    • POD2 running on namespace NS2
    • POD2 could call DEPLOYMENT1-f82hsh via http://DEPLOYMENT1-f82hsh.NS1:31333, but because the name is dynamic, at any time it could change to something else and break POD2
    • The solution is deploy service SVC1 that forward requests to DEPLOYMENT1 pods
    • POD2 then call http://SVC1:31333 that will forward the call to DEPLOYMENT1-f82hsh or whatever pod is available in the DEPLOYMENT1.

The scenarios above assume you haven't set the hostname neither subdomain in the pod and is using the default configuration.

In more advanced scenarios you would also use the cluster dns suffix to call these services. The following docs describes everything in more details https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/

-- Diego Mendes
Source: StackOverflow

10/9/2019

I would answer yes to your question... There is multiple question to speak to a service like the one ShreePrakash gave you and the same can be apply to a pod.

Here is another question in relation: 2 Kubernetes pod communicating without knowing the exposed address

This answers your question as you should be able to do the same with PODNAME.PODNAMESPACE:PORT and it should work.

Now why is it not done? Simply because pod have a random ID added to their names at creation (something like: nginx-ingress-1234456) and if it crashes and get recreates the name won't be the same. That applies to stateless apps, you may be able to deduct the name of the pod in a stateful state with only one pod...

That is why services are used to make it easier to target pod as their names is the one you declared on creation.

Hope this helps.

-- night-gold
Source: StackOverflow