Kubectl get last pod or output in descending order

Get the kubectl output in descending order

Use tail and tac command in Unix because these are common commands of Unix OS.

kubectl get pods --sort-by=.metadata.creationTimestamp --no-headers | tail -r

Note: Use no header option to get the output without header and use tail command of unix with -r option to reverse the output.

We can also use tac command for same process as shown below

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

Note: tac is the reverse of the cat and is used to reverse the order of lines in the output.

Get the output of Kubectl in ascending order

Sort the output in ascending order

kubectl get pods --sort-by=.metadata.creationTimestamp

Get the most recently created POD

Using JSONPATH filtering we will get the last created pods by using -1 in items.

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

Get the first one pod created or oldest pod

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

Leave a Reply