Schedules: Affinity & Taints

Difficulty: Advanced

Question

What is the difference between Node Affinity and Taints/Tolerations, and when would you use each (or both together)?

Answer

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: the Pod chooses

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.

Taints and Tolerations: the Node chooses

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.

Side by side

AspectNode AffinityTaints + Tolerations
Defined onPod specTaint on Node, toleration on Pod
EffectAttracts Pod to matching nodesRepels Pods that do not tolerate
Hard and soft modesrequired / preferredNoSchedule / PreferNoSchedule
Can evict running PodsNoYes, with NoExecute
Typical use"Run on SSD nodes in zone ap-south-1a""Keep everyone except GPU jobs off GPU nodes"

Using both to dedicate a node pool

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.

  1. Taint the GPU nodes with gpu=true:NoSchedule and label them accelerator=nvidia.
  2. Give inference Pods a toleration for 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.

Common mistakes

  • Assuming a toleration schedules the Pod onto the tainted node.
  • Using a required affinity on a label that only exists in one zone, then wondering why Pods are Pending after that zone's nodes are full. Check kubectl describe pod events for didn't match Pod's node affinity/selector.
  • Confusing node affinity with Pod affinity/anti-affinity, which places Pods relative to other Pods (for example spreading replicas across nodes using topologyKey).
Who decides placement. Affinity lives on the Pod and attracts; taints live on the Node and repel.
Node AffinityTaintToleration
Set onPodNodePod
DirectionPulls Pod to nodePushes Pods awayAllows past a taint
Guarantees placementYes, if requiredNoNo
Exampleaccelerator In [nvidia]gpu=true:NoSchedulekey: gpu, Exists

Code examples

Dedicating GPU nodes with taint, label and affinity

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

Pod that tolerates the taint and requires the GPU label

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.

Key points

  • Affinity attracts (Pod side); taints repel (Node side); tolerations only permit, never attract.
  • required = hard rule (Pod stays Pending), preferred = soft rule with weight 1-100.
  • IgnoredDuringExecution: changing node labels does not evict running Pods.
  • Only taints with NoExecute can evict Pods that are already running.
  • To dedicate nodes to a workload, combine a taint with a node affinity.

Common follow-up questions

  • What are the three taint effects and how does NoExecute differ?
  • How is Pod anti-affinity different from node affinity?
  • How would you spread replicas evenly across availability zones?
  • Why do DaemonSet Pods still run on tainted nodes?

Interview tips

  • Say "tolerations allow, they don't attract" explicitly; it is the most common misconception.
  • Walk through the dedicated GPU node pool example; it shows you understand why both mechanisms exist.
  • Mention the control plane taint as a real example Kubernetes ships with.

Concepts covered

Node Affinity, Taints, Tolerations, nodeSelector, Dedicated Node Pools

More Kubernetes & Orchestration questions