Problem:

You have self-managed kubernetes cluster running. You want to know

  • CPU and Memory usage on Nodes.
  • CPU and Memory usage of pods running on these Nodes.
  • CPU and Memory usage of containers running in each pod.

Solution:

You can install metric server on the cluster which discovers all nodes on the cluster and queries each node’s kubelet for CPU and memory usage. It provides ‘kubectl top’ command to display Resource (CPU/Memory/Storage) usage. The top command allows you to see the resource consumption for nodes or pods or containers.

#Get CPU and Memory current usage of all Nodes
kubectl top nodes

#Get CPU and Memory Requests and Limits for Nodes
kubectl describe nodes

OR

kubectl describe nodes | grep 'Name:\|  cpu\|  memory'

#Get CPU and Memory current usage of pods in all Namespaces
kubectl top pods --all-namespaces

#Get CPU and Memory current usage of containers running in pods in all Namespaces
kubectl top pods --all-namespaces --containers=true

#Sort (descending order) current CPU usage of pods in all Namespaces 
kubectl top pods --all-namespaces | sort --key 2 -b | awk 'NR<2{print $0;next}{print $0| "sort --key 3 --numeric -b --reverse"}'

#Sort (descending order) current Memory usage of pods in all Namespaces 
kubectl top pods --all-namespaces | sort --key 2 -b | awk 'NR<2{print $0;next}{print $0| "sort --key 4 --numeric -b --reverse"}'

#Sort (descending order) current CPU usage of containers in pods in all Namespaces 
kubectl top pods --all-namespaces --containers=true | sort --key 4 -b | awk 'NR<1{print $0;next}{print $0| "sort --key 4 --numeric -b --reverse"}'

#Sort (descending order) current Memory usage of containers in pods in all Namespaces 
kubectl top pods --all-namespaces --containers=true | sort --key 5 -b | awk 'NR<1{print $0;next}{print $0| "sort --key 5 --numeric -b --reverse"}'