How do I get a pod's name from its IP address? What's the magic incantation of kubectl
+ sed
/awk
/grep
/etc regardless of where kubectl
is invoked?
Asked
Active
Viewed 2.9k times
45

Matthew Adams
- 2,059
- 2
- 21
- 31
3 Answers
56
Example:
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
get pod name by IP
kubectl get --all-namespaces --output json pods | jq '.items[] | select(.status.podIP=="10.22.19.69")' | jq .metadata.name
"alpine-3835730047-ggn2v"
get container name by IP
kubectl get --all-namespaces --output json pods | jq '.items[] | select(.status.podIP=="10.22.19.69")' | jq .spec.containers[].name
"alpine"

Camil
- 7,800
- 2
- 25
- 28
-
Perfect. Thanks! – Matthew Adams Jan 10 '17 at 18:09
47
Can be done without additional tools, just kubectl is enough:
kubectl get pods -o custom-columns=:metadata.name --no-headers=true --field-selector status.podIP=<pod-ip-address-goes-here>

Harish Ambady
- 12,525
- 4
- 29
- 54
-
1Works great. Including `kubectl get`'s `--all-namespaces` flag like in the other answers is a helpful variation (e.g. checking IPs logged in a CNI plugin log file). – JKD May 04 '22 at 16:31
11
Another way to get pod name by ip address is like this:
$ 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>
In this example, the pod name is "jenkins-2-7d6d7fd99c-9xgkx" for ip address "10.2.6.181".

Leo Peng
- 119
- 1
- 4