Fran
Fran

Reputation: 4272

Kubernetes API - Get Pods on Specific Nodes

Reading the Kubernets documentation it looks to be possible to select a certain range of pods based on labels. 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 get pods \
--output=wide
--namespace=$NS \
--server=$SERVER | head

#=>

NAME   READY     STATUS             RESTARTS   AGE       NODE

Can any of these headers be used as selector? If yes, how to do it with kubectl? How to do it with the API?

Upvotes: 275

Views: 286729

Answers (6)

Camil
Camil

Reputation: 8456

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

Upvotes: 150

Matt Hamann
Matt Hamann

Reputation: 1668

kubectl describe node $NODE

will show all of the non-terminated pods running on $NODE.

Upvotes: 31

Kristofer
Kristofer

Reputation: 8624

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>

Upvotes: 507

Breedly
Breedly

Reputation: 14326

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.

Upvotes: 7

Mal
Mal

Reputation: 354

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

kubectl get pods -o wide --all-namespaces | grep <YOUR-NODE>

Upvotes: 31

coreypobrien
coreypobrien

Reputation: 2051

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

Upvotes: 13

Related Questions