Kubernetes API - gets Pods on specific nodes

8/30/2016

Looking at http://kubernetes.io/docs/user-guide/labels/#selecting-sets-of-nodes it looks to be possible to select a certain range of pods based on labels. But in my case I want to select all the pods on one node but I don't want to label each pod on their corresponding node.

Am I missing something from the documentation or is it just not possible to select by node? If I do:

kubectl --server="<SERVER>" --namespace=<NS> get pods -o wide | head
    NAME   READY     STATUS             RESTARTS   AGE       NODE

Can any of these header be used as selector? If yes how to do it with kubectl bust most importantly, how to do it with the API?

Thanks in advance

-- Regnoult
kubectl
kubernetes

6 Answers

7/24/2019

I've gone through the same process with the Go Client and it uncovers a few shortcuts the CLI is taking.

func doNodesHavePods(clientset *kubernetes.Clientset) error {
    nodeLabelSelector := "nodelabel=interesting_nodes"
    nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{LabelSelector: nodeLabelSelector})

    if err != nil {
        return err
    }

    nodeNames := []string{}
    for _, node := range nodes.Items {
        nodeNames = append(nodeNames, node.Name)
    }
    // --all-namespaces -> listing and looping on namespaces
    namespaces, err := clientset.CoreV1().Namespaces().List(metav1.ListOptions{})

    if err != nil {
        return err
    }
    for _, namespace := range namespaces.Items {
        for _, name := range nodeNames {
            // pods need a namespace to be listed.
            pods, err := clientset.CoreV1().Pods(namespace.Name).List(metav1.ListOptions{FieldSelector: "spec.nodeName=" + name})
            if err != nil {
                println("%v", err)
            }
            for _, pod := range pods.Items {
                fmt.Println(pod.Namespace, pod.Name)
            }
        }
    }
    return nil
}

I've started to find that a lot of the questions I need to ask are becoming too complex for the CLI which is a great workhorse, but learning to use the Go Client can help you get the first answer you're looking for, but also dig deeper into questions that those answers raise.

-- Breedly
Source: StackOverflow

6/12/2018

As mentioned in the accepted answer the PR is now merged and you can get pods by node as follows:

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

8/30/2016

Example sorting pods by nodeName:

kubectl get pods -o wide --sort-by="{.spec.nodeName}"

Example of getting pods on nodes using label filter:

for n in $(kubectl get nodes -l your_label_key=your_label_value --no-headers | cut -d " " -f1); do 
    kubectl get pods --all-namespaces  --no-headers --field-selector spec.nodeName=${n} 
done

or by number of restarts

kubectl get pods --sort-by="{.status.containerStatuses[:1].restartCount}"

Example filtering by nodeName using --template flag:

$ kubectl get nodes

NAME                         STATUS                     AGE
ip-10-0-90-30.ec2.internal   Ready                      2d
ip-10-0-90-35.ec2.internal   Ready                      2d
ip-10-0-90-50.ec2.internal   Ready,SchedulingDisabled   2d
ip-10-0-91-60.ec2.internal   Ready                      2d
ip-10-0-91-65.ec2.internal   Ready                      2d


$kubectl get pods --template '{{range .items}}{{if eq .spec.nodeName "ip-10-0-90-30.ec2.internal"}}{{.metadata.name}}{{"\n"}}{{end}}}{{end}}'

filebeat-pezch
app-5xole
node-exporter-6kfs8
prometheus-0
sso-359976856-wu8zt
-- Camil
Source: StackOverflow

1/26/2018

You also can query for all pods an a node with the following command

kubectl get pods -o wide --all-namespaces | grep <YOUR-NODE>
-- Mal
Source: StackOverflow

6/2/2018

kubectl describe node <node> will show all of the non-terminated pods running on that node

-- Matt Hamann
Source: StackOverflow

8/5/2017

What you want is supported in the Kubernetes API server-side like this:

curl --cacert ca.crt --cert apiserver.crt --key apiserver.key  https://<server>:<port>/api/v1/namespaces/<namespace>/pods?fieldSelector=spec.nodeName%3Dsomenodename

However that field selector option is not built into kubectl yet: https://github.com/kubernetes/kubernetes/pull/50140

-- coreypobrien
Source: StackOverflow