🐶
Kubernetes

List Running Pods in Kubernetes: A Quick Guide

By Jan on 01/22/2025

Learn how to efficiently list all running Kubernetes pods by name using simple and practical command-line techniques.

List Running Pods in Kubernetes: A Quick Guide

Table of Contents

Introduction

To list all running pod names in your Kubernetes cluster, use the command: kubectl get pods --template='{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'. This command retrieves all pods and uses a Go template to output only the pod names. If you want to see pod names along with their statuses and nodes, use the command: kubectl get pods -o wide. This provides a wider output with additional information about each pod.

Step-by-Step Guide

To list all running pod names in your Kubernetes cluster, use:

kubectl get pods --template='{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'

This command retrieves all pods and uses a Go template to output only the pod names.

If you want to see pod names along with their statuses and nodes, use:

kubectl get pods -o wide 

This provides a wider output with additional information about each pod.

Code Example

This code provides two examples of using kubectl to list Kubernetes pods. The first example uses a Go template to display only pod names. The second example uses the -o wide flag to display pod names along with their status, node, and other details.

This document provides code examples for listing running pod names in your Kubernetes cluster using kubectl.

1. List Pod Names Only:

kubectl get pods --template='{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'

This command uses a Go template to extract and display only the pod names.

Example Output:

pod-name-1
pod-name-2
pod-name-3

2. List Pod Names with Status and Node:

kubectl get pods -o wide

This command uses the -o wide flag to display a wider output, including the pod status and the node it's running on.

Example Output:

NAME                     READY   STATUS    RESTARTS   AGE   IP             NODE        NOMINATED NODE   READINESS GATES
pod-name-1              1/1     Running   0          5m    10.244.1.5     node-1       <none>           <none>
pod-name-2              1/1     Running   0          10m   10.244.2.6     node-2       <none>           <none>
pod-name-3              0/1     Pending   0          1m    <none>          <none>       <none>           <none>

These examples demonstrate two ways to list running pod names in your Kubernetes cluster. Choose the command that best suits your needs based on the level of detail required.

Additional Notes

  • Filtering Pods: You can further filter the output of these commands using various options like -n <namespace> to specify a namespace, -l <label> to filter by labels, or -f <filename> to use a YAML/JSON file for filtering.
  • Go Templates: Go templates provide a powerful way to customize the output of kubectl get commands. You can use them to extract specific information, format the output, or even perform calculations on the data.
  • Wide Output: The -o wide flag is useful for getting a quick overview of your pods, including their status, IP addresses, and the nodes they are running on.
  • Other Output Formats: Besides wide, kubectl supports various output formats like json, yaml, custom-columns, etc. These can be useful for scripting or integrating with other tools.
  • Alternatives: While kubectl is the primary tool for interacting with Kubernetes, there are other tools and GUIs available that can also list and manage pods.
  • Resource Management: Regularly monitoring your pods is crucial for ensuring the health and performance of your applications. Use these commands and other monitoring tools to keep track of your pods' status, resource usage, and any potential issues.
  • Security: Be mindful of the information displayed when using these commands, especially in shared environments. Avoid exposing sensitive data like pod IP addresses or internal service details.

Summary

Command Description Output
kubectl get pods --template='{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' Lists only the names of all running pods. List of pod names.
kubectl get pods -o wide Lists all pods with their statuses, nodes, and other information. Wider table format with additional pod details.

Conclusion

These examples demonstrate two ways to list running pod names in your Kubernetes cluster. Choose the command that best suits your needs based on the level of detail required. Understanding how to list and manage pods is fundamental for operating and monitoring your applications in a Kubernetes environment.

References

  • List All Container Images Running in a Cluster | Kubernetes List All Container Images Running in a Cluster | Kubernetes | This page shows how to use kubectl to list all of the Container images for Pods running in a cluster. Before you begin You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:
  • How To List Pods With kubectl | Warp How To List Pods With kubectl | Warp | Learn how to list and filter Kubernetes Pods by name, namespaces, labels, manifests, and more using the kubectl command.
  • Kubernetes: list all pods and its nodes - Stack Overflow Kubernetes: list all pods and its nodes - Stack Overflow | Feb 26, 2018 ... You can do that with custom columns: kubectl get pod -o=custom-columns=NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName --all- ...
  • kubectl Quick Reference | Kubernetes kubectl Quick Reference | Kubernetes | This page contains a list of commonly used kubectl commands and flags. Note:These instructions are for Kubernetes v1.32. To check the version, use the kubectl version command. Kubectl autocomplete BASH source <(kubectl completion bash) # set up autocomplete in bash into the current shell, bash-completion package should be installed first. echo "source <(kubectl completion bash)" >> ~/.bashrc # add autocomplete permanently to your bash shell. You can also use a shorthand alias for kubectl that also works with completion:
  • Restart all deployment in a namespace - Discuss Kubernetes Restart all deployment in a namespace - Discuss Kubernetes | Hi All, Is there a way I can restart all the deployments in a particular namespace. For example, I am using istio in my EKS cluster, once the upgrade happens I have to restart all the deployments in my application namespace to start use the new sidecars. They gave command like below but it is not working. kubectl rollout restart deployment --namespace apps But when I use this command, I get the below error. Is there a way I can do a rolling restart of my deployments.
  • kubectl - How do I list pod name/version across multiple clusters in ... kubectl - How do I list pod name/version across multiple clusters in ... | Jun 16, 2020 ... I have multiple clusters that I want to query and in each cluster I want to find and list the versions of the pods running in there.
  • How to check pod VPC and subnet - Discuss Kubernetes How to check pod VPC and subnet - Discuss Kubernetes | Hi All - I have pod running on my GKE cluster under nodes - Is there a command to find which VPC & Subnets the pods belongs to? instead of manual way?
  • How to list all containers in Kubernetes? - Server Fault How to list all containers in Kubernetes? - Server Fault | Sep 13, 2017 ... This will get all container with the namespace in a pretty format: kubectl get pods --all-namespaces ...
  • How to List All Pods and Its Nodes in Kubernetes | Baeldung on Ops How to List All Pods and Its Nodes in Kubernetes | Baeldung on Ops | Explore commands to retrieve information about all pods and their assigned nodes.

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait