NodeSelector

Difficulty: Beginner

Question

What is a nodeSelector in Kubernetes and how do you use it to control where Pods run?

Answer

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.

How it works with node labels

Nodes have labels just like any other object. Some are set automatically, and you can add your own:

  • Well-known labels: kubernetes.io/os, kubernetes.io/arch, kubernetes.io/hostname, node.kubernetes.io/instance-type, topology.kubernetes.io/zone
  • Custom labels you add: kubectl label nodes node-7 disktype=ssd or labels set on a managed node pool, such as workload=gpu

During 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.

Common use cases

  • Run GPU workloads (an ML model for fraud scoring at a payments company) only on GPU node pools
  • Pin Windows containers to Windows nodes with kubernetes.io/os: windows
  • Send ARM-built images to Graviton or other ARM nodes with kubernetes.io/arch: arm64
  • Keep databases on nodes with local NVMe SSDs

nodeSelector vs node affinity vs taints

FeatureWhat it expressesLimitation
nodeSelectorPod must land on nodes with these exact labelsOnly equality, only AND, only hard rules
Node affinityIn, NotIn, Exists, Gt, Lt, OR across terms, soft preferencesMore verbose YAML
Taints and tolerationsNode repels Pods that do not tolerate itDoes 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).

Gotchas

  • Pending Pods: a typo in a label or a missing label shows up as FailedScheduling with a message like didn't match Pod's node affinity/selector in kubectl describe pod.
  • Labels change later: if a node's label is removed after the Pod is running, the Pod is not evicted. The rule is only checked at scheduling time.
  • Capacity: if all matching nodes are full, Pods wait even though other nodes are idle. With Cluster Autoscaler, the node group template must carry the label so new nodes can be created for these Pods.
  • Label hygiene: prefer labels managed by the node pool or cloud provider over hand-labelled nodes, which are lost when nodes are replaced.

Code examples

Label a node and target it

# 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.

Pod with nodeSelector

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.

Key points

  • nodeSelector is a map of labels; a node must have all of them (AND, exact match)
  • No matching node means the Pod stays Pending with a FailedScheduling event
  • It only attracts Pods; use taints to keep other Pods off special nodes
  • Node affinity supersedes it when you need operators, OR logic or soft preferences
  • Checked only at scheduling time; removing a label does not evict running Pods

Common follow-up questions

  • How would you dedicate a set of GPU nodes to only GPU workloads?
  • What is the difference between required and preferred node affinity?
  • Why might a Pod with a nodeSelector stay Pending even though matching nodes exist?
  • What happens to running Pods if you remove the label from their node?

Interview tips

  • Say clearly that nodeSelector attracts but does not repel, then bring in taints
  • Use a concrete example like GPU or ARM nodes rather than abstract labels
  • Mention kubectl describe pod and the FailedScheduling event for debugging

Concepts covered

nodeSelector, Node Labels, Scheduling Constraints, Node Affinity, Taints and Tolerations

More Kubernetes & Orchestration questions