1. Overview

In this tutorial, we’ll learn how to sort the Kubernetes pods by their age. Sorting pods by age helps debug and troubleshoot issues in Kubernetes. We’ll be using the kubectl command to achieve this.

2. Using the –sort-by Option

The kubectl get command is used to list resources in Kubernetes.

The easiest way to sort the pods by age is by using the –sort-by flag along with the kubectl get pods command:

$ kubectl get pods --sort-by=.metadata.creationTimestamp
NAME                     READY   STATUS    RESTARTS   AGE
ubuntu-pod-1             2/2     Running   0          26m
ubuntu-pod-2             2/2     Running   0          13m7s
ubuntu-pod-3             2/2     Running   0          5m17s

The above command lists the pods in ascending order based on the pod creation timestamp, which effectively sorts them by age, with the oldest pods appearing first in the list.

Notably, we’re using creationTimestamp for sorting. Hence, the recently created pod will have the highest creationTimestamp value and the least age.

2.1. Sort in Descending Order

Let’s sort the pods in descending order, i.e., with the newest pods appearing first:

$ kubectl get pods --sort-by=.metadata.creationTimestamp --no-headers | tail -r
ubuntu-pod-3             2/2     Running   0          5m17s
ubuntu-pod-2             2/2     Running   0          13m7s
ubuntu-pod-1             2/2     Running   0          26m

Here, we ignore the headers by passing the –no-headers flag to the kubectl get pods command. Furthermore, we used the -r option for the tail command to reverse the order of lines in the output.

We can also achieve this using the tac command:

$ kubectl get pods --sort-by=.metadata.creationTimestamp --no-headers | tac

As discussed earlier, the first part of the command, kubectl get pods –sort-by=.metadata.creationTimestamp, lists the pods in the ascending order. The | (pipe) character is then used to take the output of the previous command and pass it as input to the tac command. tac is the reverse of the cat and is used to reverse the order of lines in the output.

3. Fetch the Oldest and Most Recently Created Pods

So far, we have discussed different approaches to fetch pod lists in ascending/descending order of their creation time. Let’s now find only the most recently created pod instead of the entire list of pods.

We can achieve this by adding JsonPath filtering:

$ kubectl get pods --sort-by=.metadata.creationTimestamp -o=jsonpath='{.items[-1].metadata.name}'

Here, we filter the last item from the pod name list using the -1 index.

Similarly, we can get the oldest pod using index 0:

$ kubectl get pods --sort-by=.metadata.creationTimestamp -o=jsonpath='{.items[0].metadata.name}'

This helps us when we have many pods running, but we wish to get only a few of the recent/oldest pods.

4. Conclusion

In this article, we learned how to fetch Kubernetes pod details in the ascending and descending orders of their creation time. Further, we discussed getting the oldest pod and the most recent pod using JsonPath filtering.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.