Get Pods descending sorted from age

Hello I want to call the API to get the latest pods. I have a label filter to get only the specific pod I want to, but the first pod that will shown up is the oldest one. How can I get the youngest one (latest)?

In Short:

How to get the latest instantiated pod from the API in Kubernetes?

I’m not sure about just returning the latest one, but you should be able to get their timestamp and comparing yourself. There are 2 things you can use .metadata.creationTimestamp or .status.startTime.

Can see this and sort via kubectl:

  • kubectl get pods --sort-by=.metadata.creationTimestamp
  • kubectl get pods --sort-by=.status.startTime

Thanks @mrbobbytables

I searched for an answer to sort them descending to get only the latest one. But I think I sort them ascending and take the latest one. Thanks

kubectl get po --sort-by=.metadata.creationTimestamp -n <> | tac

This also works for sorting replicasets!

kubectl get replicasets -o wide --sort-by=.metadata.creationTimestamp

Ascending order for all namespace

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

Descending order :

kubectl get pods --all-namespaces --sort-by=.metadata.creationTimestamp | tac