Get the Node Resource usage in Kubernetes – no plugin

Resources like CPU, Memory, Disk are critical for the performance of the node and the pods(containers) running on it.  You can read a detailed post about these resources here. In this post, we will cover the commands for printing the resources in a short and meaningful format.  A concise yet meaningful format is helpful for making clear decisions.

By default, you can check the status of the resource by describing the node. Sure, this command works fine but this also prints tons of additional info which may not always be required, specially if you are automating things. Here are the few commands, that could help in summarizing the resource stats in the cluster.  

Command-1: Print the node’s allocatable resources.

This command would print the actual resource status. Note that this does not signify anything related to the request and the limit. The below command output shows the allocatable resources currently present in the system. As the request does not necessarily mean a reduction in the allocatable resources.

(echo "nodename|allocatable/total-cpu|allocatable/total-mem|allocatable/total-emp-storage" && kubectl get node -o jsonpath='{range .items[*]}{.metadata.name} |{.status.allocatable.cpu}/{.status.capacity.cpu}| {.status.allocatable.memory}/{.status.capacity.memory}| {.status.allocatable.ephemeral-storage}/{.status.capacity.ephemeral-storage}|{"\n"}{end}' ) |column -t -s'|' 
nodename allocatable/total-cpu allocatable/total-mem allocatable/total-emp-storage
kube-master 3800m/4 3398936Ki/4025624Ki 75610150378/82042264Ki
kube-worker-1 1900m/2 1666184Ki/2030728Ki 37803678044/41019616Ki
kube-worker-2 1900m/2 1775748Ki/2140292Ki 47315651711/51340768Ki

Command-2: Get the resource stats per node

kubectl describe node |perl -0777 -wnE '@pods = /Name:\s+([^ ]+)\n.*?Allocated resources:.*?overcommitted..\n(.*?)(?=Events:)/gs;say for @pods'
kube-controller-1
Resource Requests Limits
-------- ------ ------
cpu 920m (51%) 300m (16%)
memory 221286400 (6%) 856515840 (24%)
ephemeral-storage 0 (0%) 0 (0%)
hugepages-2Mi 0 (0%) 0 (0%)

kube-worker-1
Resource Requests Limits
-------- -------- ------
cpu 285m (15%) 1300m (68%)
memory 202954752 (11%) 946693376 (55%)
ephemeral-storage 0 (0%) 0 (0%)
hugepages-2Mi 0 (0%) 0 (0%)

kube-worker-2
Resource Requests Limits
-------- -------- ------
cpu 375m (19%) 300m (15%)
memory 242800640 (14%) 856515840 (50%)
ephemeral-storage 0 (0%) 0 (0%)
hugepages-2Mi 0 (0%) 0 (0%)

kube-worker-3
Resource Requests Limits
-------- -------- ------
cpu 305m (16%) 1300m (68%)
memory 233400320 (13%) 934257920 (54%)
ephemeral-storage 0 (0%) 0 (0%)
hugepages-2Mi 0 (0%) 0 (0%)

Command-3: Get the Resource stats per Pod

kubectl describe  node | perl -0777 -wnE '@pods = /Name:\s+([^ ]+)\n.*?Non-terminated\s+Pods:\s+\([0-9]+\s+in\s+total\)\n(.*?)\nAllocated resources:/gs;say for @pods'
kube-controller-1
  Namespace                   Name                                         CPU Requests  CPU Limits  Memory Requests  Memory Limits  Age
  ---------                   ----                                         ------------  ----------  ---------------  -------------  ---
  kube-system                 calico-node-jwfnw                            150m (8%)     300m (16%)  64M (1%)         500M (14%)     5d21h
  kube-system                 coredns-76b4fb4578-hkx2t                     100m (5%)     0 (0%)      70Mi (2%)        170Mi (5%)     4d23h
  kube-system                 dns-autoscaler-7979fb6659-4g6gq              20m (1%)      0 (0%)      10Mi (0%)        0 (0%)         5d21h
  kube-system                 kube-apiserver-kube-controller-1             250m (13%)    0 (0%)      0 (0%)           0 (0%)         5d21h
  kube-system                 kube-controller-manager-kube-controller-1    200m (11%)    0 (0%)      0 (0%)           0 (0%)         5d21h
  kube-system                 kube-proxy-b57mk                             0 (0%)        0 (0%)      0 (0%)           0 (0%)         5d21h
  kube-system                 kube-scheduler-kube-controller-1             100m (5%)     0 (0%)      0 (0%)           0 (0%)         5d21h
  kube-system                 nodelocaldns-9xz58                           100m (5%)     0 (0%)      70Mi (2%)        170Mi (5%)     5d21h
kube-worker-1
  Namespace                   Name                                          CPU Requests  CPU Limits  Memory Requests  Memory Limits  Age
  ---------                   ----                                          ------------  ----------  ---------------  -------------  ---
  cert-manager                cert-manager-76578c9687-7v65c                 0 (0%)        0 (0%)      0 (0%)           0 (0%)         5d1h
  default                     simple-app                                    0 (0%)        0 (0%)      0 (0%)           0 (0%)         4d20h
  kube-system                 calico-node-zfdvf                             150m (7%)     300m (15%)  64M (3%)         500M (29%)     5d21h
  kube-system                 kube-proxy-wm74d                              0 (0%)        0 (0%)      0 (0%)           0 (0%)         5d21h
  kube-system                 nginx-proxy-kube-worker-1                     25m (1%)      0 (0%)      32M (1%)         0 (0%)         5d21h
  kube-system                 nodelocaldns-xqbnx                            100m (5%)     0 (0%)      70Mi (4%)        170Mi (10%)    5d21h
  kubesec                     kubesec-webhook-6f767b46d9-zwwd7              10m (0%)      1 (52%)     32Mi (1%)        256Mi (15%)    5d20h
kube-worker-2
  Namespace                   Name                                          CPU Requests  CPU Limits  Memory Requests  Memory Limits  Age
  ---------                   ----                                          ------------  ----------  ---------------  -------------  ---
  cert-manager                cert-manager-cainjector-5c55bb7cb4-thm7r      0 (0%)        0 (0%)      0 (0%)           0 (0%)         5d1h
  kube-system                 calico-node-hgwwc                             150m (7%)     300m (15%)  64M (3%)         500M (29%)     5d21h
  kube-system                 coredns-76b4fb4578-c4n7p                      100m (5%)     0 (0%)      70Mi (4%)        170Mi (10%)    4d23h
  kube-system                 kube-proxy-7t696                              0 (0%)        0 (0%)      0 (0%)           0 (0%)         5d21h
  kube-system                 nginx-proxy-kube-worker-2                     25m (1%)      0 (0%)      32M (1%)         0 (0%)         5d21h
  kube-system                 nodelocaldns-m747r                            100m (5%)     0 (0%)      70Mi (4%)        170Mi (10%)    5d21h
kube-worker-3
  Namespace                   Name                                          CPU Requests  CPU Limits  Memory Requests  Memory Limits  Age
  ---------                   ----                                          ------------  ----------  ---------------  -------------  ---
  cert-manager                cert-manager-webhook-556f979d7f-4z96l         0 (0%)        0 (0%)      0 (0%)           0 (0%)         5d1h
  default                     ubuntu                                        0 (0%)        0 (0%)      0 (0%)           0 (0%)         4d22h
  kube-system                 calico-kube-controllers-6dd874f784-qxbtn      30m (1%)      1 (52%)     64M (3%)         256M (15%)     5d21h
  kube-system                 calico-node-2qd4m                             150m (7%)     300m (15%)  64M (3%)         500M (29%)     5d21h
  kube-system                 kube-proxy-nsltd                              0 (0%)        0 (0%)      0 (0%)           0 (0%)         5d21h
  kube-system                 nginx-proxy-kube-worker-3                     25m (1%)      0 (0%)      32M (1%)         0 (0%)         5d21h
  kube-system                 nodelocaldns-jv469                            100m (5%)     0 (0%)      70Mi (4%)        170Mi (10%)    5d21h
 
0 0 votes
Please, Rate this post
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Scroll to Top