Some clarifications (not really an answer)
In kubernetes, every pod gets assigned an IP address, and every container in the pod gets assigned that same IP address. Thus, as Alex Robinson stated in his answer, you can just use hostname -i
inside your container to get the pod IP address.
I tested with a pod running two dumb containers, and indeed hostname -i
was outputting the same IP address inside both containers. Furthermore, that IP was equivalent to the one obtained using kubectl describe pod
from outside, which validates the whole thing IMO.
However, PiersyP's answer seems more clean to me.
Sources
From kubernetes docs:
The applications in a pod all use the same network namespace (same IP and port space), and can thus “find” each other and communicate using localhost. Because of this, applications in a pod must coordinate their usage of ports. Each pod has an IP address in a flat shared networking space that has full communication with other physical computers and pods across the network.
Another piece from kubernetes docs:
Until now this document has talked about containers. In reality, Kubernetes applies IP addresses at the Pod scope - containers within a Pod share their network namespaces - including their IP address. This means that containers within a Pod can all reach each other’s ports on localhost.
printenv | grep '10.254.24.167'
doesn't return anything except the user defined variable MY_POD_IP. Anyway, take my vote sir :) – Cytolysis