How to order pods based on the nodes in kubernetes

7/16/2018

I have a kubernetes cluster working perfectly fine. It has 5 worker nodes. I am using below command to get the status of pods.

kubectl get pod -o wide --namespace=machines

which shows below results

NAME                  READY     STATUS              RESTARTS   AGE       IP        NODE
deployment-26hfn      0/4       ContainerCreating   0          5m        <none>    machine003
deployment-782mk      0/4       Pending             0          5m        <none>    machine001
deployment-7kcc7      0/4       Pending             0          5m        <none>    machine002
deployment-8fzqs      0/4       ContainerCreating   0          5m        <none>    machine004
deployment-zfzts      0/4       ContainerCreating   0          5m        <none>    machine005

As you can see, the above result is not in order from machine001 to machine 005. Is it possible to print the output like below:

NAME                  READY     STATUS              RESTARTS   AGE       IP        NODE
deployment-26hfn      0/4       Pending   0          5m        <none>    machine001
deployment-782mk      0/4       Pending             0          5m        <none>    machine002
deployment-7kcc7      0/4       ContainerCreating   0          5m        <none>    machine003
deployment-8fzqs      0/4       ContainerCreating   0          5m        <none>    machine004
deployment-zfzts      0/4       ContainerCreating   0          5m        <none>    machine005
-- S Andrew
kubernetes

2 Answers

4/21/2020

I have created an extension KUBEBSORT to simplify the sorting process. Have a try.

-- AATHITH RAJENDRAN
Source: StackOverflow

7/16/2018

You can pipe the kubectl command output to sort:

kubectl get pods -o wide --namespace=machines | sort -k7

or to omit the first line

kubectl get pods -o wide --namespace=machines | sed -n '1!p' | sort -k7

Also, you should be able to do this by --sort-by option in kubectl:

kubectl get pods --all-namespaces -o wide --sort-by=.spec.nodeName
-- Crou
Source: StackOverflow