1. Overview

In Kubernetes, it’s crucial to understand the current status of pods and their assigned nodes. This information is very useful for system monitoring, scalability, and troubleshooting. We can achieve this information by listing all pods and their nodes.

In this tutorial, we’ll learn to list pods and nodes in Kubernetes using the kubectl command.

2. List All Pods in Kubernetes

The command kubectl get pods lists all the pods in Kubernetes. For instance, to list all pods in the default namespace, we can use the following kubectl command:

$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-7b4f8f4c4f 1/1 Running 0 10m
nginx-deployment-7b4f8f4c5f 1/1 Running 0 10m
nginx-deployment-7b4f8f4c6f 1/1 Running 0 10m

This command will display a table with information about all pods. This includes the pod’s name, current status, and age. We can also filter the results by introducing different flags in the command. Notably, this command will not only list the running pods but all the pods present in the cluster. 

3. List Nodes in Kubernetes

In addition to listing all pods, it’s also essential to know the nodes they are running on. To obtain information about all nodes in a Kubernetes cluster, we can use the kubectl get nodes command. This command retrieves details about all nodes that are part of the Kubernetes cluster:

$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
node1 Ready control-plane,master 3d v1.22.2
node2 Ready <none> 3d v1.22.2
node3 Ready <none> 2d v1.22.2

The above output displays a table with information about all the nodes present in the cluster. It includes the node name, current status, age, and version of Kubernetes running on each node.

We can also use different flags with the kubectl get nodes command to filter the results or get more information about the nodes. For instance, we can use the -o wide flag to display additional information such as the internal IP address, external IP address, and the OS image for each node:

$ kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNAL-VERSION CONTAINER-RUNTIME
node1 Ready control-plane,master 3d v1.22.2 10.56.179.63
  <none> Amazon Linux 2 5.4.237-143.346.amzn2.x86_64 docker://20.10.17
node2 Ready <none> 3d v1.22.2 10.26.139.3
  <none> Amazon Linux 2 5.4.237-143.346.amzn2.x86_64 docker://20.10.17
node3 Ready <none> 2d v1.22.2 10.76.179.30
  <none> Amazon Linux 2 5.4.237-143.346.amzn2.x86_64 docker://20.10.17

The first four columns are the same as in the previous output. In addition to this, it displays a few additional columns like the internal and external IP address, OS image, kernel image, and container runtime for each node.

4. List Pods and Their Nodes

To get node information on which a particular pod is running, we can use the -o wide flag. This command displays additional information, including the node on which each pod is running. To illustrate, let’s list all pods in the default namespace with their corresponding node information, we can use the following command:

$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-7b4f8f4c4f 1/1 Running 0 10m 10.244.1.225 node2 <none> <none>
nginx-deployment-7b4f8f4c5f 1/1 Running 0 10m 10.244.2.245 node1 <none> <none>
nginx-deployment-7b4f8f4c6f 1/1 Running 0 10m 10.244.3.167 node3 <none> <none>

In this example, the command lists all pods in the default namespace. It displays their name, the number of containers in each pod that are ready, the current status of each pod, the number of times each pod has been restarted, how long each pod has been running, the IP address assigned to each pod, the node name on which each pod is running, and whether each pod has been nominated as the node to run on.

We can also use different flags with the kubectl get pods -o wide command to filter the results or get more detailed information about the pods and the nodes on which they are running.

For instance, we can use the –show-labels flag to display any labels associated with each pod:

$ kubectl get pods -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
nginx-deployment-7b4f8f4c4f 1/1 Running 0 10m 10.244.1.225 node2 <none> <none> app=myapp
nginx-deployment-7b4f8f4c5f 1/1 Running 0 10m 10.244.2.245 node1 <none> <none> app=myapp
nginx-deployment-7b4f8f4c6f 1/1 Running 0 10m 10.244.3.167 node3 <none> <none> app=myapp

This command will display a table with an additional column showing any labels associated with each pod.

5. Filtering Pods by Node

If we want to filter pods by the node on which they are running, we can use the –field-selector spec.nodeName=<node-name> option. This command retrieves details about the pods running on a specific node in the Kubernetes cluster.

For example, to list all pods running on node1, we can use the following command:

$ kubectl get pods --field-selector spec.nodeName=node1
NAME READY STATUS RESTARTS AGE
nginx-deployment-7b4f8f4c5f-4tbv4 1/1 Running 0 10m

This command will display a table with information about all the pods running on node1, including their name, status, age, and IP address.

We can also use different flags along with the above command to filter the results or get more information about the pods running on a specific node. For instance, we can use the -o wide flag to display additional information, such as the node name and namespace for each pod:

$ kubectl get pods --field-selector spec.nodeName=node1 -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-7b4f8f4c5f-4tbv4 1/1 Running 0 10m 10.234.4.156 node1 <none> <none>

This command will display a table with additional columns including the node name and its IP for each pod running on node1.

6. Conclusion

In this article, we discussed commands to retrieve information about all pods and their assigned nodes easily. We also filtered pods by the node where they were running. We identified any imbalances or issues in the Kubernetes cluster with these commands, allowing us to resolve them accordingly.

1 Comment
Oldest
Newest
Inline Feedbacks
View all comments
Comments are closed on this article!