Issue : --sort-by is not working properly

I am facing issue with --sort-by command while using it with top command.

kubectl top pod --all-namespaces --sort-by=cpu
NAMESPACE NAME CPU(cores) MEMORY(bytes)
kube-system coredns-5644d7b6d9-mcl6l 2m 17Mi
jenkins-ns my-jenkins 1m 764Mi
kube-system coredns-5644d7b6d9-46fb4 2m 23Mi
kube-system etcd-kubernetesmaster.test.com 8m 293Mi
kube-system weave-net-8kknz 2m 103Mi
kube-system weave-net-wq9jj 1m 81Mi
kube-system kube-controller-manager-kubernetesmaster.test.com 6m 95Mi
kube-system weave-net-pv5l7 1m 98Mi
kube-system kube-apiserver-kubernetesmaster.test.com 15m 292Mi
kube-system metrics-server-b94bb7c77-2sjwg 1m 15Mi
kube-system weave-net-qmmwh 3m 104Mi
kube-system kube-proxy-2frfn 2m 14Mi
kube-system kube-proxy-rhbr8 1m 23Mi
kube-system kube-scheduler-kubernetesmaster.test.com 2m 16Mi
kube-system kube-proxy-5wx65 1m 15Mi
kube-system kube-proxy-jbt69 2m 22Mi
jenkins-ns mynginx-8668b9977f-cdsh6 0m 1Mi

kubectl top pod --all-namespaces --sort-by=memory
NAMESPACE NAME CPU(cores) MEMORY(bytes)
kube-system kube-controller-manager-kubernetesmaster.test.com 6m 95Mi
kube-system weave-net-8kknz 1m 103Mi
jenkins-ns my-jenkins 1m 764Mi
kube-system weave-net-pv5l7 1m 98Mi
kube-system etcd-kubernetesmaster.test.com 9m 294Mi
kube-system weave-net-wq9jj 1m 81Mi
kube-system metrics-server-b94bb7c77-2sjwg 1m 14Mi
kube-system kube-apiserver-kubernetesmaster.test.com 14m 292Mi
kube-system coredns-5644d7b6d9-mcl6l 2m 17Mi
kube-system kube-proxy-jbt69 2m 22Mi
kube-system coredns-5644d7b6d9-46fb4 2m 23Mi
kube-system weave-net-qmmwh 1m 104Mi
kube-system kube-proxy-2frfn 2m 14Mi
kube-system kube-proxy-rhbr8 1m 23Mi
kube-system kube-scheduler-kubernetesmaster.test.com 1m 16Mi
kube-system kube-proxy-5wx65 1m 15Mi
jenkins-ns mynginx-8668b9977f-cdsh6 0m 1Mi

kubectl version --short
Client Version: v1.16.0
Server Version: v1.16.0
Same isue with version v1.15.2 as well.

1 Like

Looks to be a known issue that’s being worked on.

https://github.com/kubernetes/kubernetes/issues/81270

1 Like

Any work around or alternate command to achieve same ?

Found below mentioned workaround in one of forum.
You could do kubectl top pod | sort -k3 -n to sort by CPU, or add -r at the end if you want the largest at the top.