How to list all the pods running in a particular worker node by executing a command from master?

17.2k Views Asked by At

I need to get the list of pods running in a worker node by executing a command from master node. I can achieve if i moved into the worker node and execute kubectl get pods -n ns. But i need to execute this from the master node and get pods in worker.

2

There are 2 best solutions below

2
On

Running kubectl get pods -n ns in a specific node does not give the pods running in that node, rather it will give all pods in namespace ns regardless of which nodes they run.kubectl get pods -n ns -o wide --field-selector spec.nodeName=<nodename> gives the pods in ns namespace deployed in a particular node. This command can be executed from any nodes or from a system which has access to the cluster.

kubectl get pods -n kube-system -o wide --field-selector spec.nodeName=kind-control-plane

To get pods from all namespaces running in a particular node use command

kubectl get pods -A -o wide --field-selector spec.nodeName=<nodename>

You can also use kubectl describe nodes nodename and check Non-terminated Pods section to view which pods are currently running in that particular node.

kubectl describe nodes kind-control-plane

PodCIDRs:                     10.244.0.0/24
Non-terminated Pods:          (9 in total)
  Namespace                   Name                                          CPU Requests  CPU Limits  Memory Requests  Memory Limits  AGE
  ---------                   ----                                          ------------  ----------  ---------------  -------------  ---
  kube-system                 coredns-6955765f44-ftkv6                      100m (5%)     0 (0%)      70Mi (3%)        170Mi (8%)     56m
  kube-system                 coredns-6955765f44-wgkbn                      100m (5%)     0 (0%)      70Mi (3%)        170Mi (8%)     56m
  kube-system                 etcd-kind-control-plane                       0 (0%)        0 (0%)      0 (0%)           0 (0%)         56m
  kube-system                 kindnet-248xd                                 100m (5%)     100m (5%)   50Mi (2%)        50Mi (2%)      56m
  kube-system                 kube-apiserver-kind-control-plane             250m (12%)    0 (0%)      0 (0%)           0 (0%)         56m
  kube-system                 kube-controller-manager-kind-control-plane    200m (10%)    0 (0%)      0 (0%)           0 (0%)         56m
  kube-system                 kube-proxy-n4ntb                              0 (0%)        0 (0%)      0 (0%)           0 (0%)         56m
  kube-system                 kube-scheduler-kind-control-plane             100m (5%)     0 (0%)      0 (0%)           0 (0%)         56m
  local-path-storage          local-path-provisioner-7745554f7f-wgnwm       0 (0%)        0 (0%)      0 (0%)           0 (0%)         56m
Allocated resources:
0
On

You can get pods running on specific node by using this command:

kubectl get pods --all-namespaces -o wide --field-selector spec.nodeName=<node>

This will list all pods from all namespaces but you can narrow it down for specific namespace.