2
votes

How do I get top three most CPU utilized pod in a Kubernetes cluster?

kubectl top po --all-namespaces 

Above command gives me CPU and memory utilization for all the pods across all namespaces. How to restrict it to only top three most CPU utilized pods?

Also, I've tried to sort by CPU, but seems like sorting is not working.

kubectl top po --all-namespaces --sort-by="cpu"

Output:

NAMESPACE     NAME                             CPU(cores)   MEMORY(bytes)
kube-system   weave-net-ksfp4                  1m           51Mi
kube-system   kube-controller-manager-master   10m          50Mi
kube-system   coredns-5644d7b6d9-rzd64         2m           6Mi
kube-system   weave-net-h4xlg                  1m           77Mi
kube-system   kube-proxy-lk9xv                 1m           19Mi
kube-system   coredns-5644d7b6d9-v2v4m         3m           6Mi
kube-system   kube-scheduler-master            2m           21Mi
kube-system   kube-apiserver-master            26m          228Mi
kube-system   kube-proxy-lrzjh                 1m           9Mi
kube-system   etcd-master                      12m          38Mi
kube-system   metrics-server-d58c94f4d-gkmql   1m           14Mi
4

4 Answers

1
votes

The sorting should be fixed in the next release - https://github.com/kubernetes/kubernetes/issues/81270

In the mean time you can use this

kubectl top pod --all-namespaces --no-headers \
 | sort --key 3 --numeric \
 | tail -3
1
votes

You can use this command:

kubectl top pods --all-namespaces  --sort-by=cpu | sort --reverse --key 3 --numeric | head -n 3

The head command will provide you with the top 3 pods.

0
votes

Kubectl top is an intentionally simplistic tool and not a good replacement for things like Prometheus and Grafana. Maybe check those out instead?

0
votes

There are different way you can achieve this, examples below

  1. --sort-by + head
  2. --sort-by + awk
  3. sort

Followings are the all nodes just --sort-by=cpu

$ kubectl top pod --all-namespaces --sort-by=cpu
NAMESPACE     NAME                                   CPU(cores)   MEMORY(bytes)   
default       rabbit                                 969m         1Mi             
default       lion                                   912m         1Mi             
kube-system   kube-apiserver-controlplane            42m          286Mi           
kube-system   etcd-controlplane                      14m          36Mi            
kube-system   kube-controller-manager-controlplane   12m          44Mi            
kube-system   kube-scheduler-controlplane            4m           16Mi            
kube-system   coredns-f9fd979d6-9x8vb                2m           8Mi   

1. --sort-by + awk

$ kubectl top pod --all-namespaces --sort-by=cpu | awk 'FNR <= 4'
NAMESPACE     NAME                                   CPU(cores)   MEMORY(bytes)   
default       rabbit                                 972m         1Mi             
default       lion                                   900m         1Mi             
kube-system   kube-apiserver-controlplane            42m          286Mi  

2. --sort-by + head

$ kubectl top pod --all-namespaces --sort-by=cpu | head -n 4
NAMESPACE     NAME                                   CPU(cores)   MEMORY(bytes)   
default       rabbit                                 970m         1Mi             
default       lion                                   909m         1Mi             
kube-system   kube-apiserver-controlplane            43m          293Mi 

3. sort

$ kubectl top pod --all-namespaces | sort --reverse --numeric-sort --key 3 | head -n 4
default       rabbit                                 971m         1Mi             
default       lion                                   913m         1Mi             
kube-system   kube-apiserver-controlplane            41m          286Mi           
kube-system   etcd-controlplane                      15m          38Mi  

Note: If you don't want to print the headers just put an option --no-headers