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

前端 未结 9 1398
庸人自扰
庸人自扰 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:30
    POD_HOST=$(kubectl get pod $POD_NAME --template={{.status.podIP}})
    

    This command will return you an IP

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

    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

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

    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.

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