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

前端 未结 9 1497
庸人自扰
庸人自扰 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: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

提交回复
热议问题