Sub topic 10
Placing Pods on nodes with nodeSelector
This chapter assumes you can read Pod manifests, know what a node is, and can run basic kubectl commands. It also assumes you know how labels and equality-based selectors work from earlier chapters.
Why constrain placement
By default, the scheduler picks any node with enough free resources. Sometimes a workload needs a specific kind of node. A database may need fast disks. A machine learning job may need a GPU. A latency-sensitive service may need to run in a particular zone.
Nodes are Kubernetes objects, so they carry labels like any other object. nodeSelector is the simplest way to match a Pod against those labels.
How nodeSelector works
nodeSelector is a field in the Pod spec. It holds a map of label keys and values. The scheduler only considers nodes that have every label in the map. The match is equality-based and uses AND logic. There is no in, notin, or OR here.
Step 1: Label a node
Look at the labels your nodes already have. Cloud providers and kubelets add some, such as kubernetes.io/hostname and kubernetes.io/os.
kubectl get nodes --show-labelsAdd your own label to a node:
kubectl label node worker-1 disktype=ssdReplace worker-1 with a node name from kubectl get nodes.
Step 2: Set nodeSelector in the Pod
apiVersion: v1
kind: Pod
metadata:
name: gpu-job
spec:
nodeSelector:
disktype: ssd
containers:
- name: app
image: nginx:1.27Apply it, then check where it landed:
kubectl apply -f gpu-job.yaml
kubectl get pod gpu-job -o wideThe NODE column should show a node that carries disktype=ssd.
Using it in a Deployment
In a Deployment, put nodeSelector under spec.template.spec, not at the top level of the Deployment spec. The top-level spec.selector selects Pods. It does not select nodes. Mixing these up is a common mistake.
Common errors
- Pod stuck in Pending. No node has all the required labels. Run
kubectl describe pod gpu-joband read the Events section. A message like0/3 nodes are available: 3 node(s) didn't match Pod's node affinity/selectorconfirms the cause. - Typo in the key or value. Label matching is exact and case-sensitive.
diskType=ssddoes not matchdisktype=ssd. - Label added after the Pod was scheduled.
nodeSelectoris checked only at scheduling time. Changing node labels later does not move a running Pod. - Unschedulable nodes. A matching node can still be rejected for lack of CPU or memory, or because of a taint the Pod does not tolerate.
Removing a node label
Remove a label by adding a minus sign after the key:
kubectl label node worker-1 disktype-New Pods that require disktype=ssd will no longer be placed on that node. Pods already running there keep running.
Exercise
A Deployment's Pod template sets nodeSelector: {disktype: ssd, zone: us-east-1a}. Only one node has disktype=ssd, and it is in us-east-1b. What happens to the Pods, and how do you confirm why?
Show answer
The Pods stay Pending because nodeSelector requires every listed label to match, and no node has both. Run kubectl describe pod <name> and read the Events section, which reports that no nodes matched the Pod's node selector.
Exercise
You want a Pod to run on nodes labeled either disktype=ssd or disktype=nvme. Can nodeSelector express this?
Show answer
No. nodeSelector supports only exact key-value matches combined with AND. To express OR or set-based rules, use node affinity with a matchExpressions rule using the In operator.