Grabbing a pod's IP address from another pod

4/19/2019

I have a deployment pod that needs to grab another the IP address of another deployment pod and use that as an environment variable. The closest I could find was this how-to-know-a-pods-own-ip-address-from-inside-a-container-in-the-pod

I know I can grab the IP address of a service using the environment variable:

lt;SVC NAME>_SERVICE_HOST injected in a pod that gets created after this service. Is there a similar way to inject a deployment pod's IP address into another deployment pod after the first gets created?

-- horcle_buzz
environment-variables
inject
ip
kubernetes

2 Answers

4/20/2019

There is not way currently to find another pod's IP in DNS or environment variables. For that you need to query Kubernetes API. You may create serviceaccount with pod and deployment list permissions and then use Kubernetes API library or kubectl.

-- Vasily Angapov
Source: StackOverflow

4/20/2019

You should consider exposing your target pod through a ClusterIP service, and access that pod using the service's cluster DNS FQDN. Using this method, you don't have to worry about exactly what IP your target pod is at because the Kube proxy will take care of all the DNS and routing for you. You will then only need to know what the ClusterIP service endpoint is and access your target pod through that.

The official docs contain a great case study and an interactive tutorial on this subject.

Hope this helps!

-- Frank Yucheng Gu
Source: StackOverflow