Kubernetes
Pod IP
Networking
Cluster Management
Kubernetes Pods

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.

Practice system design

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:

bash
kubectl get pods -A -o wide

That shows pod names, namespaces, nodes, and IP addresses. If you want a cleaner machine-readable list, use jsonpath:

bash
kubectl get pods -n my-ns \
  -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.podIP}{"\n"}{end}'

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:

yaml
1apiVersion: rbac.authorization.k8s.io/v1
2kind: Role
3metadata:
4  name: pod-reader
5  namespace: my-ns
6rules:
7  - apiGroups: [""]
8    resources: ["pods"]
9    verbs: ["get", "list"]

Once the service account is allowed to list pods, code running inside the pod can call the API server:

bash
1TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
2CACERT=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt
3
4curl -s --cacert "$CACERT" \
5  -H "Authorization: Bearer $TOKEN" \
6  "https://kubernetes.default.svc/api/v1/namespaces/my-ns/pods"

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:

python
1import requests
2
3token = open("/var/run/secrets/kubernetes.io/serviceaccount/token").read().strip()
4ca = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
5url = "https://kubernetes.default.svc/api/v1/namespaces/my-ns/pods"
6
7response = requests.get(
8    url,
9    headers={"Authorization": f"Bearer {token}"},
10    verify=ca,
11    timeout=5,
12)
13
14pod_ips = [item["status"].get("podIP") for item in response.json()["items"]]
15print(pod_ips)

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:

bash
kubectl get pods -n my-ns -l app=web \
  -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.podIP}{"\n"}{end}'

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 Service or headless Service instead of relying on raw pod IPs.

Summary

  • Use kubectl get pods -o wide or a jsonpath query 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.