List pods per namespace in kubernetes
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
To list pods in a specific Kubernetes namespace, use kubectl get pods -n <namespace>. To list pods across all namespaces, use kubectl get pods -A (or --all-namespaces). For structured output that groups pods by namespace, combine kubectl with --output formatting or pipe through jq. The Kubernetes API also supports namespace-scoped queries through client libraries for programmatic access.
List Pods in a Specific Namespace
List Pods Across All Namespaces
Count Pods Per Namespace
Filter Pods by Status
Custom Output Formats
Using Labels and Selectors
Programmatic Access (Python)
Watch for Pod Changes
Common Pitfalls
- Forgetting the
-nflag defaults to "default" namespace: Without-n, kubectl queries only the "default" namespace. Pods in other namespaces do not appear. Use-Ato see all namespaces, or set your context's default namespace withkubectl config set-context --current --namespace=my-namespace. - Confusing
--all-namespaceswith--all:-A(or--all-namespaces) lists pods across all namespaces. The--allflag is for other commands (likekubectl delete pods --all) and means "all resources of this type in the current namespace," not all namespaces. - Field selectors are limited compared to label selectors: Not all fields support field selectors.
status.phase,spec.nodeName, andmetadata.namespacework, but arbitrary fields likespec.containers[0].imagedo not. Usejqorjsonpathoutput with grep for filtering by unsupported fields. - Large clusters timing out with
-A: In clusters with thousands of pods,kubectl get pods -Amay time out or return partial results. Use--chunk-size=500to paginate results, or query specific namespaces individually to reduce the response size. - Not using
--no-headersin scripts: When piping kubectl output toawk,grep, orwc, the header row is counted as data. Add--no-headersto strip the column header line, or use-o jsonwithjqfor reliable programmatic parsing.
Summary
- Use
kubectl get pods -n <namespace>for a specific namespace,-Afor all namespaces - Pipe through
awk '{print $1}' | sort | uniq -cto count pods per namespace - Use
-o custom-columnsor-o jsonpathfor custom output formats - Filter with
--field-selectorfor status/node and-lfor label-based queries - Use the Python kubernetes client for programmatic access with
list_namespaced_pod()orlist_pod_for_all_namespaces()
Related reading
- Listing all resources in a namespace
- Liveness-Probe of one pod via another
- Liveness probe with http post
- Logs complaining extensions/v1beta1 Ingress is deprecated
- Logs in Kubernetes Pod not showing up
- Make RabbitMQ durable/persistent queues survive Kubernetes pod restart
- Manually change the status of a job to successful in kubernetes
- Many kubernetes secrets vs many keys in one k8s secret

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.