How to get pod IPs in Kubernetes with kubectl or from inside a pod
Codemia Knowledge Hub ·
Ask the Kubernetes API. From an admin machine, kubectl get pods -o wide shows every pod's IP. From inside a pod, call the in-cluster API, which works only if that pod's service account has permission to list pods. Pods do not get a list of other pod IPs in their environment variables.
Listing pod IPs with kubectl
This shows pod names, namespaces, nodes and IP addresses. For a clean, machine-readable list, use jsonpath:
This is usually the right answer when the need is operational rather than inside an application.
Narrowing by namespace or label
You rarely need every pod IP in the cluster. Restricting the query to one namespace or one label gives more useful output, is less noisy in large clusters and needs a smaller RBAC scope:
Getting pod IPs from inside a pod
To discover other pods, a pod has to call the Kubernetes API, and that requires RBAC permissions. An example Role:
Once the service account can list pods, code in the pod can call the API server:
The response is the pod list for the namespace. It includes each pod's status, with the IP in .status.podIP.
The same call from a Python application, useful for a controller, sidecar or diagnostics tool:
When pod IPs are the wrong tool
Pod IPs are usually ephemeral. A restarted pod often gets a new IP, so hard-coding or tracking IPs directly is brittle. For stable service-to-service communication, Kubernetes Service objects are normally the better abstraction. If you need per-pod discovery, a headless Service or a direct API query is usually better than distributing lists of pod IPs yourself.
Related reading
- how to get container host machine ip address and container name in EKS
- How to get current date in Helm
- How to get current namespace from running pod usin client api
- How to Get Current Pod in Kubernetes Java Application
- How to get back Kafka producer and consumer configuration (Java API)?
- How to get bearer token from header of a request in java spring boot?
- how to get hold of the azure kubernetes cluster outbound ip address
- How to get HTTP/2 working in a Kubernetes cluster using ingress-nginx
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a GPU Cluster Scheduler for ML Training: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.