kubectl top

2 min read

If we have the metrics-server installed on our kubernetes cluster, we can use kubectl to see the amount of resources out pods are using and how much loaded are our worker nodes

To check the amount of resources currently in use for each node we can use kubectl top nodes:

$ kubectl top nodes
NAME                        CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
canterbury.pet2cattle.com   254m         2%     1729Mi          5%
scopuli.pet2cattle.com      1047m        26%    1203Mi          15%
anubis.pet2cattle.com       154m         1%     2729Mi          9%

We can also get the CPU and memory each pod is currently using by running kubectl top pod. This command is namespaced, so we can either specify the namespace we are interested in:

$ kubectl top pod -n jenkins
NAMESPACE     NAME                                        CPU(cores)   MEMORY(bytes)   
jenkins       jenkins-0                                   1m           685Mi    

Or use the -A flag for getting the pod usage for all the namespaces:

$ kubectl top pod -A
NAMESPACE     NAME                                        CPU(cores)   MEMORY(bytes)   
jenkins       jenkins-0                                   1m           685Mi           
kube-system   coredns-f9fd979d6-g2bp8                     3m           12Mi            
kube-system   etcd-minikube                               30m          40Mi            
kube-system   ingress-nginx-controller-558664778f-t64dl   4m           189Mi           
kube-system   kube-apiserver-minikube                     85m          337Mi           
kube-system   kube-controller-manager-minikube            25m          55Mi            
kube-system   kube-proxy-6khw8                            0m           21Mi            
kube-system   kube-scheduler-minikube                     2m           19Mi            
kube-system   metrics-server-d9b576748-57k42              0m           15Mi            
kube-system   storage-provisioner                         1m           14Mi            

kubectl top can also sort it's output by CPU usage:

$ kubectl top pod -A --sort-by=cpu
NAMESPACE     NAME                                        CPU(cores)   MEMORY(bytes)   
kube-system   kube-apiserver-minikube                     90m          337Mi           
kube-system   etcd-minikube                               29m          40Mi            
kube-system   kube-controller-manager-minikube            26m          55Mi            
kube-system   ingress-nginx-controller-558664778f-t64dl   5m           189Mi           
kube-system   coredns-f9fd979d6-g2bp8                     3m           12Mi            
kube-system   kube-scheduler-minikube                     2m           19Mi            
kube-system   storage-provisioner                         2m           15Mi            
jenkins       jenkins-0                                   1m           685Mi           
kube-system   kube-proxy-6khw8                            0m           21Mi            
kube-system   metrics-server-d9b576748-57k42              0m           15Mi       

Or per memory usage using --sort-by=memory:

$ kubectl top pod -A --sort-by=memory
NAMESPACE     NAME                                        CPU(cores)   MEMORY(bytes)   
jenkins       jenkins-0                                   1m           685Mi           
kube-system   kube-apiserver-minikube                     90m          337Mi           
kube-system   ingress-nginx-controller-558664778f-t64dl   5m           189Mi           
kube-system   kube-controller-manager-minikube            26m          55Mi            
kube-system   etcd-minikube                               29m          40Mi            
kube-system   kube-proxy-6khw8                            0m           21Mi            
kube-system   kube-scheduler-minikube                     2m           19Mi            
kube-system   metrics-server-d9b576748-57k42              0m           15Mi            
kube-system   storage-provisioner                         2m           15Mi            
kube-system   coredns-f9fd979d6-g2bp8                     3m           12Mi     

Posted on 4/1/2021