Scheduling and nodesLesson 6 of 10
- Requests, limits and quotas, not reconciled
- QoS classes and node-pressure eviction, not reconciled
- cgroups, memory pressure and the OOM killer, not reconciled
- How the scheduler places a pod, not reconciled
- Affinity, taints and tolerations, not reconciled
- Topology spread constraints, not reconciled
- Priority and preemption, not reconciled
- Nodes and the kubelet, not reconciled
- Disruptions and PodDisruptionBudgets, not reconciled
- Cluster autoscaling, not reconciled
Topology spread constraints
Topology spread constraints keep a workload's replicas balanced across zones and nodes, as a hard filter or a soft preference, and they hold only at the moment each pod is scheduled.
Shows the lesson one block at a time. Arrow keys step, number keys jump to a scene, and Escape returns to the page where you were.
A Deployment with three replicas tells the cluster how many pods to run, not where. The scheduler places each pod on its own, one scheduling cycle at a time, and nothing in a plain pod template stops all three from landing on the same node or in the same zone. Topology spread constraints add the missing rule: keep the pods that match a selector roughly even across the values of a node label.
Reconcile, in 7 scenes
Why spread
Replicas exist so that one failure does not take the service down. That only works if the replicas fail independently. Three pods on one node die together when the node’s kernel panics; three pods in one zone die together when the zone loses power or network. Spreading is how you turn a replica count into actual redundancy.
topology domainThe set of nodes that share one value of a node label. Every node withtopology.kubernetes.io/zone=zone-a is one zone domain; every node is its own kubernetes.io/hostname domain.
Before spread constraints, the tool for this was pod anti-affinity, and it is too blunt in both of its modes. Required anti-affinity allows at most one matching pod per topology domain, so a workload spread by zone across three zones can never run a fourth replica. Preferred anti-affinity is only a scoring hint and enforces nothing. Affinity also answers a different question: required pod affinity passes a node when any node in the node’s topology domain runs a matching pod, so it reasons about presence, not counts. A spread constraint counts matching pods per domain and caps the difference, which is the property redundancy needs.
The scheduler evaluates spread for one pod at a time. Scheduling cycles run serially, one pod each; only the binding step that follows can run concurrently. Every spread decision therefore sees the pods already placed and the one being placed, and nothing else. The Kubernetes documentation gives no fixed throughput for this loop: how many pods per second it places depends on cluster size, the enabled plugins and percentageOfNodesToScore.
maxSkew and topologyKey
A constraint has four parts you always set. The API requires three of them; it accepts a constraint without the fourth, labelSelector, but such a constraint counts no pods at all.
topologyKeyis a node label key. Each distinct value of that label is one domain.labelSelectorpicks the pods that are counted, usually the workload’s own pod labels.maxSkewis the largest allowed difference between the number of matching pods in a domain and the global minimum, the lowest count among eligible domains.whenUnsatisfiabledecides what happens when no node keeps the skew withinmaxSkew; the next section covers it.
The arithmetic is per placement. Suppose three zones hold 2, 2 and 1 matching pods and maxSkew is 1. The global minimum is 1. Placing the next pod in either of the first two zones would make that zone 3 against a minimum of 1, a skew of 2, so only the third zone qualifies. After that pod lands, all three zones hold 2 and any of them can take the next one.
The two keys you use most are well known. The kubelet sets kubernetes.io/hostname on every node. Cloud providers set topology.kubernetes.io/zone and topology.kubernetes.io/region when a node registers. A kind cluster has no zones, so the transcript below labels the kx workers into two pretend zones first. A node that lacks the topology key is not part of any domain, and a DoNotSchedule constraint filters it out entirely.
Here is a Deployment that spreads four replicas across zones as a hard rule and across nodes as a preference. Save it as web.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 4
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: web
matchLabelKeys:
- pod-template-hash
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app: web
matchLabelKeys:
- pod-template-hash
containers:
- name: web
image: nginx:1.30
resources:
requests:
cpu: 100m
memory: 64Mi
kubectl label node kx-worker kx-worker2 topology.kubernetes.io/zone=zone-a
node/kx-worker labeled node/kx-worker2 labeled
kubectl label node kx-worker3 topology.kubernetes.io/zone=zone-b
node/kx-worker3 labeled
kubectl get nodes -L topology.kubernetes.io/zone
NAME STATUS ROLES AGE VERSION ZONE kx-control-plane Ready control-plane 30s v1.37.0 kx-worker Ready <none> 25s v1.37.0 zone-a kx-worker2 Ready <none> 25s v1.37.0 zone-a kx-worker3 Ready <none> 25s v1.37.0 zone-b
kubectl apply -f web.yaml
deployment.apps/web created
kubectl rollout status deployment web
Waiting for deployment "web" rollout to finish: 0 of 4 updated replicas are available... deployment "web" successfully rolled out
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES web-5bd8568956-crwtn 1/1 Running 0 5s 10.244.3.2 kx-worker3 <none> <none> web-5bd8568956-dq87d 1/1 Running 0 5s 10.244.2.2 kx-worker2 <none> <none> web-5bd8568956-nshlv 1/1 Running 0 5s 10.244.1.3 kx-worker <none> <none> web-5bd8568956-pggpj 1/1 Running 0 5s 10.244.3.3 kx-worker3 <none> <none>
Zone-a holds two pods and zone-b holds two, so the hard zone rule is met exactly. Zone-b has only one node, so both of its pods share kx-worker3: the hostname constraint is a preference and loses to the zone rule. Constraints on one pod are combined with a logical AND, and a pod may carry only one constraint per pair of topologyKey and whenUnsatisfiable.
The two constraints do different jobs. A zone constraint alone says nothing about nodes inside a zone; with only the zone rule, all of zone-a’s pods can stack on one of its nodes, and losing that node costs half the service. The hostname constraint is what spreads them inside each zone.
whenUnsatisfiable
The field decides which phase of the scheduler the constraint belongs to.
DoNotSchedule, the default, makes the constraint a filter. A node that would push the skew pastmaxSkewis removed from the candidate list, exactly like a node without enough CPU. If every node is removed, the pod stays Pending.ScheduleAnywaymakes it a scoring signal. Nodes that reduce the skew score higher, but the score is weighed against every other scoring plugin and no node is ever ruled out. The pod is always placed if anything else fits.
ExplainerThe scheduling pipeline
The pending pod web, labelled app=web. The controls edit its YAML.
pod.yaml
Edits rerun the scheduler as you type.
Filters, in order
- NodeUnschedulable
- NodeName
- TaintToleration
- NodeAffinity
- NodePorts
- NodeResourcesFit
- VolumeBinding
- PodTopologySpread
- InterPodAffinity
Drag the pod onto a node, or onto the scheduler to let it choose. On a touch screen, tap the pod, then tap a node. From the keyboard, Space picks it up and the arrow keys choose.
A placed pod carries a required node affinity on kubernetes.io/hostname, not spec.nodeName, so the scheduler still runs every filter and can refuse it.
kx-control-plane
control plane, no zone labeltaint node-role.kubernetes.io/control-plane:NoScheduleno app=web podsPlaced hereCPU3050m free of 4000m
Memory7902Mi free of 8192Mi
TaintToleration: node(s) had untolerated taint(s)
kx-worker
zone zone-a, disktype=ssd2 app=web podsPlaced hereCPU2800m free of 4000m
Memory6918Mi free of 8192Mi
Passed all 9 filters
kx-worker2
zone zone-b, disktype=ssdtaint dedicated=batch:NoScheduleno app=web podsPlaced hereCPU1400m free of 4000m
Memory4046Mi free of 8192Mi
TaintToleration: node(s) had untolerated taint(s)
kx-worker3
zone zone-b, disktype=hdd1 app=web podPlaced hereCPU1400m free of 2000m
Memory3534Mi free of 4096Mi
Passed all 9 filters
$
Each placement writes pods/<name>.yaml and runs kubectl apply on it; the command and the scheduler's event show here.
Nothing placed yet.
Scoring Each plugin scores 0 to 100, times its weight; the highest total wins.
| Plugin | kx-worker | kx-worker3 |
|---|---|---|
| Taint | 300100 × 3 | 300100 × 3 |
| Node | 00 × 2 | 00 × 2 |
| Node | 6666 × 1 | 5757 × 1 |
| Pod | 00 × 2 | 00 × 2 |
| Inter | 00 × 2 | 00 × 2 |
| Node | 7373 × 1 | 7070 × 1 |
| Image | 00 × 1 | 00 × 1 |
| Total of 1200 | 439 | 427 |
Event from default-scheduler
Normal Scheduled
Successfully assigned default/web to kx-worker
Stage 6 of 6
Bound to kx-worker: 2 nodes passed the filters and it scored highest, 439 of 1200.
Bound to kx-worker: 2 nodes passed the filters and it scored highest, 439 of 1200.
The filter behaviour is easiest to see when a domain stops accepting pods. Cordon the only node in zone-b and scale the Deployment to six:
kubectl label node kx-worker kx-worker2 topology.kubernetes.io/zone=zone-a
node/kx-worker labeled node/kx-worker2 labeled
kubectl label node kx-worker3 topology.kubernetes.io/zone=zone-b
node/kx-worker3 labeled
kubectl apply -f web.yaml
deployment.apps/web created
kubectl rollout status deployment web
Waiting for deployment "web" rollout to finish: 0 of 4 updated replicas are available... deployment "web" successfully rolled out
kubectl cordon kx-worker3
node/kx-worker3 cordoned
kubectl scale deployment web --replicas=6
deployment.apps/web scaled
sleep 10
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES web-5bd8568956-2n5lp 1/1 Running 0 10s 10.244.2.3 kx-worker2 <none> <none> web-5bd8568956-crwtn 1/1 Running 0 15s 10.244.3.2 kx-worker3 <none> <none> web-5bd8568956-dq87d 1/1 Running 0 15s 10.244.2.2 kx-worker2 <none> <none> web-5bd8568956-lvj5d 0/1 Pending 0 10s <none> <none> <none> <none> web-5bd8568956-nshlv 1/1 Running 0 15s 10.244.1.3 kx-worker <none> <none> web-5bd8568956-pggpj 1/1 Running 0 15s 10.244.3.3 kx-worker3 <none> <none>
kubectl get events --field-selector reason=FailedScheduling
LAST SEEN TYPE REASON OBJECT MESSAGE 10s Warning FailedScheduling pod/web-5bd8568956-lvj5d 0/4 nodes are available: 1 node(s) had untolerated taint(s), 1 node(s) were unschedulable, 2 node(s) didn't match pod topology spread constraints. preemption: 0/4 nodes are available: 2 No preemption victims found for incoming pod, 2 Preemption is not helpful for scheduling.
The fifth pod went to zone-a, making the count 3 against 2. The sixth would make it 4 against 2, a skew of 2, and the only zone-b node is cordoned, so every worker fails a filter. The event tallies each node once: the control plane for its taint, kx-worker3 for being unschedulable, and the two zone-a workers for the spread constraint. Zone-b still counts as a domain holding two pods even though it cannot take a third: a cordoned node keeps its pods, and with the default nodeTaintsPolicy, covered below, it keeps its place in the arithmetic. A PodDisruptionBudget never appears among these reasons: budgets limit evictions and play no part in scheduling.
That is the trade you choose per constraint. DoNotSchedule protects the property: if a zone fails or fills, extra replicas wait rather than concentrate in the survivors. ScheduleAnyway protects the replica count: pods keep starting, and the spread degrades. A common shape is the one above: a hard zone rule for a service that has capacity headroom in every zone, and a soft hostname rule so a small zone does not block scale-up.
minDomains
The scheduler knows only the domains that existing nodes belong to. If an autoscaled zone’s node group sits at zero nodes, that zone does not exist as far as the calculation goes, and a hard zone constraint across “all zones” quietly becomes a constraint across the zones that happen to have nodes. Pods pile into those zones and nothing ever asks for a node in the empty one.
minDomains, stable since Kubernetes 1.30, fixes the count of domains you expect. When fewer eligible domains exist, the global minimum is treated as zero, so each existing domain can hold at most maxSkew matching pods. The remaining replicas stay Pending, and a Pending pod is exactly the signal a node autoscaler acts on. Once enough domains exist, minDomains has no further effect.
topologySpreadConstraints:
- maxSkew: 1
minDomains: 3
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: web
minDomains is accepted only together with whenUnsatisfiable: DoNotSchedule; a scoring rule has no global minimum to override. Leaving it unset behaves as minDomains: 1.
matchLabelKeys
A rolling update runs two ReplicaSets at once, and both sets of pods match the Deployment’s app: web selector. Pods of the incoming revision are placed to balance the total across both revisions. When the rollout finishes and the outgoing pods are deleted, the incoming revision can be left badly skewed, because it was balancing against pods that no longer exist.
matchLabelKeys narrows the counted group by the incoming pod’s own label values. Listing pod-template-hash, the label the Deployment controller stamps on each revision’s pods, makes each revision spread on its own. In Kubernetes 1.34 and later, the API server reads those keys when the pod is created and merges the key-value pairs into the pod’s labelSelector, so kubectl get pod -o yaml shows the selector the scheduler really uses. The field is beta and enabled by default since Kubernetes 1.27.
Two rules apply. A key cannot appear in both matchLabelKeys and labelSelector, and matchLabelKeys requires a labelSelector. Avoid keys whose values someone edits directly on running pods: the merged selector is fixed at creation and does not follow the edit.
nodeAffinityPolicy and nodeTaintsPolicy
Not every node with the topology key should count as a domain. A pod restricted to two zones by node affinity should not be balanced against a third zone it can never use, and a pod that cannot tolerate the control-plane taint should not treat the control plane as an empty domain. Two fields, stable since Kubernetes 1.33, decide which nodes enter the calculation:
| Field | Honor | Ignore | Default |
|---|---|---|---|
nodeAffinityPolicy | Only nodes that match the pod’s nodeAffinity and nodeSelector count | All nodes count | Honor |
nodeTaintsPolicy | Only untainted nodes and nodes whose taints the pod tolerates count | All nodes count | Ignore |
The taint default surprises people on small clusters. Every kind node, the control plane included, carries kubernetes.io/hostname. Spread five replicas by hostname with maxSkew: 1:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
spec:
replicas: 5
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: api
containers:
- name: api
image: nginx:1.30
resources:
requests:
cpu: 100m
memory: 64Mi
kubectl apply -f api.yaml
deployment.apps/api created
sleep 15
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES api-5f486658c9-6x95w 1/1 Running 0 15s 10.244.2.2 kx-worker2 <none> <none> api-5f486658c9-crwtn 1/1 Running 0 15s 10.244.3.2 kx-worker3 <none> <none> api-5f486658c9-dq87d 1/1 Running 0 15s 10.244.1.3 kx-worker <none> <none> api-5f486658c9-nshlv 0/1 Pending 0 15s <none> <none> <none> <none> api-5f486658c9-pggpj 0/1 Pending 0 15s <none> <none> <none> <none>
kubectl get events --field-selector reason=FailedScheduling
LAST SEEN TYPE REASON OBJECT MESSAGE 9s Warning FailedScheduling pod/api-5f486658c9-nshlv 0/4 nodes are available: 1 node(s) had untolerated taint(s), 3 node(s) didn't match pod topology spread constraints. preemption: 0/4 nodes are available: 1 Preemption is not helpful for scheduling, 3 No preemption victims found for incoming pod. 9s Warning FailedScheduling pod/api-5f486658c9-pggpj 0/4 nodes are available: 1 node(s) had untolerated taint(s), 3 node(s) didn't match pod topology spread constraints. preemption: 0/4 nodes are available: 1 Preemption is not helpful for scheduling, 3 No preemption victims found for incoming pod.
The control plane is a domain with zero matching pods, so the global minimum is zero and each worker is capped at one. Adding nodeTaintsPolicy: Honor to the constraint drops the control plane from the calculation, the minimum becomes the lowest worker count, and all five replicas schedule. The same phantom-domain effect appears with any tainted node pool the workload does not tolerate, such as GPU nodes.
Defaults and drift
A pod that sets no constraints is not unconstrained. If the pod belongs to a Service, ReplicaSet, StatefulSet or ReplicationController, the scheduler applies cluster-level defaults. With no configuration it behaves as if every such pod carried two soft constraints: kubernetes.io/hostname with maxSkew: 3 and topology.kubernetes.io/zone with maxSkew: 5, both ScheduleAnyway. Administrators can replace them in the PodTopologySpread plugin arguments of a KubeSchedulerConfiguration profile, using defaultConstraints with an empty labelSelector (the selector comes from the owning object) and defaultingType: List. These defaults are weak by design: they nudge scoring and never block a pod.
The larger limitation is time. A spread constraint is checked when a pod is scheduled and never again. Several everyday events break the balance afterwards:
- Scale-down: the ReplicaSet controller chooses which pods to delete without consulting spread constraints.
- Node failure: replacements are placed wherever constraints allow at that moment, and the recovered node comes back empty.
- Rollouts without
matchLabelKeys: the incoming revision balances against outgoing pods that are about to disappear.
Nothing in the scheduler moves running pods. Restoring spread takes a fresh scheduling decision, and the usual source of one is the Descheduler, which evicts the minimum number of pods needed to bring each domain back within maxSkew. Its RemovePodsViolatingTopologySpreadConstraint plugin considers only DoNotSchedule constraints unless you list ScheduleAnyway as well. Evicted pods return to the scheduler, which places them under the same constraints.
kubectl --context kind-kx label node kx-worker kx-worker2 topology.kubernetes.io/zone=zone-akubectl --context kind-kx label node kx-worker3 topology.kubernetes.io/zone=zone-bkubectl --context kind-kx apply -f web.yamlkubectl --context kind-kx get pods -o wide -l app=webkubectl --context kind-kx get pod -l app=web -o jsonpath='{.items[0].spec.topologySpreadConstraints}'kubectl --context kind-kx delete deployment webkubectl --context kind-kx label node kx-worker kx-worker2 kx-worker3 topology.kubernetes.io/zone-
For your own kind cluster kx: copy a command and paste it into your terminal. Run in the simulator runs the same command in this page's simulated cluster instead; nothing on this page touches your machine. Save the web Deployment from this lesson as web.yaml first. The in-browser cluster accepts minDomains, matchLabelKeys, nodeAffinityPolicy and nodeTaintsPolicy but does not evaluate them, so try those fields here: the jsonpath query shows the pod-template-hash merged into each pod's selector.
Actual state
Check your understanding
Your first answer to each check is the one that counts. After a miss you can answer again; passing every check reconciles the lesson.
Lesson not yet reconciled
Desired: 4 objectives, 0 metReconciled: 4 of 4 objectives met
- Spread replicas across zones and nodes with maxSkew and topologyKey. (not yet met)
- Choose DoNotSchedule or ScheduleAnyway for each constraint. (not yet met)
- Use minDomains, matchLabelKeys and nodeTaintsPolicy to avoid skewed rollouts and phantom domains. (not yet met)
- Explain why spread holds only at scheduling time and what restores it. (not yet met)
Pass every check above to reconcile this lesson.
Every check has passed, so the desired state is met. First-try accuracy: 0 of 5. This lesson's review cards are in your queue.
Practice
Put this lesson to work on the simulated cluster. Everything runs in your browser, with nothing to install.
Changed since the notes
This lesson draws on an earlier set of study notes. Where the documentation contradicts a claim in them, qualifies it or has moved past it, the claim is listed here with the corrected statement and the source it was checked against.
Show 7 corrections
The notes saidKarpenter replaces Cluster Autoscaler and provisions nodes from pending pods
CorrectedTrue. Karpenter v1 APIs are stable: NodePool karpenter.sh/v1, EC2NodeClass karpenter.k8s.aws/v1. The latest AWS provider release is v1.15.2 (2026-11-12). On EKS Auto Mode, AWS runs Karpenter as a managed component and you configure only NodePools.
Sourcegithub.com/aws/karpenter-provider-aws/releases/tag/v1.15.2
The notes saidFargate has no node-level scheduling and each pod is isolated
CorrectedPods that match a Fargate profile get schedulerName: fargate-scheduler, a second scheduler that EKS runs. Each pod runs in its own VM and binds to its own fargate-* Node object, one pod per node. DaemonSets, privileged containers, hostNetwork and hostPort are not supported.
The notes saidThe default scheduler handles thousands of pods per minute
CorrectedThe Kubernetes docs give no throughput figure. Throughput depends on cluster size, plugins and percentageOfNodesToScore. That setting is adaptive: 50% at 100 nodes, falling to 10% at 5000 nodes, with a 5% floor.
Sourcekubernetes.io/docs/concepts/scheduling-eviction/scheduler-perf-tuning
The notes saidkubectl logs -n kube-system kube-scheduler-<node> shows scheduler logs on EKS
CorrectedEKS has no visible kube-scheduler pod. Enable the scheduler control plane log type and read it in CloudWatch Logs under /aws/eks/<cluster>/cluster. The kubectl command works only on self-managed clusters.
Sourcedocs.aws.amazon.com/eks/latest/userguide/control-plane-logs.html
The notes saidMultiple pods are scheduled in parallel through concurrent scheduling cycles
CorrectedScheduling cycles run serially, one pod at a time. Only binding cycles may run concurrently.
Sourcekubernetes.io/docs/concepts/scheduling-eviction/scheduling-framework
The notes saidPDB blocking is a reason pods won't schedule
CorrectedPDBs never block scheduling. They limit voluntary evictions. During preemption the scheduler respects PDBs only on a best-effort basis.
Sourcekubernetes.io/docs/concepts/scheduling-eviction/pod-priority-preemption
The notes saidRequired pod affinity filters out nodes that don't run the matching pods
CorrectedRequired pod affinity filters by topology domain: a node passes if any node in its topologyKey domain runs a matching pod. That equals 'nodes without those pods' only when topologyKey is kubernetes.io/hostname.
Sourcekubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node
Every correction across the guide, by note and topic.
Sources
Checked against kubernetes.io and AWS documentation for Kubernetes 1.37 on .
- kubernetes.io/docs/concepts/scheduling-eviction/topology-spread-constraints
- kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node
- kubernetes.io/docs/concepts/scheduling-eviction/scheduling-framework
- kubernetes.io/docs/concepts/scheduling-eviction/scheduler-perf-tuning
- kubernetes.io/docs/concepts/scheduling-eviction/pod-priority-preemption
- kubernetes.io/docs/reference/labels-annotations-taints
- github.com/kubernetes-sigs/descheduler
- karpenter.sh/docs/concepts/scheduling
- docs.aws.amazon.com/eks/latest/userguide/fargate.html
- docs.aws.amazon.com/eks/latest/userguide/fargate-profile.html
- docs.aws.amazon.com/eks/latest/userguide/control-plane-logs.html
- docs.aws.amazon.com/eks/latest/userguide/ebs-csi.html
Topology spread constraints
↓ ↑ step, 1 to 7 scene, Esc leave