Skip to main content

Taints and Affinity

· 4 min read
Femi Adigun
Senior Software Engineer & Coach

These are core Kubernetes scheduling concepts, and they often trip people up because they sound similar but serve different purposes.

Let’s make them visual, memorable, and practical — so you’ll never forget them again.


🎯 TL;DR Memory Hook

🧲 Affinity = attraction (where Pods want to go) ☠️ Taint = repulsion (where Pods cannot go — unless they tolerate it)


TAINTS and TOLERATIONS (Think: “KEEP OUT” signs)

Analogy:

Imagine Kubernetes nodes as rooms in a hotel.

  • Some rooms have “Do Not Disturb” signs (🚫) — those are taints.
  • Only guests (Pods) with matching “permission slips” (🪪 tolerations) can enter those rooms.

💡 Real-world Example:

Let’s say you have a GPU node for machine learning jobs. You don’t want random web servers to run there — only GPU workloads.

You’d taint the node like this:

kubectl taint nodes gpu-node gpu=true:NoSchedule

This means:

“Don’t schedule any Pod here unless it tolerates this taint.”

Now, a Pod that can run on GPUs adds a toleration:

tolerations:
- key: "gpu"
operator: "Equal"
value: "true"
effect: "NoSchedule"

That Pod is now “immune” to the taint — it can run on that node.


Quick Rule of Thumb

ConceptPurposeMemory Trick
TaintMarks a node as restricted“KEEP OUT” sign on node
TolerationLets a pod enter despite taint“Permission slip” on pod

Types of Taint Effects

EffectMeaning
NoSchedulePod won’t be scheduled unless it tolerates the taint
PreferNoScheduleTry to avoid scheduling, but not strict
NoExecuteExisting Pods are evicted if they don’t tolerate the taint

NODE and POD AFFINITY (Think: “PREFERENCE”)

Now, affinity and anti-affinity are about where Pods prefer to be scheduled — not forced, but guided.


Analogy:

  • Affinity = “I like to be near X.”
  • Anti-affinity = “I don’t want to be near X.”

Example: You may want all backend Pods to run on the same node zone as your database for low latency.

That’s node affinity.

Or maybe you want replicas of a web service spread across different nodes for high availability — that’s pod anti-affinity.


💡 Node Affinity Example:

affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: zone
operator: In
values:
- us-east-1a

Meaning:

“Only schedule this Pod on nodes in us-east-1a.”


💡 Pod Affinity Example:

affinity:
podAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- frontend
topologyKey: "kubernetes.io/hostname"

Meaning:

“Try to schedule this Pod near another Pod with label app=frontend on the same node.”


Pod Anti-Affinity Example:

affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- web
topologyKey: "kubernetes.io/hostname"

Meaning:

“Don’t put two Pods with label app=web on the same node.” Used for high availability — prevents all replicas landing on one machine.


Simple Way to Remember

ConceptWhat it ControlsEasy Way to Remember
TaintNode says “I don’t want certain Pods”Node → “Stay away!”
TolerationPod says “I’m okay with that taint”Pod → “I can handle it.”
Node AffinityPod prefers certain NodesPod → “I like those nodes.”
Pod AffinityPod prefers to be near other PodsPod → “I like being near X.”
Pod Anti-AffinityPod avoids certain PodsPod → “I don’t like being near X.”

Bonus Tip:

You can mix them:

  • Taints/tolerations = hard rules (enforcement)
  • Affinity/anti-affinity = soft preferences (placement)

Think of it like:

“Taints control who can enter the building; affinities control where they sit inside.”