Explain taints, tolerations, and node affinity.
Quick Answer
Taints repel pods from a node unless the pod has a matching toleration — used to reserve nodes. Node affinity attracts pods to nodes with certain labels. Taints are opt-out, affinity is opt-in; together they control placement.
Detailed Answer
A taint on a node says 'don't schedule here unless you explicitly tolerate me' (effects: NoSchedule, PreferNoSchedule, NoExecute — the last also evicts running pods that don't tolerate it). You use taints to dedicate nodes: GPU nodes, control-plane nodes, or nodes under maintenance. A toleration on a pod lets it ignore a matching taint (but doesn't force it there). Node affinity is the pull side: requiredDuringScheduling... hard-requires node labels, preferred... is a soft preference — e.g., 'schedule on nodes with disktype=ssd.' Pod affinity/anti-affinity extends this to co-locate or spread pods relative to other pods (e.g., anti-affinity to keep replicas on different nodes/zones for HA). The mental model: taints/tolerations gate access to nodes; affinity expresses preferences/requirements about where to land.
Code Example
# Reserve GPU nodes
kubectl taint nodes gpu-1 gpu=true:NoSchedule
# Pod that may use them + prefers SSDs
tolerations: [{ key: gpu, value: "true", effect: NoSchedule }]
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms: [{ matchExpressions: [{ key: disktype, operator: In, values: [ssd] }] }]Interview Tip
One crisp line: 'taints repel (opt-out via toleration), affinity attracts (opt-in).' Then mention pod anti-affinity for spreading replicas across zones — a classic HA follow-up.