<p>How do I get top three most CPU utilized pod in a Kubernetes cluster?</p>

<pre><code>kubectl top po --all-namespaces 
</code></pre>

<p>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?</p>

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

<pre><code>kubectl top po --all-namespaces --sort-by="cpu"
</code></pre>

<p>Output:</p>

<pre><code>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
</code></pre>

Naseem Khan
<p>There are different way you can achieve this,
examples below</p>
<ol>
<li><code>--sort-by + head</code></li>
<li><code>--sort-by + awk</code></li>
<li><code>sort</code></li>
</ol>
<p>Followings are the all nodes just <code>--sort-by=cpu</code></p>
<pre><code>$ 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   
</code></pre>
<p><strong>1. <code>--sort-by + awk</code></strong></p>
<pre><code>$ kubectl top pod --all-namespaces --sort-by=cpu | awk 'FNR &lt;= 4'
NAMESPACE     NAME                                   CPU(cores)   MEMORY(bytes)   
default       rabbit                                 972m         1Mi             
default       lion                                   900m         1Mi             
kube-system   kube-apiserver-controlplane            42m          286Mi  
</code></pre>
<p><strong>2. <code>--sort-by + head</code></strong></p>
<pre><code>$ 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 
</code></pre>
<p><strong>3. <code>sort</code></strong></p>
<pre><code>$ 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  
</code></pre>
<p><strong>Note:</strong> If you don't want to print the headers just put an option  <code>--no-headers</code></p>
