Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List `kubectl top pods` filtered by node

Tags:

kubernetes

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.

like image 865
Vojtěch Avatar asked Aug 13 '19 07:08

Vojtěch


People also ask

How do I get pods for specific nodes?

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.

What does kubectl top nodes show?

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.

How do you use kubectl top command?

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.


Video Answer


2 Answers

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 information
  • grep <node>: let you filter pod located on a specific node
  • awk {'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>

like image 100
Titou Avatar answered Sep 21 '22 02:09

Titou


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
like image 34
user3010591 Avatar answered Sep 22 '22 02:09

user3010591