155

I can sort my Kubernetes pods by name using:

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

How can I sort them (or other resoures) by age using kubectl?

Nikhil
  • 110
  • 4
Eugene Platonov
  • 3,145
  • 3
  • 26
  • 20

7 Answers7

230

Pods have status, which you can use to find out startTime.

I guess something like kubectl get po --sort-by=.status.startTime should work.

You could also try:

  1. kubectl get po --sort-by='{.firstTimestamp}'.
  2. kubectl get pods --sort-by=.metadata.creationTimestamp Thanks @chris

Also apparently in Kubernetes 1.7 release, sort-by is broken.

https://github.com/kubernetes/kubectl/issues/43

Here's the bug report : https://github.com/kubernetes/kubernetes/issues/48602

Here's the PR: https://github.com/kubernetes/kubernetes/pull/48659/files

vjdhama
  • 4,878
  • 5
  • 33
  • 47
  • @vjdhama is there a way to get a full list of things which can be sorted by in this way? – Matt Aug 17 '17 at 21:29
  • What do you mean by full list of things? You mean all types of resources. – vjdhama Aug 17 '17 at 21:32
  • 27
    Is there a way to reverse sort? So, for example, you can do a watch kubectl and get the newest pods at the top? – Joe J Jun 14 '18 at 16:29
  • This does not seem to work? `error: couldn't find any field with path "{.status.startTime}" in the list of objects` - because the pods are still pending perhaps? – Chris Stryczynski Jul 20 '18 at 09:36
  • 2
    This only works if I also include the `-o json` or `-o wide` flags. Tested on 1.7.x and 1.9.x – s g Dec 04 '18 at 20:43
  • 7
    @JoeJ The kubectl docs don't have a reverse order but you can do that with the tail command. `kubectl get pods --sort-by=.metadata.creationTimestamp | tail -r | head -n 1` I used `tail -r` to revert the output. Then `head -n 1` to print the newest pod – mowzy May 24 '19 at 18:31
  • The `.metadata.creationTimestamp` is a better choice as it includes the pending containers which naturally have zero `.status.startTime` and would therefore appear at the top of the list, not the bottom. – Matthew Wilcoxson Oct 02 '19 at 14:32
  • @Matthew, you may refer following URL for additional details (hope you already did, but it may help others): https://kubernetes.io/docs/reference/kubectl/overview/#output-options – Amit Verma Apr 17 '20 at 08:43
  • That's what I'm looking for. Thank you! – Mayur Jul 10 '20 at 12:50
50
kubectl get pods --sort-by=.metadata.creationTimestamp
Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
  • I'm getting `error: unknown type *api.Pod, expected unstructured in map[reflect.Type]*printers.handlerEntry{}` response – s g Dec 04 '18 at 20:37
  • This is useful for sorting the **ConfigMaps**: `kubectl get cm --sort-by=.metadata.creationTimestamp` – Ivan Aracki Aug 28 '19 at 09:17
8

If you are trying to get the most recently created pod you can do the following

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

Note the -1: gets the last item in the list, then we return the pod name

Antony Denyer
  • 1,541
  • 1
  • 15
  • 31
3

If you want to sort them in reverse order based on the age:

kubectl get po --sort-by=.metadata.creationTimestamp -n <<namespace>> | tac
imriss
  • 1,815
  • 4
  • 31
  • 46
1

This new command (since Kubernetes 1.23) worked perfectly for me:

kubectl alpha events

In contrast, neither of these worked for me (got unsorted events):

  • kubectl get events --sort-by='.lastTimestamp'
  • kubectl get events --sort-by=.metadata.creationTimestamp

I also saw reports that the above failed due to missing event properties: https://github.com/kubernetes/kubernetes/issues/29838#issuecomment-991070746

Ohad Schneider
  • 36,600
  • 15
  • 168
  • 198
0

If you want just the name of most-recently-created pod;

POD_NAME=$(kubectl get pod --sort-by=.metadata.creationTimestamp -o name | cut -d/ -f2 | tail -n 1)
echo "${POD_NAME}"
Steve Cooper
  • 20,542
  • 15
  • 71
  • 88
0

I wanted to see all pods that were updated in the past 24 hours. This worked perfectly well and doesn't rely on a particular version of Kubernetes or Kubernetes advanced parameters besides get pods:

kubectl get pods | awk '{print $1 " : " $5}' | grep -E ':\s([1-9]|[12][0-4])h$' | sort -k3,3

james-see
  • 12,210
  • 6
  • 40
  • 47