get name of the pod consuming highest cpu using kubectl command

6/22/2021

I am trying to get the pod name with highest CPU utilization using kubectl command. Able to retrieve list using following command but unable to write a jsonpath query to fetch the name of first pod from the output. Appreciate any help in this regard. Thanks!

kubectl top pod POD_NAME --sort-by=cpu
-- haridurgempudi
kubectl
kubernetes

1 Answer

6/22/2021

kubectl top doesn't appear to enable --output formatting and so no JSON and thus no JSONPath :-(

You can:

kubectl top pod \
--sort-by=cpu \
--no-headers \
--namespace=${NAMESPACE} \
| head -n 1

I think it would be useful to support --output for all kubectl commands and you may wish to submit a feature request for this.

NOTE Hmmm kubectl top output format options

-- DazWilkin
Source: StackOverflow