How to get all Kubernetes pod IP in each pods?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
If you need the IP addresses of Kubernetes pods, the reliable source is the Kubernetes API. From an admin machine you can query it through kubectl, and from inside a pod you can query the in-cluster API if that pod's service account has permission to list pods.
Get Pod IPs With kubectl
From outside the cluster or from an operator shell, the simplest command is:
That shows pod names, namespaces, nodes, and IP addresses. If you want a cleaner machine-readable list, use jsonpath:
This is usually the best answer when the question is operational rather than application-level.
Get Pod IPs From Inside a Pod
A pod does not automatically know every other pod IP. To discover them, it has to call the Kubernetes API. That requires RBAC permissions.
Example Role:
Once the service account is allowed to list pods, code running inside the pod can call the API server:
The response contains each pod's status, including .status.podIP.
A Small In-Cluster Python Example
If the caller is an application instead of a shell script, the same idea works in Python:
This is useful when a controller, sidecar, or diagnostics tool needs the information programmatically.
Know When Pod IPs Are the Wrong Abstraction
Pod IPs are usually ephemeral. A restarted pod often gets a new IP, which means direct IP tracking is fragile unless your application truly needs it.
For stable service-to-service communication, Kubernetes Service objects are normally the better abstraction. If you need per-pod discovery, a headless service or direct API query may make more sense than manually distributing pod IP lists.
Namespace Scope Matters
In many clusters, the correct answer is not "all pod IPs everywhere" but "all pod IPs in one namespace" or "all pod IPs matching one label." Narrowing the query makes the output more useful and reduces unnecessary RBAC scope.
For example:
That narrower query is usually easier to secure and easier to consume. It is also less noisy in larger clusters. It usually matches real access patterns better too. That makes troubleshooting simpler later.
Common Pitfalls
- Pods do not automatically receive a list of every other pod IP in environment variables.
- Querying the Kubernetes API from inside a pod requires RBAC permissions.
- Pod IPs can change when pods restart, so hard-coding them is brittle.
- If your real goal is service discovery, use a
Serviceor headlessServiceinstead of relying on raw pod IPs.
Summary
- Use
kubectl get pods -o wideor ajsonpathquery to list pod IPs operationally. - From inside a pod, query the Kubernetes API with a properly authorized service account.
- Treat pod IPs as ephemeral data, not as stable addresses.
- For most application traffic, Kubernetes services are a better fit than direct pod IP management.
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 Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.