How to List Pods in Kubernetes: Simple Commands Explained
Use the
kubectl get pods command to list all pods in the current namespace. To see pods in all namespaces, add the --all-namespaces flag.Syntax
The basic command to list pods is kubectl get pods. You can add options like -n <namespace> to specify a namespace or --all-namespaces to list pods across all namespaces.
kubectl: The Kubernetes command-line tool.get pods: Command to retrieve pod information.-n <namespace>: Optional flag to specify a namespace.--all-namespaces: Optional flag to list pods in all namespaces.
bash
kubectl get pods kubectl get pods -n <namespace> kubectl get pods --all-namespaces
Example
This example shows how to list all pods in the default namespace and then list pods in all namespaces.
bash
kubectl get pods kubectl get pods --all-namespaces
Output
NAME READY STATUS RESTARTS AGE
my-app-pod-1 1/1 Running 0 3h
my-app-pod-2 1/1 Running 0 3h
NAMESPACE NAME READY STATUS RESTARTS AGE
default my-app-pod-1 1/1 Running 0 3h
default my-app-pod-2 1/1 Running 0 3h
kube-system coredns-558bd4d5db-7x9zv 1/1 Running 0 5d
Common Pitfalls
One common mistake is forgetting to specify the namespace if your pods are not in the default namespace. This results in no pods being listed. Another is not having the correct Kubernetes context set, which can cause commands to run against the wrong cluster.
bash
kubectl get pods
# May show no pods if you are in the wrong namespace
kubectl get pods -n my-namespace
# Correct way to list pods in a specific namespaceQuick Reference
| Command | Description |
|---|---|
| kubectl get pods | List pods in the current namespace |
| kubectl get pods -n | List pods in a specific namespace |
| kubectl get pods --all-namespaces | List pods in all namespaces |
| kubectl get pods -o wide | List pods with more details like node and IP |
Key Takeaways
Use
kubectl get pods to list pods in the current namespace.Add
-n <namespace> to list pods in a specific namespace.Use
--all-namespaces to see pods across all namespaces.Ensure your Kubernetes context is set correctly to avoid listing pods from the wrong cluster.
Use
-o wide for more detailed pod information.