Kubernetes How do I get all pods in a namespace using the python api?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The Kubernetes Python client can list every pod in a namespace with a single API call, but the setup differs depending on where the code runs. Local scripts usually load a kubeconfig file, while code running inside the cluster uses the pod service account. Once configuration is loaded, list_namespaced_pod is the standard method.
Load Client Configuration Correctly
Before you can query pods, the Python client must know how to reach the API server and how to authenticate.
For local development:
For code running inside a Kubernetes pod:
Do not use both in the same execution path unless you have a very deliberate fallback strategy. In most applications, one environment is known ahead of time.
List All Pods in a Namespace
The direct call is list_namespaced_pod.
The result is a V1PodList. The actual pod objects are stored in pods.items.
If you want a little more context:
That is often enough for scripts that inventory workloads or check pod state.
Filter with Selectors Instead of Post-Processing
If the namespace is large, pull only what you need. The API supports both label selectors and field selectors.
Label selector example:
Field selector example for only running pods:
This is better than fetching all pods and filtering in Python because the API server does part of the work for you.
Handle API Errors Explicitly
Production code should not assume the call always succeeds. Namespaces may not exist, RBAC may block access, or credentials may be invalid.
Typical meanings:
- '
403usually means an RBAC problem' - '
404usually means the namespace name is wrong' - connection or TLS errors usually mean kubeconfig or cluster access is broken
Large Namespaces May Need Pagination
For very large namespaces, the response may be paginated. The client exposes this with limit and _continue.
You can ignore this in small clusters, but it matters in shared production namespaces with hundreds or thousands of pods.
Convert Pod Objects Into Simpler Data
If the result is being passed to other code, normalize it into dictionaries or dataclasses instead of passing client objects everywhere.
That makes downstream logging, JSON output, and reporting much easier.
Common Pitfalls
- Using
load_kube_config()inside a pod instead ofload_incluster_config(). - Forgetting RBAC permissions for the service account.
- Pulling every pod and filtering client-side when selectors would do the job.
- Ignoring pagination in large namespaces.
- Treating
pods.itemsas plain dictionaries instead of typed client objects.
Summary
- Use
CoreV1Api.list_namespaced_podto get all pods in a namespace. - Load configuration from kubeconfig locally or in-cluster config inside Kubernetes.
- Use label and field selectors when you only need a subset of pods.
- Catch
ApiExceptionso permission and connectivity failures are visible. - Normalize results if the pod list is feeding automation or reports.

