kubernetes list all running pods name
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Kubernetes (K8s) is an open-source platform for automating the deployment, scaling, and management of containerized applications. Understanding how to list running pods and their respective names is a fundamental operation when managing and troubleshooting Kubernetes resources.
What Are Pods in Kubernetes?
In Kubernetes, a Pod is the smallest and simplest unit in the Kubernetes object model. A Pod represents a set of running containers on your cluster. Each Pod is intended to run a single instance of a given application. You can think of Pods as a single application-specific "logical host" that contains one or more containers that are tightly coupled.
At a basic level, when you want to know the current state of your deployed applications, one of the first steps is to list all running Pods in your Kubernetes cluster.
Listing Pods with kubectl
kubectl is the command-line tool used for Kubernetes cluster operations. It interacts with Kubernetes, allowing you to manage your cluster with various commands. To list all running Pods, you'll primarily use the kubectl get pods command.
Basic Command
To list all Pods in the default namespace:
This command will return a list of all Pods in the default namespace, showing the Pod names, their current status, the number of container restarts, and their age.
Namespace Considerations
Kubernetes supports the creation of multiple isolated namespaces. To list Pods in a specific namespace, use the -n or --namespace option:
Wide Output for More Details
For more detailed information, including the node each Pod is assigned to, you can use the -o wide option:
This will add extra columns like NODE and IP to the output, which can be helpful when diagnosing issues.
Filtering by Labels
Pods can be filtered by their labels. For instance, if your Pods are labeled with app=myapp, you can list them using:
Custom Column Output
You can customize the output format to focus on specific fields using the -o custom-columns option:
This command will display only the names, namespaces, and status of the Pods.
Parsing Output with JSON and JSONPath
For scripting purposes, you might want to get the Pods' names in a machine-readable format using -o json or -o jsonpath.
The above command will output the detailed JSON data.
To extract only the Pod names from the JSON data, use jsonpath:
Example: Formatting Output
By using a multi-line format output, you might extract specific details concisely:
Summary Table of Commands
| Command | Description |
kubectl get pods | List all Pods in the default namespace |
kubectl get pods -n <namespace-name> | List all Pods in a specific namespace |
kubectl get pods -o wide | List Pods with additional details such as node and IP |
kubectl get pods -l app=myapp | List Pods filtered by a specific label |
kubectl get pods -o custom-columns=... | Output custom columns focusing on specific fields |
kubectl get pods -o json | Output the details of Pods in JSON format |
kubectl get pods -o jsonpath='{.items[*].metadata.name}' | Output only the Pod names using JSONPath syntax |
kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' | List Pod names in a multi-line format |
Advanced Topics
Understanding Pod Status
When listing Pods, you might encounter different statuses such as Running, Pending, or Failed. Understanding what these mean is crucial for maintaining cluster health.
- Running: At least one container in the Pod is in the
Runningstate. - Pending: Pod is not yet in the scheduled state, usually due to resource constraints.
- Failed: All containers in the Pod have terminated, with at least one container in a
Failedstate.
Troubleshooting Pods
To troubleshoot Pods, examine logs or describe a Pod in more detail:
These commands provide insight into the events that have occurred within the Pod, helping identify issues.
Enhancing Security
In a multi-tenant environment, restrict access to namespaces and limit the permissions users have using Role-Based Access Control (RBAC).
Conclusion
Effectively listing and managing Pods is fundamental to operating in a Kubernetes environment. With the versatile kubectl tool, you can retrieve and format Pod information to suit your needs, debugging applications efficiently and maintaining robust operations across your Kubernetes ecosystem. Understanding these commands enables a more secure, navigable, and optimized cluster management experience.
Related reading
- Kubernetes Liveness Probe Logging
- Kubernetes livenessProbe restarting vs destroying of the pod
- kubernetes local cluster create pods got errors like ‘ErrImagePull’ and ‘ImagePullBackOff’
- Kubernetes log location in pod
- Kubernetes log, User systemserviceaccountdefaultdefault cannot get services in the namespace
- Kubernetes logging level --v
- Kubernetes Logs - How to get logs for kube-system pods
- kubernetes lost /.kube/config

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.