How to know a Pod's own IP address from inside a container in the Pod?

Kubernetes

Kubernetes Problem Overview


Kubernetes assigns an IP address for each container, but how can I acquire the IP address from a container in the Pod? I couldn't find the way from documentations.

Edit: I'm going to run Aerospike cluster in Kubernetes. and the config files need its own IP address. And I'm attempting to use confd to set the hostname. I would use the environment variable if it was set.

Kubernetes Solutions


Solution 1 - Kubernetes

The simplest answer is to ensure that your pod or replication controller yaml/json files add the pod IP as an environment variable by adding the config block defined below. (the block below additionally makes the name and namespace available to the pod)

env:
- name: MY_POD_NAME
  valueFrom:
    fieldRef:
      fieldPath: metadata.name
- name: MY_POD_NAMESPACE
  valueFrom:
    fieldRef:
      fieldPath: metadata.namespace
- name: MY_POD_IP
  valueFrom:
    fieldRef:
      fieldPath: status.podIP

Recreate the pod/rc and then try

echo $MY_POD_IP

also run env to see what else kubernetes provides you with.

Cheers

Solution 2 - Kubernetes

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.

Solution 3 - Kubernetes

kubectl describe pods <name of pod> will give you some information including the IP

Solution 4 - Kubernetes

POD_HOST=$(kubectl get pod $POD_NAME --template={{.status.podIP}})

This command will return you an IP

Solution 5 - Kubernetes

kubectl get pods -o wide

Give you a list of pods with name, status, ip, node...

Solution 6 - Kubernetes

The container's IP address should be properly configured inside of its network namespace, so any of the standard linux tools can get it. For example, try ifconfig, ip addr show, hostname -I, etc. from an attached shell within one of your containers to test it out.

Solution 7 - Kubernetes

You could use

kubectl describe pod `hostname` | grep IP | sed -E 's/IP:[[:space:]]+//'

which is based on what @mibbit suggested.

This takes the following facts into account:

Solution 8 - Kubernetes

Even simpler to remember than the sed approach is to use awk.

Here is an example, which you can run on your local machine:

kubectl describe pod `<podName>` | grep IP | awk '{print $2}'

The IP itself is on column 2 of the output, hence $2 .

Solution 9 - Kubernetes

In some cases, instead of relying on downward API, programmatically reading the local IP address (from network interfaces) from inside of the container also works.

For example, in golang: https://stackoverflow.com/a/31551220/6247478

Solution 10 - Kubernetes

Containers have the same IP with the pod they are in.

So from inside the container you can just do ip a and the IP you get is the one the pod has also.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyananaView Question on Stackoverflow
Solution 1 - KubernetesPiersyPView Answer on Stackoverflow
Solution 2 - KubernetesElouan Keryell-EvenView Answer on Stackoverflow
Solution 3 - KubernetesmibbitView Answer on Stackoverflow
Solution 4 - KubernetesAntonView Answer on Stackoverflow
Solution 5 - KubernetesBelen MartinView Answer on Stackoverflow
Solution 6 - KubernetesAlex RobinsonView Answer on Stackoverflow
Solution 7 - KubernetesAdam RomanekView Answer on Stackoverflow
Solution 8 - KuberneteswhirlwinView Answer on Stackoverflow
Solution 9 - KubernetesKevinView Answer on Stackoverflow
Solution 10 - KubernetesDimiDakView Answer on Stackoverflow