In Kubernetes, Pods provide a shared environment for one or more containerized applications, allowing the containers to communicate and share common resources.
The short answer
To list all the Pods in your Kubernetes cluster at once, you can use the kubectl get pods command as follows:
$ kubectl get pods
Run in Warp
This command provides details for every Pod within your cluster including their status, allowing you to quickly evaluate its overall health and troubleshoot any potential Pod issues, such as non-running or failed Pods.
Easily retrieve this command using Warp’s AI Command Suggestions
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:
Entering k8s list pods in the AI Command Suggestions will prompt a kubectl command that can then quickly be inserted into your shell by doing CMD+ENTER.
Understanding the output of the kubectl get pods command
By default, the kubectl get pods command provides details about all the Pods in your cluster in a tabular format:
Where:
- NAME is the unique name of the Pod.
- READY is the number of containers in the Pod that are ready. For example, 1/1 indicates that all containers in the Pod are ready and running,whereas 0/2 indicates that none are running.
- STATUS is the current execution of the status (such as Pending, Running, Succeeded, Failed or Unknown).
- RESTARTS is the number of times the Pod has restarted. It will indicate the Pod’s stability and the occurrence of any issues that might have initiated the restart.
- AGE is the time when Pod was created.
Understanding the Pod status
The Pod Status property gives an immediate overview of the Pod’s current health.
It takes 5 possible values:
- Pending: the Pod is created, but its containers are not yet ready to run.
- Running: The Pod is actively running on the Node, with at least one container in a running state.
- Succeeded: all containers in the Pod have terminated successfully (with an exit status of 0).
- Failed: one or more containers within your Pod have terminated with a non-zero exit status, signifying an error.
- Unknown: the state of the Pod cannot be determined and is typically temporary due to Node failures or network issues.
Note that if a Pod gets stuck in a status different from Running for a long period, this may indicate a potential issue with the functioning of the Pod.
Listing all Pods with additional information
To display additional information about the Pods, you can use the -o wide flag (short for --output wide) as follows:
$ kubectl get pods -o wide
Run in Warp
Where:
- IP is the IP address of the Pod.
- NODE is the Node where the Pod is currently running.
- NOMINATED NODE is the preferred Node to run the Pod on, identified by the Kubernetes scheduler based on the Pod’s resource requirements.
- READINESS GATES are the conditions that must be satisfied for the Pod to be considered ready.
Additionally, you can read our article on how to describe a Kubernetes Pod with kubectl to retrieve more comprehensive information about Pods.
Listing Pods by name
To list one or more Pods by name in your Kubernetes cluster, you can use the kubectl get pods command as follows:
$ kubectl get pods <pod_name …>
Run in Warp
Where:
- pod_name is a list of Pod names separated by a space character.
For example:
$ kubectl get pods myPod1 myPod2
Run in Warp
Upon execution, the above command will output a table of information about the Pods named myPod1 and myPod2, including their name, status, restart count, and age.
Note that if you specify a Pod name that does not exist, the command will result in an error indicating that the specified Pod was not found.
Listing Pods in a namespace
In Kubernetes, namespaces provide a logical way to separate resources within an application.
To list all the Pods in a specified namespace, you can use the kubectl get pods command with the -n flag (short for --namespace):
$ kubectl get pods -n <namespace>
Run in Warp
For example:
$ kubectl get pods -n myNamespace
Run in Warp
Upon execution, the above command will list all the Pods and their status in the Namespace named myNamespace.
If you want to learn more about namespaces, you can read our article on how to create a namespace in Kubernetes with kubectl.
Listing Pods in all namespaces
By default, the kubectl get pods only lists Pods within the current namespace.
To list Pods across all namespaces, you can use the kubectl get pods command with the --all-namespaces flag:
$ kubectl get pods --all-namespaces
Run in Warp
Listing Pods by label
Labels are key-value pairs attached to the Kubernetes objects that organize resources based on specific criteria.
To list Pods based on a specific label, you can use the kubectl get pods command with the -l flag (short for --label):
$ kubectl get pods -l <label>=<value>
Run in Warp
Where:
- label is the key of the label.
- value is the value associated with the label.
For example:
$ kubectl get pods -l app=myapp
Run in Warp
Upon execution, the above command will display all Pods with the label app=myapp assigned to them.
Displaying the labels of all Pods
To view the labels associated with all the Pods at once, you can use the kubectl get pods command with the --show-labels flag:
$ kubectl get pods --show-labels
Run in Warp
Upon execution, the above command will output an additional column showing any labels associated with Pods.
Listing Pods in the YAML and JSON formats
By default, the output format of the kubectl get pods command is a table. However, you can specify other formats, such as YAML or JSON using the -o flag:
$ kubectl get pods -o <output_format>
Run in Warp
Where:
- output_format is one of yaml or json.
For example:
$ kubectl get pods -o yaml
Run in Warp
Upon execution, the above command will display comprehensive details about all the Pods in the specified YAML format.
Filtering Pods using a field selector
To filter the list of Pods based on a specific field, you can use the kubectl get pods command with the --field-selector flag:
$ kubectl get pods --field-selector=<field_name>=<field_value>
Run in Warp
Where:
- field_name is a JSONPath expression used for selecting a specific field.
- field_value is the value for the specified field.
For example, this command will filter and display the list of all Pods running on the Node named node01:
$ kubectl get pods --field-selector=spec.nodeName=node01
Run in Warp
And this command will filter and display all Pods in a Running phase:
$ kubectl get pods --field-selector=status.phase=Running
Run in Warp
You can learn more about JSONPath expressions by visiting the official Kubernetes documentation page.
Sorting the output of the kubectl get pods command
To sort the output of the kubectl get pods command based on a specific field, you can use the kubectl get pods command with the --sort-by flag:
$ kubectl get pods --sort-by=<expression>
Run in Warp
Where:
- expression is a JSONPath expression.
For example, this command will display the list of all Pods sorted by their names in ascending order:
$ kubectl get pods --sort-by=.metadata.name
Run in Warp
And this command will display the list of all Pods sorted by their restart count:
$ kubectl get pods --sort-by=.status.containerStatuses[0].restartCount
Run in Warp
Note that the .status.containerStatuses[0].restartCount expression selects the restart count of the first container in the Pod.
Customizing the output of the kubectl get pods command
To customize the output columns of the kubectl get pods command, you can use the kubectl get pods command with the -o custom-columns flag:
$ kubectl get pods -o custom-columns=<custom_column_name>:<expression>
Run in Warp
- custom_column_name is the name you want to assign to a column.
- expression is a JSONPath expression.
For example:
$ kubectl get pods -o custom-columns=NAME:.metadata.name,QoS_CLASS:.status.qosClass
Run in Warp
Upon execution, the above command will output two columns, NAME populated with the values of metadata.name and QoS_CLASS populated with the values of status.qosClass.
Customizing the output using a template file
To customize the output columns of the kubectl get pods command using a template file (i.e. a predefined column configurations), you can use the -o custom-column-file flag:
$ kubectl get pods -o custom-columns-file=<template_file_path>
Run in Warp
Where:
- template_file_path is an absolute or relative file path for a template file.
For example:
$ kubectl get pods -o custom-columns-file=./myTemplate.txt
Run in Warp
Where the myTemplate.txt file contains:
NAME QoS_CLASS
metadata.name status.qosClass
Run in Warp
Upon execution, the above command will output two columns NAME populated with the values of metadata.name and QoS_CLASS populated with the values of status.qosClass.
Extracting Pod information
To output specific field values of Pods, you can use the kubectl get pods command with the -o flag (short for --output) combined with a JSONPath expression as follows:
$ kubectl get pods -o jsonpath=”<expression>”
Run in Warp
Where:
- expression is a JSONPath expression.
For example, this command will extract the image name of each container running your cluster’s Pods.
$ kubectl get pods -o jsonpath=”{.items[*].spec.containers[*].image}”
Run in Warp
Where:
- .items[*] indicates the iteration over each Pod.
- .spec specifies the Pod’s specification.
- .containers[*] marks the iteration over each container within the Pod.
- .image retrieves the image name for each container.
Written by
Mansi Manhas
Filed Under
Related Articles
Copy Files From Pod in Kubernetes
Learn how to copy files and directories from within a Kubernetes Pod into the local filesystem using the kubectl command.
Scale Deployments in Kubernetes
Learn how to manually and automatically scale a Deployment based on CPU usage in Kubernetes using the kubectl-scale and kubectl-autoscale commands.
Get Kubernetes Logs With kubectl
Learn how to get the logs of pods, containers, deployments, and services in Kubernetes using the kubectl command. Troubleshoot a cluster stuck in CrashloopBackoff, ImagePullBackoff, or Pending error states.
Forward Ports In Kubernetes
Learn how to forward the ports of Kubernetes resources such as Pods and Services using the kubectl port-forward command.
Tail Logs In Kubernetes
Learn how to tail and monitor Kubernetes logs efficiently to debug, trace, and troubleshoot errors more easily using the kubectl command.
Get Context In Kubernetes
Learn how to get information about one or more contexts in Kubernetes using the kubectl command.
Delete Kubernetes Namespaces With kubectl
Learn how to delete one or more namespaces and their related resources in a Kubernetes cluster using the kubectl command.
Get Kubernetes Secrets With kubectl
Learn how to list, describe, customize, sort and filter secrets in a Kubernetes cluster by name, type, namespace, label and more using the kubectl command.
List Kubernetes Namespaces With kubectl
Learn how to list, describe, customize, sort and filter namespaces in a Kubernetes cluster by name, label, and more using the kubectl command.
How To List Events With kubectl
Learn how to list and filter events in Kubernetes cluster by namespace, pod name and more using the kubectl command.
Kubernetes vs Docker: The Backbone of Modern Backend Technologies
Lean the fundamentals of the Kubernetes and Docker technologies and how they interplay with each other.
Set Context With kubectl
Learn how to create, modify, switch, and delete a context in Kubernetes using the kubectl config command.