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

前端 未结 9 1397
庸人自扰
庸人自扰 2020-11-30 17:50

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.

相关标签:
9条回答
  • 2020-11-30 18:18
    kubectl get pods -o wide
    

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

    0 讨论(0)
  • 2020-11-30 18:20

    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.

    0 讨论(0)
  • 2020-11-30 18:23

    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 .

    0 讨论(0)
  • 2020-11-30 18:26

    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

    0 讨论(0)
  • 2020-11-30 18:28

    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:

    • hostname is set to POD's name but this might change in the future
    • kubectl was manually placed in the container (possibly when the image was built)
    • Kubernetes provides a service account credential to the container implicitly as described in Accessing the Cluster / Accessing the API from a Pod, i.e. /var/run/secrets/kubernetes.io/serviceaccount in the container
    0 讨论(0)
  • 2020-11-30 18:30

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

    0 讨论(0)
提交回复
热议问题