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
1# List pods in the "default" namespace
2kubectl get pods -n default
3
4# List pods in the "kube-system" namespace
5kubectl get pods -n kube-system
6
7# With more detail (node, IP, status)
8kubectl get pods -n default -o wide
List Pods Across All Namespaces
1# Short form
2kubectl get pods -A
3
4# Long form
5kubectl get pods --all-namespaces
6
7# With additional columns
8kubectl get pods -A -o wide
9
10# Output:
11# NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE
12# default web-app-7d4b8c9f6-abc12 1/1 Running 0 2h 10.0.1.5 node-1
13# kube-system coredns-5d78c9869d-xyz89 1/1 Running 0 24h 10.0.0.3 node-2
14# monitoring prometheus-0 1/1 Running 0 12h 10.0.2.8 node-1
Count Pods Per Namespace
1# Count pods grouped by namespace
2kubectl get pods -A --no-headers | awk '{print $1}' | sort | uniq -c | sort -rn
3
4# Output:
5# 12 kube-system
6# 8 default
7# 5 monitoring
8# 3 ingress-nginx
9
10# Using jsonpath
11kubectl get pods -A -o jsonpath='{range .items[*]}{.metadata.namespace}{"\n"}{end}' | sort | uniq -c | sort -rn
Filter Pods by Status
1# Running pods in a namespace
2kubectl get pods -n default --field-selector=status.phase=Running
3
4# Failed pods across all namespaces
5kubectl get pods -A --field-selector=status.phase=Failed
6
7# Pods on a specific node
8kubectl get pods -A --field-selector=spec.nodeName=node-1
1# JSON output for a namespace
2kubectl get pods -n default -o json
3
4# YAML output
5kubectl get pods -n default -o yaml
6
7# Custom columns
8kubectl get pods -A -o custom-columns=\
9NAMESPACE:.metadata.namespace,\
10NAME:.metadata.name,\
11STATUS:.status.phase,\
12RESTARTS:.status.containerStatuses[0].restartCount,\
13NODE:.spec.nodeName
14
15# JSONPath — pod names grouped by namespace
16kubectl get pods -A -o jsonpath='{range .items[*]}{.metadata.namespace}{"\t"}{.metadata.name}{"\n"}{end}'
Using Labels and Selectors
1# List pods with a specific label
2kubectl get pods -n default -l app=web
3
4# Multiple label selectors
5kubectl get pods -A -l 'app=web,environment=production'
6
7# Exclude by label
8kubectl get pods -A -l 'app!=test'
9
10# List all pods with their labels
11kubectl get pods -A --show-labels
Programmatic Access (Python)
1from kubernetes import client, config
2
3config.load_kube_config() # or config.load_incluster_config() in a pod
4v1 = client.CoreV1Api()
5
6# List pods in a specific namespace
7pods = v1.list_namespaced_pod(namespace="default")
8for pod in pods.items:
9 print(f"{pod.metadata.name}: {pod.status.phase}")
10
11# List pods across all namespaces
12all_pods = v1.list_pod_for_all_namespaces()
13by_namespace = {}
14for pod in all_pods.items:
15 ns = pod.metadata.namespace
16 by_namespace.setdefault(ns, []).append(pod.metadata.name)
17
18for ns, names in sorted(by_namespace.items()):
19 print(f"\n{ns} ({len(names)} pods):")
20 for name in names:
21 print(f" - {name}")
Watch for Pod Changes
1# Watch pods in real-time (updates as pods start/stop)
2kubectl get pods -n default -w
3
4# Watch across all namespaces
5kubectl get pods -A -w
1from kubernetes import client, config, watch
2
3config.load_kube_config()
4v1 = client.CoreV1Api()
5w = watch.Watch()
6
7for event in w.stream(v1.list_namespaced_pod, namespace="default"):
8 pod = event['object']
9 print(f"{event['type']}: {pod.metadata.name} -> {pod.status.phase}")
Common Pitfalls
Forgetting the -n flag defaults to "default" namespace: Without -n, kubectl queries only the "default" namespace. Pods in other namespaces do not appear. Use -A to see all namespaces, or set your context's default namespace with kubectl config set-context --current --namespace=my-namespace.
Confusing --all-namespaces with --all: -A (or --all-namespaces) lists pods across all namespaces. The --all flag is for other commands (like kubectl 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, and metadata.namespace work, but arbitrary fields like spec.containers[0].image do not. Use jq or jsonpath output with grep for filtering by unsupported fields.
Large clusters timing out with -A: In clusters with thousands of pods, kubectl get pods -A may time out or return partial results. Use --chunk-size=500 to paginate results, or query specific namespaces individually to reduce the response size.
Not using --no-headers in scripts: When piping kubectl output to awk, grep, or wc, the header row is counted as data. Add --no-headers to strip the column header line, or use -o json with jq for reliable programmatic parsing.
Summary
Use kubectl get pods -n <namespace> for a specific namespace, -A for all namespaces
Pipe through awk '{print $1}' | sort | uniq -c to count pods per namespace
Use -o custom-columns or -o jsonpath for custom output formats
Filter with --field-selector for status/node and -l for label-based queries
Use the Python kubernetes client for programmatic access with list_namespaced_pod() or list_pod_for_all_namespaces()