Scheduling: where Pods run
How the scheduler picks a node, and how to steer it: node selectors, taints and tolerations, topology spread across zones, and draining a node safely with a PodDisruptionBudget.
By the end of this lesson you'll be able to
0 of 4 completeVerified lab: topology spread across two zones, a taint keeping a Pod out, and a drain blocked by a
PodDisruptionBudget, on the course's EKS cluster. The output shown is real.
Code walkthrough: node affinity and priority (explained).
Cloud lab: zone spreading needs a multi-zone cluster; the rest works on kind.
Most of the time you don't care which node a Pod lands on. Sometimes you do: spread replicas so one zone's outage doesn't take them all, keep special workloads on special nodes, and move Pods off a node safely before maintenance.
How the scheduler decides
For every new Pod, the scheduler runs two phases:
- Filter: drop nodes that can't run it: not enough unreserved CPU or memory for its requests, a node selector that doesn't match, a taint it doesn't tolerate, a volume in another zone.
- Score: rank the rest, preferring spread-out placement and balanced usage, and pick the best.
If no node passes the filter, the Pod stays Pending and the scheduler writes why in its events, as lessons 9 and 10 showed.
Spread across zones
The course cluster has nodes in two zones:
$ kubectl get nodes -L topology.kubernetes.io/zone NAME STATUS ROLES AGE VERSION ZONE ip-10-42-0-115.ap-south-1.compute.internal Ready <none> 63m v1.37.1-eks-6e7947a ap-south-1a ip-10-42-14-248.ap-south-1.compute.internal Ready,SchedulingDisabled <none> 12m v1.37.1-eks-6e7947a ap-south-1a ip-10-42-28-149.ap-south-1.compute.internal Ready <none> 63m v1.37.1-eks-6e7947a ap-south-1b
kubectl get nodes -L topology.kubernetes.io/zonekubectltalk to a Kubernetes clustergetlist objects
(The middle node was the Cluster Autoscaler's extra node, already SchedulingDisabled because the autoscaler was removing it.) To guarantee replicas end up in both zones:
# 11-spread.yaml (excerpt) spec: replicas: 4 template: spec: topologySpreadConstraints: - maxSkew: 1 # zones may differ by at most 1 Pod topologyKey: topology.kubernetes.io/zone whenUnsatisfiable: DoNotSchedule labelSelector: matchLabels: app: spread
$ kubectl get pods -l app=spread -o custom-columns=POD:.metadata.name,NODE:.spec.nodeName spread-b547844df-52tsg ip-10-42-0-115.ap-south-1.compute.internal spread-b547844df-58j5p ip-10-42-28-149.ap-south-1.compute.internal spread-b547844df-chwpt ip-10-42-28-149.ap-south-1.compute.internal spread-b547844df-pvtb5 ip-10-42-0-115.ap-south-1.compute.internal
kubectl get pods -l app=spread -o custom-columns=POD:.metadata.name,NODE:.spec.nodeNamekubectltalk to a Kubernetes clustergetlist objects-lonly objects with this label-ooutput format
Two Pods per zone. If ap-south-1a went down, half the app would keep serving. whenUnsatisfiable: ScheduleAnyway
makes it a preference instead of a rule.
Taints and tolerations
A taint on a node repels Pods; a toleration on a Pod lets it in anyway. Use them to reserve nodes: GPU machines, nodes for batch jobs, or nodes for one team.
$ kubectl label node ip-10-42-0-115.ap-south-1.compute.internal workload=batch $ kubectl taint node ip-10-42-0-115.ap-south-1.compute.internal dedicated=batch:NoSchedule
kubectl label node ip-10-42-0-115.ap-south-1.compute.internal workload=batchkubectltalk to a Kubernetes cluster
Two Pods both select that node with nodeSelector: workload: batch; only the first tolerates the taint:
# 11-gpu-only.yaml (excerpt, first Pod) tolerations: - key: dedicated operator: Equal value: batch effect: NoSchedule nodeSelector: workload: batch
$ kubectl get pods tolerates-dedicated no-toleration -o wide tolerates-dedicated 1/1 Running 0 19s 10.42.3.48 ip-10-42-0-115.ap-south-1.compute.internal <none> <none> no-toleration 0/1 Pending 0 18s <none> <none> <none> <none> $ kubectl describe pod no-toleration | sed -n '/Events:/,$p' Warning FailedScheduling 21s default-scheduler 0/3 nodes are available: 1 node(s) didn't match Pod's node affinity/selector, 1 node(s) had untolerated taint(s), 1 node(s) were unschedulable. ...
kubectl get pods tolerates-dedicated no-toleration -o widekubectltalk to a Kubernetes clustergetlist objects-ooutput format
kubectl describe pod no-toleration | sed -n '/Events:/,$p'describeshow details and events|pipe: send output to the next command
The scheduler's message accounts for every node: one didn't match the selector, one had the taint, one was being
removed. A toleration only allows a Pod onto a tainted node; it doesn't send it there. That's why the example
pairs it with a node selector (or node affinity, its more flexible form). Effects: NoSchedule blocks new Pods,
PreferNoSchedule avoids if possible, NoExecute also evicts Pods already running.
Draining and evictions
Before upgrading or replacing a node, you drain it: mark it unschedulable (cordon) and evict its Pods so their controllers recreate them elsewhere. A PodDisruptionBudget stops a drain from taking too many replicas of one app at once:
# 11-pdb.yaml apiVersion: policy/v1 kind: PodDisruptionBudget metadata: name: shop-web spec: minAvailable: 2 selector: matchLabels: app: shop-web
At that moment shop-web had exactly 2 replicas:
$ kubectl get pdb shop-web NAME MIN AVAILABLE MAX UNAVAILABLE ALLOWED DISRUPTIONS AGE shop-web 2 N/A 0 3s $ kubectl drain ip-10-42-0-115.ap-south-1.compute.internal --ignore-daemonsets --delete-emptydir-data --timeout=180s node/ip-10-42-0-115.ap-south-1.compute.internal cordoned Warning: ignoring DaemonSet-managed Pods: kube-system/aws-node-hqdfw, kube-system/ebs-csi-node-lp977, kube-system/eks-pod-identity-agent-mfk7m, kube-system/kube-proxy-6r4f2 evicting pod default/shop-web-57f494cdcb-ltvkc error when evicting pods/"shop-web-57f494cdcb-ltvkc" -n "default" (will retry after 5s): Cannot evict pod as it would violate the pod's disruption budget. pod/aws-load-balancer-controller-6f89d47788-5fj68 evicted pod/coredns-8966fd9f9-b7ppz evicted pod/metrics-server-596c8d7669-j7hrt evicted ... error when evicting pods/"shop-web-57f494cdcb-ltvkc" -n "default" (will retry after 5s): Cannot evict pod as it would violate the pod's disruption budget. ... There are pending nodes to be drained: $ kubectl uncordon ip-10-42-0-115.ap-south-1.compute.internal node/ip-10-42-0-115.ap-south-1.compute.internal uncordoned
kubectl get pdb shop-webkubectltalk to a Kubernetes clustergetlist objects
Everything else moved off the node, but the shop-web Pod didn't: with 2 replicas and minAvailable: 2, allowed
disruptions is 0, so every eviction was refused and the drain timed out after 3 minutes. The PDB did its job
too well. The fix is to run more replicas than the budget requires (3 replicas with minAvailable: 2 allows one
at a time) or use maxUnavailable: 1. DaemonSet Pods are skipped (--ignore-daemonsets) because they belong on
every node.
PDBs only stop voluntary disruptions like drains and autoscaler scale-downs. A node that crashes, or node-pressure eviction (the kubelet evicting Pods when the node runs out of memory or disk), ignores them.
Place, reserve and drain
Spread a 4-replica Deployment across zones and record where each Pod lands. Taint one node and show a Pod without a toleration staying Pending. Apply a PDB with minAvailable 2 to shop-web, try draining with 2 replicas, then with 3, and record the difference.
~/scheduling.txtReveal one safe solution
$ kubectl apply -f 11-spread.yaml && kubectl rollout status deployment/spread $ kubectl get pods -l app=spread -o custom-columns=POD:.metadata.name,NODE:.spec.nodeName > ~/scheduling.txt $ kubectl apply -f 11-pdb.yaml && kubectl scale deployment shop-web --replicas=2 $ N=$(kubectl get pods -l app=shop-web -o jsonpath='{.items[0].spec.nodeName}') $ kubectl drain $N --ignore-daemonsets --timeout=60s >> ~/scheduling.txt 2>&1; kubectl uncordon $N $ kubectl scale deployment shop-web --replicas=3 && kubectl rollout status deployment/shop-web $ kubectl drain $N --ignore-daemonsets --timeout=120s >> ~/scheduling.txt 2>&1; kubectl uncordon $N
kubectl apply -f 11-spread.yaml && kubectl rollout status deployment/spreadkubectltalk to a Kubernetes clusterapplycreate or update from a file-ffrom this file&&run the next command only if this one workedrolloutmanage a rollout
kubectl get pods -l app=spread -o custom-columns=POD:.metadata.name,NODE:.spec.nodeName > ~/scheduling.txtgetlist objects-lonly objects with this label-ooutput format>write output to a file, replacing it
kubectl apply -f 11-pdb.yaml && kubectl scale deployment shop-web --replicas=2scalechange the number of replicas--replicas=2how many copies
kubectl drain $N --ignore-daemonsets --timeout=60s >> ~/scheduling.txt 2>&1; kubectl uncordon $N>>append output to a file
kubectl scale deployment shop-web --replicas=3 && kubectl rollout status deployment/shop-web--replicas=3how many copies
The first drain should time out on the disruption budget; the second should complete.
Try it yourself
0 of 5 steps doneRun each task in your own account or terminal, then tick it off.
Quick check
A node has the taint dedicated=batch:NoSchedule. Which Pods can be scheduled on it?
Official reading for this lesson
Or get every quiz answer right and it completes itself.
Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.