Top three most CPU utilized pods in a kubernetes cluster

2/8/2020

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
-- Naseem Khan
kubernetes

3 Answers

2/8/2020

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

-- coderanger
Source: StackOverflow

2/8/2020

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
-- Matt
Source: StackOverflow

2/9/2020

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.

-- vinay_kumar
Source: StackOverflow