Kubernetes - display current pods vs. capacity with kubectl
Asked Answered
E

3

6

Kubernetes dashboard is able to show "current running pods / pods capacity" per node. But when I try to get the same info with kubectl I have to run two commands:

kubectl describe node | grep -E (^Name:|^Non-terminated)

which lists "current running pod on node", and

kubectl get nodes -o=custom-columns=NAME:.metadata.name,CAPACITY:.status.capacity.pods

which shows the node's capacity

Does anyone know how I can get the output similar to below using one command only?

NAME     CURRENT   CAPACITY
node_1   7         15
node_2   8         15
node_3   15        15

Thanks in advance!

Enscroll answered 2/10, 2019 at 12:47 Comment(0)
D
9

There is no one command for this.

It is possible to write script to do that with combining those two commands.

Note that using integer based metrics like number of pods can be very misleading as pods can vary in how much space and cpus they consume. You might use up cpus and memory before you reach node pod count capacity.

You can check available resources with command: kubectl top nodes

Node capacity

The capacity of the node (number of cpus and amount of memory) is part of the node object. Normally, nodes register themselves and report their capacity when creating the node object. If you are doing manual node administration, then you need to set node capacity when adding a node.

The Kubernetes scheduler ensures that there are enough resources for all the pods on a node. It checks that the sum of the requests of containers on the node is no greater than the node capacity. It includes all containers started by the kubelet, but not containers started directly by the container runtime nor any process running outside of the containers.

P.S.

On Debian the first command had to be slightly modified to work:

kubectl describe node | grep -E "(^Name:|^Non-terminated)"
Delija answered 4/10, 2019 at 16:0 Comment(1)
I know I can write a script but I wanted to avoid this. Thanks for pointing the difference between max pod number and available resources.Enscroll
W
13

I find the following helpful to see the number of pods running per node

kubectl get pods \
  -A \
  -o jsonpath='{range .items[?(@.spec.nodeName)]}{.spec.nodeName}{"\n"}{end}' \
  | sort | uniq -c | sort -rn

which produces an output like this

  89 node1
  88 node4
  87 node2
  87 node3
  86 node5
Wiggs answered 18/10, 2022 at 20:4 Comment(0)
D
9

There is no one command for this.

It is possible to write script to do that with combining those two commands.

Note that using integer based metrics like number of pods can be very misleading as pods can vary in how much space and cpus they consume. You might use up cpus and memory before you reach node pod count capacity.

You can check available resources with command: kubectl top nodes

Node capacity

The capacity of the node (number of cpus and amount of memory) is part of the node object. Normally, nodes register themselves and report their capacity when creating the node object. If you are doing manual node administration, then you need to set node capacity when adding a node.

The Kubernetes scheduler ensures that there are enough resources for all the pods on a node. It checks that the sum of the requests of containers on the node is no greater than the node capacity. It includes all containers started by the kubelet, but not containers started directly by the container runtime nor any process running outside of the containers.

P.S.

On Debian the first command had to be slightly modified to work:

kubectl describe node | grep -E "(^Name:|^Non-terminated)"
Delija answered 4/10, 2019 at 16:0 Comment(1)
I know I can write a script but I wanted to avoid this. Thanks for pointing the difference between max pod number and available resources.Enscroll
O
0

Adapting @anapsix's command to include some headers using AWK

kubectl get pods \
  -A \
  -o jsonpath='{range .items[?(@.spec.nodeName)]}{.spec.nodeName}{"\n"}{end}' \
  | sort | uniq -c | sort -rn \
  | awk 'BEGIN {print "Count\tNodeName"} {print $1 "\t" $2}'
Obmutescence answered 16/8 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.