Sub topic 3
Constraining pods with nodeSelector
This chapter assumes you can write and apply pod manifests with kubectl, and that you know how labels and label selectors work. The previous chapter covered node labels. Here you use them to control where a pod runs.
What nodeSelector does
nodeSelector is a field in the pod spec. It holds a map of label keys and values. The scheduler considers only nodes that carry every one of those labels with exactly those values. This is a hard requirement. If no node matches, the pod stays Pending.
It is the simplest placement control Kubernetes offers. It supports only exact equality, and every entry must match (logical AND). It cannot express "either of these values", "not this value", or "prefer this but accept others". Node affinity, covered in later chapters, handles those cases.
Worked example
First, label a node and check the result.
kubectl label nodes worker-1 disktype=ssd
kubectl get nodes -L disktype
kubectl get nodes --show-labelsThe -L disktype flag adds a column showing each node's value for that label. --show-labels lists every label on every node.
Next, create a pod that requires that label.
apiVersion: v1
kind: Pod
metadata:
name: nginx-ssd
spec:
nodeSelector:
disktype: ssd
containers:
- name: nginx
image: nginx:1.27Apply it with kubectl apply -f. Then run kubectl get pod nginx-ssd -o wide. The NODE column should show worker-1, or any other node you labeled disktype=ssd.
How the scheduler applies it
The check happens in the filtering phase. Nodes that fail are removed before scoring, so a nodeSelector never affects which matching node wins. It only decides which nodes are eligible.
Common errors
- A typo in the key or value.
disktype: ssdanddiskType: ssdare different labels. Label keys and values are case-sensitive. - Forgetting to label any node. The manifest is valid, so the API server accepts it. The pod then waits in
Pending. - Quoting numbers and booleans. Label values are strings. In YAML, write
gpu: "true"andcount: "2". An unquotedtrueor2is parsed as a boolean or integer, and the API server rejects the manifest. - Assuming the selector moves running pods. The scheduler evaluates
nodeSelectoronly when it places a pod. Removing the label from a node later does not evict pods already running there. - Labeling the wrong node.
kubectl labelon an existing key fails unless you add--overwrite. That protects you from silently changing a value, so use the flag only when you mean to.
Checking and removing labels
To list only the nodes a selector would match, use a label selector on kubectl get.
kubectl get nodes -l disktype=ssdIf a pod is Pending and this returns no nodes, you have found the cause. To remove a label, add a trailing dash to the key.
kubectl label nodes worker-1 disktype-When to move beyond nodeSelector
Use nodeSelector when one or two exact labels are enough and the requirement is strict. Switch to node affinity when you need a set of allowed values, exclusions, numeric comparisons, or soft preferences.
Exercise
A pod has nodeSelector with disktype: ssd and zone: a. Only one node has disktype=ssd, and that node has zone=b. What happens to the pod, and which command shows why?
Show answer
The pod stays Pending, because a node must have every label in the selector and no node has both. Run kubectl describe pod <name> and read the FailedScheduling event in the Events section. Compare it with kubectl get nodes --show-labels.
Exercise
You remove the label disktype=ssd from worker-1 while the pod nginx-ssd is running there. Does the pod move?
Show answer
No. The scheduler checks nodeSelector only at scheduling time, so the running pod stays on worker-1. If the pod is deleted and recreated, it will be Pending unless another node has the label.