繁体   English   中英

如何从 IP 地址获取 Kubernetes pod 的名称?

[英]How do you get a Kubernetes pod's name from its IP address?

如何从 IP 地址获取 Pod 的名称? 无论kubectl在哪里调用, kubectl + sed / awk / grep /etc 的神奇咒语是什么?

例子:

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"

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

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

另一种通过 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>

在此示例中,IP 地址“10.2.6.181”的 pod 名称为“jenkins-2-7d6d7fd99c-9xgkx”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM