33

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?

Matthew Adams
  • 1,826
  • 2
  • 19
  • 27

3 Answers3

42

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
  • 6,889
  • 2
  • 24
  • 28
31

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
  • 11,017
  • 4
  • 26
  • 54
  • Works 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
9

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
  • 91
  • 1
  • 2