Kubernetes
Pods
Running Pods
Command Line
DevOps

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.

Practice system design

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:

bash
kubectl get pods

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:

bash
kubectl get pods -n <namespace-name>

Wide Output for More Details

For more detailed information, including the node each Pod is assigned to, you can use the -o wide option:

bash
kubectl get pods -o wide

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:

bash
kubectl get pods -l app=myapp

Custom Column Output

You can customize the output format to focus on specific fields using the -o custom-columns option:

bash
kubectl get pods -o custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace,STATUS:.status.phase

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.

bash
kubectl get pods -o json

The above command will output the detailed JSON data.

To extract only the Pod names from the JSON data, use jsonpath:

bash
kubectl get pods -o jsonpath='{.items[*].metadata.name}'

Example: Formatting Output

By using a multi-line format output, you might extract specific details concisely:

bash
kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'

Summary Table of Commands

CommandDescription
kubectl get podsList all Pods in the default namespace
kubectl get pods -n <namespace-name>List all Pods in a specific namespace
kubectl get pods -o wideList Pods with additional details such as node and IP
kubectl get pods -l app=myappList Pods filtered by a specific label
kubectl get pods -o custom-columns=...Output custom columns focusing on specific fields
kubectl get pods -o jsonOutput the details of Pods in JSON format
kubectl get pods -o jsonpath='&#123;.items[*].metadata.name&#125;'Output only the Pod names using JSONPath syntax
kubectl get pods -o jsonpath='&#123;range .items[*]&#125;&#123;.metadata.name&#125;&#123;"\n"&#125;&#123;end&#125;'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 Running state.
  • 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 Failed state.

Troubleshooting Pods

To troubleshoot Pods, examine logs or describe a Pod in more detail:

bash
kubectl logs <pod-name>
kubectl describe pod <pod-name>

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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.