【问题标题】:How do you get a Kubernetes pod's name from its IP address?如何从 IP 地址获取 Kubernetes pod 的名称?
【发布时间】:2017-05-24 14:23:35
【问题描述】:

如何从其 IP 地址中获取 pod 的名称?不管kubectl在哪里被调用,kubectl + sed/awk/grep/etc的魔法咒语是什么?

【问题讨论】:

    标签: kubernetes


    【解决方案1】:

    示例:

    kubectl get pods -o wide
    NAME                               READY     STATUS    RESTARTS   AGE       IP            NODE
    alpine-3835730047-ggn2v            1/1       Running   0          5d        10.22.19.69   ip-10-35-80-221.ec2.internal 
    

    通过 IP 获取 pod 名称

    kubectl get --all-namespaces  --output json  pods | jq '.items[] | select(.status.podIP=="10.22.19.69")' | jq .metadata.name
    "alpine-3835730047-ggn2v"
    

    通过IP获取容器名称

    kubectl get --all-namespaces  --output json  pods | jq '.items[] | select(.status.podIP=="10.22.19.69")' | jq .spec.containers[].name
    "alpine"
    

    【讨论】:

    • 完美。谢谢!
    【解决方案2】:

    无需额外工具即可完成,只需 kubectl 即可:

    kubectl get pods -o custom-columns=:metadata.name --no-headers=true --field-selector status.podIP=<pod-ip-address-goes-here>
    

    【讨论】:

      【解决方案3】:

      另一种通过 ip 地址获取 pod 名称的方法是这样的:

      $ kubectl get pods --all-namespaces -o wide | grep 10.2.6.181
      
      jenkins       jenkins-2-7d6d7fd99c-9xgkx                                                2/2     Running            3          12d   10.2.6.181      ip.ap-southeast-2.compute.internal    <none>
      

      在本例中,Pod 名称为“jenkins-2-7d6d7fd99c-9xgkx”,IP 地址为“10.2.6.181”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-14
        • 2019-10-25
        相关资源
        最近更新 更多