How to sort Kubernetes objects using kubectl

3 min read

kubectl get has an option to be able to sort it's output by any numeric or string field using it's JSON path. Let's take a look on how to do so:

We can use the PersistentVolume objects as an example:

$ kubectl get pv
NAME         CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
audit-logs   2Gi        RWO            Retain           Available           manual                  122m
pv0002       1Gi        RWO            Retain           Available           manual                  122m
pv0003       3Gi        RWO            Retain           Available           manual                  122m

To be able to sort it's output we must take a look at it's yaml definition in order to get it's path. For a PersistentVolume we can see that this would be .spec.capacity.storage:

$ kubectl get pv pvc-13901610-49f0-4fcf-b674-a3bc1d5e378c -o yaml | grep -E "storage: [0-9]" -B4
spec:
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 8Gi

Executing the same command with the --sort-by option and this path we will get the volumes sorted by size:

$ kubectl get pv --sort-by=.spec.capacity.storage
NAME         CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pv0002       1Gi        RWO            Retain           Available           manual                  122m
audit-logs   2Gi        RWO            Retain           Available           manual                  122m
pv0003       3Gi        RWO            Retain           Available           manual                  122m

For sorting the pods by it's restart count it's a little more tricky:

$ kubectl get pods --sort-by='.status.containerStatuses[0].restartCount' -A
NAMESPACE     NAME                                        READY   STATUS      RESTARTS   AGE
kube-system   ingress-nginx-admission-create-hjck6        0/1     Completed   0          53m
jenkins       default-ffcjr                               0/1     Error       0          31h
kube-system   ingress-nginx-controller-558664778f-t64dl   1/1     Running     0          53m
kube-system   ingress-nginx-admission-patch-f5dc5         0/1     Completed   0          53m
jenkins       default-3x1fk                               0/1     Error       0          26h
jenkins       jenkins-0                                   2/2     Running     23         20d
kube-system   kube-scheduler-minikube                     1/1     Running     11         23d
kube-system   coredns-f9fd979d6-g2bp8                     1/1     Running     11         23d
kube-system   kube-proxy-6khw8                            1/1     Running     11         23d
kube-system   etcd-minikube                               1/1     Running     12         23d
kube-system   kube-apiserver-minikube                     1/1     Running     16         23d
kube-system   metrics-server-d9b576748-57k42              1/1     Running     16         23d
kube-system   kube-controller-manager-minikube            1/1     Running     18         23d
kube-system   storage-provisioner                         1/1     Running     30         23d

Right now, there's no way of sorting it in descending order, but tac always comes handy:

$ kubectl get pods --sort-by='.status.containerStatuses[0].restartCount' -A | tac
kube-system   storage-provisioner                         1/1     Running     30         23d
kube-system   kube-controller-manager-minikube            1/1     Running     18         23d
kube-system   metrics-server-d9b576748-57k42              1/1     Running     16         23d
kube-system   kube-apiserver-minikube                     1/1     Running     16         23d
kube-system   etcd-minikube                               1/1     Running     12         23d
kube-system   kube-proxy-6khw8                            1/1     Running     11         23d
kube-system   coredns-f9fd979d6-g2bp8                     1/1     Running     11         23d
kube-system   kube-scheduler-minikube                     1/1     Running     11         23d
jenkins       jenkins-0                                   2/2     Running     23         20d
jenkins       default-3x1fk                               0/1     Error       0          26h
kube-system   ingress-nginx-admission-patch-f5dc5         0/1     Completed   0          61m
kube-system   ingress-nginx-controller-558664778f-t64dl   1/1     Running     0          61m
jenkins       default-ffcjr                               0/1     Error       0          31h
kube-system   ingress-nginx-admission-create-hjck6        0/1     Completed   0          61m
NAMESPACE     NAME                                        READY   STATUS      RESTARTS   AGE

Posted on 31/12/2020

Categories