Is there a way to get top pods
filtered by node?
Use case: I have a node which is reported to use 103% of the cpu and I want to validate which pods are causing it.
You can add the nodeSelector field to your Pod specification and specify the node labels you want the target node to have. Kubernetes only schedules the Pod onto nodes that have each of the labels you specify. See Assign Pods to Nodes for more information.
A kubectl top is a command used to list all the running nodes and pods along with their resource utilization. It provides you a snapshot of resource utilization metrics like CPU, memory, and storage on each running node.
We execute the command “kubectl top pod –namespace default”. This command displays the metrics in the default namespace. Whenever we need to obtain the metric from any definite namespace, we need to identify the namespace: We observe that the various indicators are not occurring in large numbers.
I don't think there is direct way to do it with kubectl top pods
command since the only option to filter is label/selector which apply to pod only.
For your use case, you can use the command:
kubectl get pods -o wide | grep <node> | awk {'print $1'} | xargs -n1 command kubectl top pods --no-headers
kubectl get pods -o wide
: display pods with their associated node informationgrep <node>
: let you filter pod located on a specific nodeawk {'print $1'}
: print the first column (name of the pods)xargs -n1 command kubectl top pods --no-headers
: execute the top command for each pod without the headers (NAME, CPU, MEMORY)Additionally, you can check the limits you've set for each pod in one specific node using the command kubectl describe node <node>
For powershell users, just set your <node_name> to filter
kubectl get pods -o wide --all-namespaces | sls "<node_name>" | ForEach-Object {$row = ($_ -split "\s+"); $top = ("kubectl -n $($row[0]) top pod $($row[1]) --no-headers --containers" | iex); foreach($line in $top){$container=$line -split "\s+"; [pscustomobject]@{node=$nodeName; pod=$container[0];container=$container[1];cpu=$container[2];memory=$container[3]}} } | Sort-Object pod| ft -AutoSize
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With