Kubernetes: list a node's all pods and pod statuses

Viewed 758

I want to list a node's pods and pod statues, eg.

Node A
Pod1 Status
Pod2 Status

Node B
Pod1 Status
Pod2 Status

Is there a kubectl command I can use for this?

3 Answers
kubectl get pods

will give you almost what you want, but it has no Node information, that is why you would need -o wide (I doubt you really want the -A parameter here); then you need a bit of awk. So may be like this:

kubectl get pods -o wide  | awk '{print $1"  "$3"  "$7}

This script gives exactly the output you want:

kubectl get --no-headers pods --all-namespaces -o wide > /tmp/allpods

while read node; do
    echo "${node/node\//}"
    grep " ${node/node\//} " /tmp/allpods | \
        while read line; do
            set -- $line
            echo "$2 $4"
        done
done < <(kubectl get nodes --no-headers --output=name)

But this version has a more readable output in my opinion:

  • first column: k8s node name
  • second column: namespace/pod-name
  • third column: pod status
kubectl get --no-headers pods --all-namespaces -o wide > /tmp/allpods

while read node; do
    echo "${node/node\//}"
    while read line; do
        set -- $line
        echo " $2 $4"
    done < <(grep " ${node/node\//} " /tmp/allpods)
done < <(kubectl get nodes --no-headers --output=name) | column -t -s ' '

Output:

k8s-node01
            my-namespace01/mypod01-321-86d58674d8-kv222         Completed
            my-namespace01/mypod01-321-redis-55dc88454c-z6xfj   Running
            [...]
k8s-node02
            [...]

Doing an API call for each cluster node, as the accepted answer suggests, makes the script execution very long in a production environment with dozens of nodes and thousands of pods. That's why I opted for a solution where the result of a single call is saved to a temporary file.

Try this:
kubectl get pods -A --field-selector spec.nodeName=<node name> | awk '{print $2" "$4}'

Related