Topology spread, priority and preemption

Advanced · 10 min

Spreading replicas evenly across zones and nodes with topology spread constraints, and how PriorityClasses let important Pods preempt others.

Before thisNode selectors and affinityTaints and tolerations

Lukonde Mwila · 10 min

Topology spread, priority and preemption

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Lukonde MwilaReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A topology spread constraint caps the skew: the difference in matching Pods between the most and least loaded domains (zones, nodes) may not exceed maxSkew.
  • whenUnsatisfiable DoNotSchedule makes the rule a filter that can leave Pods Pending. ScheduleAnyway makes it a scoring preference. Combine a hard zone rule with a soft node rule.
  • Spread is checked only at scheduling time, against current Pods, so later deletions and scale-downs can unbalance a workload until something reschedules it.
  • A PriorityClass gives Pods an integer priority. Higher-priority Pods are queued first, and when none fit, the scheduler evicts lower-priority Pods to make room.
  • Any PriorityClass above zero outranks the default priority of 0. preemptionPolicy Never gives queue priority without evicting anyone, and preemption honours PodDisruptionBudgets only on a best-effort basis.

Two questions a full cluster raises

A Deployment with six replicas is only highly available if those replicas aren’t all in one zone. The default scheduler nudges them apart, but a nudge isn’t a guarantee, and pod anti-affinity is too blunt: one per node at most, then Pending. What you actually want is “roughly even, never lopsided by more than one”.

The second question comes when the cluster is full. A checkout service needs to scale up during a sale, and every node is busy running nightly batch jobs. Waiting for the autoscaler takes minutes. Kubernetes lets you say which Pods matter more, and lets the important ones take the space.

Topology spread constraints

spec:
  topologySpreadConstraints:
  - maxSkew: 1
    topologyKey: topology.kubernetes.io/zone
    whenUnsatisfiable: DoNotSchedule
    labelSelector: {matchLabels: {app: api}}
  - maxSkew: 1
    topologyKey: kubernetes.io/hostname
    whenUnsatisfiable: ScheduleAnyway
    labelSelector: {matchLabels: {app: api}}

topologyKey groups nodes into domains, here zones and then individual nodes. The scheduler counts Pods matching labelSelector in each domain, and skew is the count in a candidate domain after placement minus the count in the emptiest domain. maxSkew: 1 means never more than one ahead.

whenUnsatisfiable decides how hard the rule is. DoNotSchedule is a filter: a node whose domain would break the skew is rejected. ScheduleAnyway is a score: it prefers less-skewed placements without blocking. The pair above, hard by zone and soft by node, is the common pattern. Think of seating guests at a wedding: the rule that each side of the aisle has about the same number is firm, and the wish that tables are even is a preference.

On our lab cluster with four fake nodes, two in eu-a and two in eu-b, six such Pods landed three per zone. Then we cordoned both eu-b nodes and added two more. The seventh went to eu-a (skew 4 against 3, allowed). The eighth was refused:

0/4 nodes are available: 2 node(s) didn't match pod topology spread constraints, 2 node(s) were unschedulable.

Putting it in eu-a would make the skew 2. When we uncordoned eu-b, it was scheduled to n4 within seconds, as the queue retried on the node event.

A few fields tune the behaviour. minDomains counts missing domains as empty, so a three-zone rule holds even while only two zones have nodes (useful with autoscalers). nodeAffinityPolicy and nodeTaintsPolicy (GA in 1.33) decide whether nodes the Pod can’t use anyway count as domains. matchLabelKeys: [pod-template-hash] makes each ReplicaSet spread on its own during a rollout.

Two limits. Spread is checked when each Pod is scheduled, against the Pods that exist then. Scale down, lose a node, or delete Pods, and the remaining set can be lopsided, and nothing moves them back. The descheduler project exists to fix that. And without explicit constraints, the scheduler’s PodTopologySpread plugin applies default soft constraints by hostname and zone to Pods owned by Services, ReplicaSets and StatefulSets. That’s the nudge you get for free.

PriorityClasses

apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata: {name: prod-high}
value: 100000
description: "Customer-facing services"

A Pod sets priorityClassName: prod-high and admission resolves it to spec.priority: 100000. Higher priority Pods are taken from the scheduling queue first, and if none of the nodes fit them, the DefaultPreemption plugin looks for lower-priority Pods to evict. Two system classes exist, system-cluster-critical (2,000,000,000) and system-node-critical, for things like CoreDNS and node agents. Pods with no class get the globalDefault class if one is defined, otherwise 0.

That last point surprised us. We created a batch-low class with value 100 and launched twelve 1-CPU batch Pods into a cluster already running our eight api Pods with no class. The batch Pods didn’t fit, so the scheduler preempted api Pods: api-1 Preempted by pod ... on node n2, six of them in all. Any class above zero outranks every Pod without one. Introducing a “low” class for batch work makes batch work more important than everything you haven’t classified. Classify production services first, or set a globalDefault.

Preemption, step by step

Then we created checkout-1 with prod-high and a 2-CPU request. The scheduler picked n1, chose victims there (lowest priority first, the fewest that free enough room), deleted them with their normal grace period, and set status.nominatedNodeName: n1 on checkout-1. The victims got Preempted events and a deletion timestamp. The preemptor isn’t bound until the space is actually free. In the lab, with no kubelet to finish terminating the victims, it stayed nominated until we force-deleted them, then bound to n1 at once.

preemptionPolicy: Never gives a class queue priority without the right to evict. Our high-no-preempt Pod stayed Pending with preemption: not eligible due to preemptionPolicy=Never. That suits important batch jobs that should go first but never kill anything.

Preemption and PodDisruptionBudgets interact only loosely: the scheduler prefers victims whose PDBs allow the disruption, but it will violate a PDB if there’s no other way. It’s not an eviction through the eviction API. Workload-aware preemption, beta in 1.37, extends this to whole PodGroups for gang-scheduled jobs.

About the video

The embed is Lukonde Mwila’s “Pod Topology Spread Constraints in Kubernetes”, January 2023, 10 minutes. Its chapters cover spreading applications across topology domains, using pod anti-affinity for availability and its limits, configuring topology spread constraints, and a demo with Karpenter provisioning nodes into the right zones. It doesn’t cover priority or preemption. KodeKloud’s 2025 video on PriorityClasses and preemption policies is first in deeper, and Piyush’s lab observing preemption second. Judged from chapters, since transcripts can’t be fetched here. The embed has no audio-track flag and has English captions. KodeKloud reports an “English (US) original” track.

What was run: against a local v1.37.3 control plane with four fake Nodes in two zones, kept alive by a script renewing their Leases, we placed spread-constrained Pods, cordoned a zone and read the spread refusal, uncordoned and watched the retry, created three PriorityClasses, watched batch Pods preempt unclassified ones, watched a high-priority Pod nominate a node, preempt the lowest-priority victims and bind once they were gone, and saw preemptionPolicy: Never refuse to preempt. No containers ran, checked 2026-11-29.

Where this fits

Preemption is one kind of involuntary disruption. Node drains, upgrades and autoscaler scale-downs are the planned kind. Next: disruptions, drains and PodDisruptionBudgets.

Check yourself

  1. 01Three zones hold 3, 3 and 1 replicas of a Deployment with maxSkew 1 by zone and DoNotSchedule. Where can the next replica go?

    Only the zone with 1. Putting it in a zone with 3 would make the skew 4 minus 1 equals 3. If that zone has no room, the Pod stays Pending with 'didn't match pod topology spread constraints'.

  2. 02Why do people pair a DoNotSchedule zone constraint with a ScheduleAnyway hostname constraint?

    Zone balance is the availability requirement, so it's hard. Node balance is nice to have; making it hard too could leave Pods Pending whenever node counts are uneven, so it's a scoring preference.

  3. 03You add a PriorityClass with value 100 for batch jobs. The next morning several web Pods have been evicted, though nobody touched them. What happened?

    The web Pods had no priority class, so priority 0. When the batch Pods didn't fit, the scheduler preempted lower-priority Pods, and 0 is lower than 100. Give production services a higher class, or make the batch class preemptionPolicy Never.

  4. 04A high-priority Pod shows nominatedNodeName n1 but isn't bound yet. What is it waiting for?

    The victims it preempted on n1 to finish terminating, within their grace periods. The scheduler reserves n1 for it with the nomination and binds once the space is really free; meanwhile other, lower-priority Pods can't take that space.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark