Lesson 30 · Section 3: Scheduling
Lesson 30: Node selectors: send a Pod to the nodes you choose
How nodeSelector matches a Pod to node labels, how to label nodes and read the labels they already have, what the scheduler does when nothing matches, and where nodeSelector stops and node affinity or taints take over. · 15 min read · 4 min video
Official competency this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Why node selectors exist
Left alone, the scheduler considers every node with enough free capacity a fair choice. That is what you want most of the time. But nodes are rarely identical. Some have SSDs and some have slow disks, one has a GPU, a few are in another zone, and one pool runs Windows. The scheduler has no way to guess that your database needs the fast disk or that your training job is useless without the GPU.
A node selector is the simplest way to tell it. You describe nodes with labels, and the Pod lists the labels a node must have. You met labels and selectors two lessons ago, where a Service used them to find Pods. Here the same idea points the other way: a Pod uses them to find nodes.
What it is
Parts:
- You (kubectl label node)
- node-2 (disktype=ssd)
- Pod web (nodeSelector set)
- kube-scheduler (label = selector?)
- Binding (web → node-2)
Connections:
- You to node-2: label
- Pod web to kube-scheduler: pending
- kube-scheduler to node-2: reads labels
- kube-scheduler to Binding: match
A node selector is always two things, set in two different places:
| Part | Where it lives | How you set it |
|---|---|---|
| The label | On the Node object, under metadata.labels | kubectl label node node-2 disktype=ssd |
| The selector | In the Pod, under spec.nodeSelector | Written in the manifest, before the Pod is created |
The field itself is a plain map of strings, at the same level as containers:
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
nodeSelector:
disktype: ssd
containers:
- name: web
image: nginx:1.29-alpine
There are no operators and no nesting: each line is key: value, and it means "the node has this label with this
value".
For anything that creates Pods from a template (a Deployment, ReplicaSet, DaemonSet, StatefulSet or Job), the field
goes in the Pod template, spec.template.spec.nodeSelector, because that is where the Pod spec is:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web # which Pods belong to this Deployment
template:
metadata:
labels:
app: web
spec:
nodeSelector:
disktype: ssd # which nodes those Pods may run on
containers:
- name: web
image: nginx:1.29-alpine
How it works
Parts:
- Pod web (wants disktype=ssd)
- kube-scheduler (compares labels)
- node-1 (disktype=hdd)
- node-2 (disktype=ssd)
- node-3 (no disktype label)
Connections:
- Pod web to kube-scheduler: pending
- kube-scheduler to node-1: no
- kube-scheduler to node-2: match
- kube-scheduler to node-3: no
The node selector is a filter in the scheduler, the first of the two phases you saw in the kube-scheduler lesson. For each Pending Pod the scheduler goes through the nodes and asks a yes or no question: does this node carry all the labels the Pod lists? A node that does not is removed from the list, however empty it is. The nodes that remain then go through the other filters (resources, taints) and are scored as usual.
Three consequences follow from "it is a filter":
- It is a hard rule. There is no "try to". If no node matches, the Pod stays
Pendingwith aFailedSchedulingevent. It does not fall back to another node. - It is checked once. The scheduler places a Pod and never looks again. If you remove the label from the node later, the Pod keeps running there. Only a new or recreated Pod is checked against the new labels.
- It still needs the other filters to pass. A matching node that is full, cordoned or tainted is still rejected. The selector narrows the choice; it does not override anything.
Several keys: all of them must match
Parts:
- Pod db (ssd and zone=east)
- kube-scheduler (needs both labels)
- node-1 (ssd, zone=west)
- node-2 (ssd, zone=east)
- node-3 (hdd, zone=east)
Connections:
- Pod db to kube-scheduler: pending
- kube-scheduler to node-1: 1 of 2
- kube-scheduler to node-2: 2 of 2
- kube-scheduler to node-3: 1 of 2
You can list more than one label. The entries are joined with AND: a node must have every one.
spec:
nodeSelector:
disktype: ssd
topology.kubernetes.io/zone: east
A node with the SSD in zone west fails, and so does a node in zone east with a slow disk. There is no way to
write OR in a node selector.
Under the hood · How the scheduler implements it
In the scheduler, nodeSelector is handled by the same filter plugin as node affinity, called NodeAffinity. That
is why the event for a Pod that fits nowhere reads "didn't match Pod's node affinity/selector", even when the Pod
uses only a node selector.
If a Pod sets both nodeSelector and required node affinity, a node must satisfy both.
The plugin compares the Pod's map with the labels stored on the Node object in the API server. It does not ask the node anything. That also explains why labelling a node is enough to unblock a Pending Pod: the change to the Node object is an event that makes the scheduler try the waiting Pods again.
There is one more place a node selector can come from. With the PodNodeSelector admission plugin enabled (it is
not enabled by default), a namespace can carry an annotation that adds a node selector to every Pod created in it.
If you find a nodeSelector on a Pod that nobody wrote, look at the namespace's annotations.
Labels on nodes
You can only select labels that exist, so half of the work is knowing what the nodes carry.
Labels every node already has
The kubelet and the cloud provider put a set of well-known labels on each node. You can use them in a node selector without labelling anything:
| Label | Example value | Useful for |
|---|---|---|
kubernetes.io/hostname | node-2 | Pinning a Pod to one specific node |
kubernetes.io/os | linux, windows | Mixed Linux and Windows clusters |
kubernetes.io/arch | amd64, arm64 | Images built for one CPU architecture |
topology.kubernetes.io/zone | eu-west-1a | Keeping a Pod in one zone (set on cloud clusters) |
topology.kubernetes.io/region | eu-west-1 | Keeping a Pod in one region (set on cloud clusters) |
node.kubernetes.io/instance-type | m5.large | Targeting a machine size (set on cloud clusters) |
node-role.kubernetes.io/control-plane | empty | Targeting control plane nodes (set by kubeadm) |
Your own labels
kubectl label node node-2 disktype=ssd # add
kubectl label node node-2 disktype=nvme --overwrite # change: needs --overwrite
kubectl label node node-2 disktype- # remove: the key followed by a minus
kubectl label node node-1 node-2 disktype=ssd # several nodes at once
And to read them:
kubectl get nodes --show-labels # every label, in one long column
kubectl get nodes -L disktype # one extra column with the value of that label
kubectl get nodes -l disktype=ssd # only the nodes that would match the selector
kubectl describe node node-2 | grep -A 10 Labels
The third command is the important one: kubectl get nodes -l uses the same matching as the scheduler. If it returns
no nodes for the labels in your selector, the Pod will be Pending.
Under the hood · Labels a node cannot give itself
A kubelet is allowed to set some labels on its own Node object. On a shared cluster that is a risk: a compromised
node could label itself pci=true and attract sensitive workloads.
For that reason the NodeRestriction admission plugin, which kubeadm enables, stops kubelets from setting or
changing any label with the prefix node-restriction.kubernetes.io/. When a node selector is used for isolation
and not just for convenience, use a key under that prefix, for example
example.com.node-restriction.kubernetes.io/pci=true. Only an administrator can set it.
What a node selector cannot do
Parts:
- Pod train (nodeSelector: gpu)
- Pod web (no nodeSelector)
- node-gpu (gpu=true)
- node-1 (no gpu label)
Connections:
- Pod train to node-gpu: must
- Pod web to node-1: may
- Pod web to node-gpu: may too
Node selectors are deliberately simple, and it helps to know exactly where they stop:
| You want | Node selector? | Use |
|---|---|---|
"Only nodes with disktype=ssd" | Yes | nodeSelector |
"ssd and zone east" | Yes | nodeSelector with two entries |
"ssd or nvme" | No | Node affinity, operator In |
"Anything except hdd", "nodes that have no such label" | No | Node affinity, operators NotIn, DoesNotExist |
| "Prefer SSD, but run anywhere if there is none" | No | Node affinity, the preferred... form |
| "Keep other Pods off this node" | No | A taint on the node, with a toleration on the Pods allowed there |
| "Exactly this one node, whatever its state" | Not quite | nodeName, which skips the scheduler |
Node affinity is the next lesson. It can express everything a node selector can and more, with a longer syntax, so
nodeSelector stays the right tool whenever a plain "must have these labels" is enough.
nodeName and nodeSelector look similar and behave very differently:
nodeSelector | nodeName | |
|---|---|---|
| Who places the Pod | The scheduler | Nobody: the Pod already has its node |
| Taints, cordons, free resources | Checked | Not checked by the scheduler |
| Can match several nodes | Yes, any node with the labels | No, one node by name |
| If the node does not qualify | Pod stays Pending with an event | The Pod is sent there anyway |
To pin a Pod to one node and keep the scheduler's checks, select the node's hostname label:
nodeSelector: {kubernetes.io/hostname: node-2}.
Using it
Example · Run a Pod on the SSD node
The task: label node-2 with disktype=ssd and run a Pod web that can only be scheduled on nodes with that
label.
kubectl label node node-2 disktype=ssd
kubectl get nodes -l disktype=ssd # node-2 is listed: the label is there
kubectl run web --image=nginx:1.29-alpine --dry-run=client -o yaml > web.yaml
kubectl run has no flag for a node selector, so add the two lines to the file by hand:
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
nodeSelector:
disktype: ssd
containers:
- name: web
image: nginx:1.29-alpine
kubectl apply -f web.yaml
kubectl get pod web -o wide # NODE is node-2
Example · Fix a Pod that is Pending because of its selector
The Pod web has been Pending for a minute. Read the event:
kubectl describe pod web | tail -n 5
Warning FailedScheduling default-scheduler 0/3 nodes are available: 1 node(s) had untolerated taint
{node-role.kubernetes.io/control-plane: }, 2 node(s) didn't match Pod's node affinity/selector.
Two nodes were rejected for their labels. Compare what the Pod asks for with what the nodes have:
kubectl get pod web -o jsonpath='{.spec.nodeSelector}{"\n"}' # {"disktype":"sdd"}
kubectl get nodes -L disktype # node-2 has disktype=ssd
Here the selector says sdd, a typo. There are two ways out, and the task decides which one is right:
-
The Pod is wrong. A Pod's
nodeSelectorcannot be edited, so recreate the Pod with the corrected value:kubectl get pod web -o yaml > web.yaml # fix the value in the file kubectl replace --force -f web.yaml # deletes and creates -
The node is missing the label. Add it, and do nothing else. The scheduler retries the waiting Pod by itself:
kubectl label node node-2 disktype=ssd kubectl get pod web -o wide -w
Example · Move a Deployment to labelled nodes
The Deployment web already runs three replicas, and they must move to nodes labelled disktype=ssd. Edit the Pod
template, with kubectl edit deployment web or with one patch:
kubectl patch deployment web --type merge \
-p '{"spec":{"template":{"spec":{"nodeSelector":{"disktype":"ssd"}}}}}'
kubectl rollout status deployment web
kubectl get pods -l app=web -o wide # every replica is on a labelled node
Changing the template starts a rolling update: new Pods are created with the selector and scheduled onto matching nodes, and the old Pods are removed. If no node matches, the new Pods stay Pending and the rollout stalls while the old Pods keep serving, so check the rollout before you move on.
To remove the selector again, set the key to null in the patch:
kubectl patch deployment web --type merge \
-p '{"spec":{"template":{"spec":{"nodeSelector":null}}}}'
Example · Run a Pod on the control plane node
On a kubeadm cluster the control plane node has the label node-role.kubernetes.io/control-plane with an empty
value, and a NoSchedule taint with the same key. A node selector alone is not enough: the label gets the Pod past
one filter, and the taint stops it at the next. You need the selector and a toleration:
apiVersion: v1
kind: Pod
metadata:
name: cp-tools
spec:
nodeSelector:
node-role.kubernetes.io/control-plane: ""
tolerations:
- key: node-role.kubernetes.io/control-plane
operator: Exists
effect: NoSchedule
containers:
- name: tools
image: busybox:1.37
command: ["sleep", "3600"]
The empty string matters. The label's value is empty, so the selector's value must be empty too; "true" would not
match.
Tricks
Common mistakes
- Writing the selector and forgetting the label. The Pod is accepted and then sits in
Pending. Label the node first, or check withkubectl get nodes -l. - A typo in the key or the value.
disktype: sddordiskType: ssdmatches nothing. Keys and values are case sensitive and compared exactly. - Putting
nodeSelectorat the wrong level. In a Pod it is underspec, next tocontainers, not inside a container. In a Deployment it is underspec.template.spec, not underspecand not underspec.selector. - Expecting it to reserve the node. Other Pods can still be scheduled onto a labelled node. Use a taint to keep them out.
- Trying to add it to a running Pod.
kubectl edit podrejects the change. Recreate the Pod, or change the template of the Deployment that owns it. - Removing a label to move Pods away. Running Pods are not rechecked, so nothing moves. Delete the Pods (their controller recreates them elsewhere) or drain the node.
- Forgetting the taint. A selector for a tainted node, such as the control plane, also needs a toleration.
Recap
spec.nodeSelectoris a map of labels; a node must carry every entry with exactly the same value.- It takes two steps:
kubectl label node <name> key=value, then the selector in the Pod spec or Pod template. - It is a hard filter, checked once: no match means
Pending, and removing the label later moves nothing. - It attracts Pods to nodes but does not keep other Pods away; that is what taints are for.
- For OR, NOT or "prefer", use node affinity.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Practise: try it on a real cluster
2 hands-on labs for this lesson. You get goals, not steps: any solution that reaches them passes.
Describe three nodes with labels, pin a workload to the nodes with fast disks, and get a pod running whose node selector matches no node.
Build · Level 1 · warm-up · target 12 min
A worker was reset by mistake and is no longer a node. Join it again with kubeadm, and give the new Node object what the old one had so that the waiting workload runs on it.
Fix · Level 3 · challenge · target 20 min
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.