133

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.

Shubham
  • 2,847
  • 4
  • 24
  • 37
yanana
  • 2,241
  • 2
  • 18
  • 28

10 Answers10

240

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.

starball
  • 20,030
  • 7
  • 43
  • 238
PiersyP
  • 5,003
  • 2
  • 33
  • 35
  • 2
    I've just tested your solution, and it works super fine. I'm 100% sure kubernetes doesn't have such an environment variable, because `printenv | grep '10.254.24.167'` doesn't return anything except the user defined variable MY_POD_IP. Anyway, take my vote sir :) – Elouan Keryell-Even Jan 08 '16 at 16:08
  • 1
    Have you ever tried to fetch the IP of the host through `status.hostIP`? – PEdroArthur May 02 '17 at 19:46
  • @PEdroArthur No I have not, afaik it is a recent addition. See the pull request here - https://github.com/kubernetes/kubernetes/pull/42717 – PiersyP May 03 '17 at 19:28
  • 1
    This is called "Downwad API": https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#the-downward-api – Alec Istomin Aug 08 '18 at 22:19
  • Thank you very much , this answer really make me a big faver. – Liping Huang Jul 20 '20 at 03:12
  • 1
    For a future-safer option, `status.podIPs` can be used, which will inject a comma-separated list of IP addresses (such as IPv4 and IPv6 on a dual-stack pod) – Michael Graff Jan 03 '21 at 15:34
  • Thanks, What if I have 3 replicas (instances) running and want to know the IP of each container. – PraveenMak May 01 '22 at 13:52
40

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.

Elouan Keryell-Even
  • 990
  • 1
  • 14
  • 36
27

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

Roberto Decurnex
  • 2,514
  • 1
  • 19
  • 28
mibbit
  • 4,997
  • 3
  • 26
  • 34
25
kubectl get pods -o wide

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

Belen Martin
  • 507
  • 5
  • 7
19
POD_HOST=$(kubectl get pod $POD_NAME --template={{.status.podIP}})

This command will return you an IP

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Anton
  • 321
  • 3
  • 2
  • 3
    Thanks for your answer. But I want to know the IP address in a container in a Pod. So I want to know it without using `kubectl`. – yanana Jan 24 '17 at 06:41
14

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.

Alex Robinson
  • 12,633
  • 2
  • 38
  • 55
11

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:

Adam Romanek
  • 1,809
  • 1
  • 19
  • 36
  • 1
    Don't pipe `grep` to `sed`: `kubectl describe pod $POD | sed -nE '/IP/s/IP:[[:space:]]+//p'` (but awk is better for this than sed) – William Pursell May 04 '18 at 22:41
3

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 .

whirlwin
  • 16,044
  • 17
  • 67
  • 98
  • 4
    I assume it's because you're not answering the question. The questions is `but how can I acquire the IP address from a container in the Pod?` You're assuming the person in question has access to `kubectl` on the pod itself. – Seivan Dec 18 '17 at 09:31
  • 4
    Don't pipe grep to awk: `kubectl describe pod $POD | awk '/IP/{print $2}'` – William Pursell May 04 '18 at 22:38
2

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

Kevin
  • 2,775
  • 4
  • 16
  • 27
  • Is this the GO equivalent of doing `hostname -i` and taking the first ipv4 address that isn't loopback? – Phil Jan 12 '22 at 14:15
-1

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.

DimiDak
  • 4,820
  • 2
  • 26
  • 32