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

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"

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>

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".

Tags:

Kubernetes