Kubernetes
Pod IP
Networking
Cluster Management
Kubernetes Pods

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

bash
kubectl get pods -A -o wide

This shows pod names, namespaces, nodes and IP addresses. For a clean, 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 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:

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

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:

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 can list pods, code in 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 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:

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)

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.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design