Difficulty: Beginner
What is a nodeSelector in Kubernetes and how do you use it to control where Pods run?
nodeSelector is the simplest way to constrain where a Pod can be scheduled. It is a map of label key-value pairs in the Pod spec, and the scheduler only places the Pod on nodes that carry every one of those labels; if no node matches, the Pod stays Pending.
Nodes have labels just like any other object. Some are set automatically, and you can add your own:
kubernetes.io/os, kubernetes.io/arch, kubernetes.io/hostname, node.kubernetes.io/instance-type, topology.kubernetes.io/zonekubectl label nodes node-7 disktype=ssd or labels set on a managed node pool, such as workload=gpuDuring scheduling, the filtering step drops every node whose labels do not include all the pairs in nodeSelector. Matching is exact and AND-only: disktype: ssd and zone: ap-south-1a means both must be present. The remaining nodes are then scored as usual.
kubernetes.io/os: windowskubernetes.io/arch: arm64| Feature | What it expresses | Limitation |
|---|---|---|
nodeSelector | Pod must land on nodes with these exact labels | Only equality, only AND, only hard rules |
| Node affinity | In, NotIn, Exists, Gt, Lt, OR across terms, soft preferences | More verbose YAML |
| Taints and tolerations | Node repels Pods that do not tolerate it | Does not attract Pods by itself |
A key point interviewers look for: nodeSelector attracts Pods to nodes, but it does not keep other Pods away. To dedicate a GPU pool to GPU workloads, you taint the GPU nodes (so other Pods are repelled) and give GPU Pods both a toleration and a nodeSelector (so they land there).
FailedScheduling with a message like didn't match Pod's node affinity/selector in kubectl describe pod.# Add a label to a node
kubectl label nodes worker-3 disktype=ssd
# Show labels as columns
kubectl get nodes -L disktype,topology.kubernetes.io/zone
# If a Pod is stuck Pending, check the scheduler message
kubectl describe pod redis-0 | grep -A3 Events
-L shows chosen labels as columns, which makes it quick to check which nodes a nodeSelector can match.
apiVersion: v1
kind: Pod
metadata:
name: redis
spec:
nodeSelector:
disktype: ssd
kubernetes.io/arch: amd64
containers:
- name: redis
image: redis:7.4
The Pod can only be scheduled on nodes that have both labels; otherwise it remains Pending.
nodeSelector, Node Labels, Scheduling Constraints, Node Affinity, Taints and Tolerations