Place pods with affinity, taints and tolerations
In this task you'll control which nodes your pods run on, and keep other pods off special nodes.
The scheduler picks a node for each new pod. You steer it from two sides. From the pod's side, a nodeSelector or node affinity says which nodes the pod wants, by their labels (required, or just preferred). From the node's side, a taint keeps pods away unless they carry a matching toleration.
Use them together for dedicated nodes: taint the GPU or database nodes so ordinary pods stay off, and give only the right pods both the toleration (allowed there) and the affinity (wants to go there). A toleration alone only allows a pod onto a node; it doesn't send it there. Pod anti-affinity and topology spread spread copies of an app across nodes or zones.
Example. Brindle Outdoor runs its image-resizing service on two large nodes. The nodes are labelled workload=media and tainted dedicated=media:NoSchedule. The resizer's deployment has the toleration and a required node affinity for workload=media, so it lands only there, and nothing else can.
Before you start
You need: Docker Desktop, kind and kubectl on your computer (all free), and a cluster from Run a practice cluster on your computer. Time: 30 minutes.
Where to look
- Terminal
- kubectl
Steps
- Label one worker:
kubectl label node lab-worker workload=media. - Taint the same node:
kubectl taint node lab-worker dedicated=media:NoSchedule. - Create a plain nginx deployment with 3 replicas and check with
kubectl get pods -o widethat none run on lab-worker.The taint keeps them away. - Add the toleration and a required node affinity for
workload=mediato the deployment (see Commands), apply it, and check all pods now run on lab-worker. - Remove the affinity but keep the toleration, and see the pods spread across both workers again.Allowed is not the same as sent.
- Clean up:
kubectl taint node lab-worker dedicated=media:NoSchedule-andkubectl label node lab-worker workload-.
Commands
kubectl label node lab-worker workload=media
kubectl taint node lab-worker dedicated=media:NoSchedule
# In the deployment's pod template (spec.template.spec):
tolerations:
- key: dedicated
operator: Equal
value: media
effect: NoSchedule
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: workload
operator: In
values: [media]
kubectl get pods -o wide
Watch out for
- Cost
- Free on kind.
- Security
- Taints aren't a security boundary: anyone who can create pods can add a toleration. Use admission policies to enforce who may run where.
- Common mistakes
- Using only a toleration and expecting the pod to move, and using required affinity to labels no node has, which leaves pods Pending.
Clean up
Remove the taint and label as in the last step, and delete the deployment.
Words to know
- Node affinity
- Rules in a pod that say which node labels it needs or prefers.
- Taint
- A mark on a node that repels pods without a matching toleration.
- Toleration
- A setting in a pod that lets it run on a tainted node.
- Topology spread
- A rule that spreads a deployment's pods evenly across nodes or zones.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On