26

I use below command to sort the pods by age

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

It shows up pods in descending order. How can we select sorting order like ascending?

P Ekambaram
  • 15,499
  • 7
  • 34
  • 59
  • 1
    You can try something like this `kubectl get pods --sort-by=.metadata.creationTimestamp | sort -r` little hacky way. – mchawre Jul 31 '19 at 16:16
  • @mchawre my output looks like `18m Normal SuccessfulCreate ...`. It is likely that there are other units, too. If the output starts with `50s`, then `sort -r` will create undesired results. – guettli Apr 13 '23 at 07:09

6 Answers6

18

Not supported by kubectl or the kube-apiserver as of this writing (AFAIK), but a workaround would be:

$ kubectl get pods --sort-by=.metadata.creationTimestamp | tail -n +2 | tac

or if tac is not available (MacOS X):

$ kubectl get pods --sort-by=.metadata.creationTimestamp | tail -n +2 | tail -r

If you want the header:

$ echo 'NAME                                                              READY   STATUS             RESTARTS   AGE' | \
 kubectl get pods --sort-by=.metadata.creationTimestamp | tail -n +2 | tac

You might just have to adjust the tabs on the header accordingly. Or if you don't want to use tail -n +2 you can use --no-headers. For example:

$ kubectl get pods --sort-by=.metadata.creationTimestamp --no-headers | tac
Rico
  • 58,485
  • 12
  • 111
  • 141
11

It Is Quite EASY: Once you have used --no-headers option, the HEADER will not be part of output (ascending ordered-listing of pods) and you can simply reverse sort the outcome of the command.

Here's the complete command to get exactly what is expected:

kubectl get po --sort-by={metadata.creationTimestamp} --no-headers | tac

Amit Verma
  • 8,660
  • 8
  • 35
  • 40
7

Sorted kubectl output and awk provide the table view with a header. Installation of extra tools is not needed.

# kubectl get pods --sort-by=.status.startTime | awk 'NR == 1; NR > 1 {print $0 | "tac"}'

An approach with JSON processor offered by paulogrell works also but could require more effort: for some Linux distributions you'll need to download and compile jq from source code. As for the jq command line I'd suggest to add the "name" to the map parameters and sort by "timestamp":

# kubectl get pods -o json | jq '.items | group_by(.metadata.creationTimestamp) | map({"name": .[0].metadata.name, "timestamp": .[0].metadata.creationTimestamp, "count": length}) | sort_by(.timestamp)'
mebius99
  • 2,495
  • 1
  • 5
  • 9
1

I believe the Kubernetes API doesnt support this option yet, but as a workaround you can use a JSON processor (jq) to adjust its output.

Ascending

kubectl get pods -o json | jq '.items | group_by(.metadata.creationTimestamp) | map({"timestamp": .[0].metadata.creationTimestamp, "count": length}) | sort_by(.count)'

Descending

kubectl get pods -o json | jq '.items | group_by(.metadata.creationTimestamp) | map({"timestamp": .[0].metadata.creationTimestamp, "count": length}) | sort_by(.count) | reverse'

Hope this helps

paulogrell
  • 136
  • 1
  • 6
1

A simpler version that works on MacOS and retains arbitrary headers:

kubectl get node --sort-by=.metadata.creationTimestamp | { read -r headers; echo "$headers"; tail -r; }
pnovotnak
  • 4,341
  • 2
  • 27
  • 38
0

If you are looking for a way to find the latest pod, try:

kubectl get pod --selector='app=my-app-name' \
  --sort-by='.metadata.creationTimestamp' \
  -o=jsonpath='{.items[-1].metadata.name}'
kinjelom
  • 6,105
  • 3
  • 35
  • 61