Difficulty: Advanced
What is the difference between Node Affinity and Taints/Tolerations, and when would you use each (or both together)?
Node Affinity is a rule on the Pod that pulls it towards nodes with certain labels. A Taint is a mark on the Node that pushes Pods away, and a Toleration is a Pod-side exception that lets a Pod ignore a specific taint. Affinity answers "where does this Pod want to go", taints answer "who is allowed on this node", and to truly dedicate nodes to a workload you need both.
Node Affinity is a more expressive version of nodeSelector. It matches node labels using operators In, NotIn, Exists, DoesNotExist, Gt and Lt, and comes in two flavours:
requiredDuringSchedulingIgnoredDuringExecution: a hard rule. If no node matches, the Pod stays Pending.preferredDuringSchedulingIgnoredDuringExecution: a soft rule with a weight from 1 to 100. The scheduler adds the weight to a node's score but will still place the Pod elsewhere if needed.The IgnoredDuringExecution suffix matters: if someone removes the label from a node later, Pods already running there are not evicted.
A taint has the form key=value:Effect, for example kubectl taint nodes gpu-1 gpu=true:NoSchedule. Any Pod without a matching toleration cannot be scheduled there. A toleration matches with operator: Equal (key and value) or operator: Exists (key only).
The key point interviewers look for: a toleration only permits, it never attracts. A Pod that tolerates the GPU taint can still land on an ordinary node. Kubernetes uses this itself: control plane nodes carry node-role.kubernetes.io/control-plane:NoSchedule so normal workloads stay off them.
| Aspect | Node Affinity | Taints + Tolerations |
|---|---|---|
| Defined on | Pod spec | Taint on Node, toleration on Pod |
| Effect | Attracts Pod to matching nodes | Repels Pods that do not tolerate |
| Hard and soft modes | required / preferred | NoSchedule / PreferNoSchedule |
| Can evict running Pods | No | Yes, with NoExecute |
| Typical use | "Run on SSD nodes in zone ap-south-1a" | "Keep everyone except GPU jobs off GPU nodes" |
Say a team at Swiggy runs ML inference on expensive GPU nodes. They want two guarantees: general Pods never waste GPU capacity, and inference Pods never land on cheap CPU nodes.
gpu=true:NoSchedule and label them accelerator=nvidia.gpu=true (allowed in) plus a required node affinity for accelerator=nvidia (must go there).Taint alone keeps others out but lets inference Pods drift; affinity alone pulls inference Pods in but lets others in too. Managed services like EKS, GKE and AKS let you set both on a node group so new nodes come up correctly.
Pending after that zone's nodes are full. Check kubectl describe pod events for didn't match Pod's node affinity/selector.topologyKey).| Node Affinity | Taint | Toleration | |
|---|---|---|---|
| Set on | Pod | Node | Pod |
| Direction | Pulls Pod to node | Pushes Pods away | Allows past a taint |
| Guarantees placement | Yes, if required | No | No |
| Example | accelerator In [nvidia] | gpu=true:NoSchedule | key: gpu, Exists |
# Repel everything that does not tolerate gpu=true
kubectl taint nodes gpu-node-1 gpu=true:NoSchedule
# Label the node so Pods can be attracted to it
kubectl label nodes gpu-node-1 accelerator=nvidia
# Remove the taint later (note the trailing minus)
kubectl taint nodes gpu-node-1 gpu=true:NoSchedule-
The taint keeps general workloads off the node and the label gives affinity something to match on.
apiVersion: v1
kind: Pod
metadata:
name: inference
spec:
tolerations:
- key: gpu
operator: Equal
value: "true"
effect: NoSchedule # allowed onto tainted nodes
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: accelerator
operator: In
values: [nvidia] # must go to GPU nodes
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 50
preference:
matchExpressions:
- key: topology.kubernetes.io/zone
operator: In
values: [ap-south-1a]
containers:
- name: model
image: myorg/inference:1.4.0
The toleration lets the Pod in, the required affinity forces it there, and the weighted preference nudges it towards one zone without making it mandatory.
Node Affinity, Taints, Tolerations, nodeSelector, Dedicated Node Pools