How to set topology spread constraints for AI agent replicas

Add a topologySpreadConstraints entry to each agent pod spec to control how replicas spread across failure domains such as zones and nodes. Set topologyKey to the node label that defines a domain, maxSkew to the largest imbalance you accept, and whenUnsatisfiable to decide whether a replica waits or schedules anyway.
Based on the Kubernetes Pod Topology Spread Constraints page, fetched 2026-11-26 (page last modified October 27, 2025). No cluster test was run for this post.
Why agent replicas need a spread rule
The Kubernetes docs use a simple case to explain the field. With two replicas, you would prefer that they do not share a node, because a single node failure could take the whole workload offline. As the replica count grows, a different concern appears. If clients sit in three zones, you want a similar number of replicas in each zone, which keeps latency and cross-zone network costs down. You also want the cluster to recover on its own when one zone has a problem.
The same logic applies to a pool of agent workers. A spread rule states the distribution you want, and the scheduler works toward it each time it places a new replica. With a workload controller such as a Deployment, you declare the rule once in the pod template, and the page says the template applies it to the pods.
The topologySpreadConstraints field
The field sits at spec.topologySpreadConstraints in the Pod API. Each entry defines one constraint, and you can list several. The manifest below is the page’s one-constraint example. The page’s short zone key is changed to topology.kubernetes.io/zone, which the page recommends. The page’s pause container stands in for your agent container.
kind: Pod
apiVersion: v1
metadata:
name: mypod
labels:
foo: bar
spec:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
foo: bar
containers:
- name: pause
image: registry.k8s.io/pause:3.1
The labelSelector decides which existing pods are counted in each domain. In this example only pods labeled foo: bar count. Use the labels your agent pods actually carry.
Field reference
| Field | What the page says |
|---|---|
maxSkew |
You must set it, and it must be greater than zero. Its meaning depends on whenUnsatisfiable. |
topologyKey |
A node label key. Nodes with the same key and value form one domain. |
whenUnsatisfiable |
DoNotSchedule (the default) or ScheduleAnyway. |
labelSelector |
Selects the pods counted in each domain. |
minDomains |
Optional. Must be greater than 0. Allowed only with DoNotSchedule. |
matchLabelKeys |
Optional. Cannot be set unless labelSelector is set. |
nodeAffinityPolicy |
Optional. Honor or Ignore. A null value behaves as Honor. |
nodeTaintsPolicy |
Optional. Honor or Ignore. A null value behaves as Ignore. |
Set maxSkew and whenUnsatisfiable
With DoNotSchedule, maxSkew is the largest permitted difference between the number of matching pods in the target domain and the global minimum. The page’s example has three zones with 2, 2 and 1 matching pods. With maxSkew: 1, the global minimum is 1, so placing a new pod in a zone that already holds two matching pods would exceed the limit.
With ScheduleAnyway, maxSkew does not block placement. The scheduler gives higher precedence to the topologies that would reduce the skew, and the replica still runs. The page also notes that this preference is combined with other internal scheduling priorities, such as resource usage ratio.
whenUnsatisfiable |
What happens to a replica that breaks the constraint | Choose it when |
|---|---|---|
DoNotSchedule (default) |
The pod stays Pending. | You would rather hold a replica than run it unevenly. |
ScheduleAnyway |
The pod schedules, and nodes that reduce skew are preferred. | Every replica must run, even if the spread is uneven. |
Steps to spread agent replicas across zones and nodes
- Confirm that the nodes carry the label key you plan to use. The page recommends
topology.kubernetes.io/zoneandtopology.kubernetes.io/region. It also says most clusters populatekubernetes.io/hostnameautomatically, and tells you to check whether yours does. - Choose a
topologyKeyfor each constraint. Use the zone key to spread across zones, andkubernetes.io/hostnameto spread across nodes. - Set
labelSelectorto the labels your agent replicas carry. Pods that do not match it are not counted. - Confirm that each agent pod matches its own selector. The page warns that a pod whose labels do not match its selector does not count itself, which produces “ghost pods” that can pile up on one topology.
- Set
maxSkewto a number greater than zero. The page’s examples use 1 for an even spread. - Set
whenUnsatisfiable. Keep the defaultDoNotScheduleif a waiting replica is acceptable, or useScheduleAnywayif every replica must run. - For zone and node spread together, add a second constraint. The page says there can be only one constraint for each
topologyKeyandwhenUnsatisfiablepair, so two entries that share atopologyKeymust use differentwhenUnsatisfiablevalues. Entries with different keys, as below, do not hit that rule. - Put the constraints in the Deployment pod template, so every replica carries the same block. The page says to set the same constraints on all pods in a group.
Combine zone and node spread
spec:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
foo: bar
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
foo: bar
When a pod defines more than one constraint, they are combined with a logical AND. The scheduler only accepts a node that satisfies all of them.
Keep the spread across rollouts with matchLabelKeys
matchLabelKeys lets the spread tell revisions of the same Deployment apart. The page’s example uses pod-template-hash, which the Deployment controller adds automatically. The page calls the field beta, enabled by default in 1.27, and advises against using it with labels that might be edited directly on pods.
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: foo
matchLabelKeys:
- pod-template-hash
Check for Pending replicas
A replica stays Pending when its constraints cannot all be met. The page’s conflict example has three nodes across two zones. One constraint allows the pod only in zone B, and a second constraint allows it only on one node in zone A. Because the two sets do not overlap, the scheduler cannot place the pod.
The page gives three fixes: raise maxSkew, change one constraint to ScheduleAnyway, or delete an existing pod manually. Manual deletion is the page’s example for a bug-fix rollout that is not making progress. Check the pending replica’s constraints before you edit anything else.
Spread constraints or pod anti-affinity
The page compares the two features directly. Pod anti-affinity in the required mode allows only one pod in each topology domain. In the preferred mode, the constraint can no longer be enforced. Topology spread constraints give finer control. They distribute pods across domains, and maxSkew sets how uneven the spread may be. The page says this helps with high availability, cost saving, rolling updates and smooth scale-out.
| Setting | Effect on agent replicas |
|---|---|
podAntiAffinity, required mode |
Allows a single pod per topology domain. |
podAntiAffinity, preferred mode |
Prefers separation, but the separation cannot be enforced. |
topologySpreadConstraints |
Sets the allowed imbalance with maxSkew, and lets you choose between waiting and scheduling anyway. |
Gotchas from the docs
- A node that lacks any of the constraint’s
topologyKeyvalues is bypassed. Pods on that node do not affect the skew calculation, and new replicas are not placed there. - Only pods in the same namespace as the incoming pod are matching candidates.
- The constraints are not guaranteed to hold when pods are removed. Scaling a Deployment down can leave an uneven spread, and the page names the Descheduler as a way to rebalance.
- The scheduler learns about domains from existing nodes. A node pool scaled to zero therefore hides its zone until a node exists there. The page suggests a node autoscaler that is aware of topology spread constraints.
- If a pod defines no constraints and belongs to a Service, ReplicaSet, StatefulSet or ReplicationController, cluster-level defaults apply. With no defaults configured, the scheduler acts as if the following were set.
defaultConstraints:
- maxSkew: 3
topologyKey: "kubernetes.io/hostname"
whenUnsatisfiable: ScheduleAnyway
- maxSkew: 5
topologyKey: "topology.kubernetes.io/zone"
whenUnsatisfiable: ScheduleAnyway
The page says these defaults do not apply to pods that define their own constraints. To turn them off, it shows setting defaultingType to List with empty defaultConstraints in the PodTopologySpread plugin arguments of a scheduling profile. If you rely on zone spread, set your own constraints on the agent pods rather than depending on the built-in defaults.
Where to go next
Topology spread decides where new replicas are placed. It does not change how many resources a replica may use. For that, read how to set resource limits on AI agent pods. For how the scheduler orders pending pods and preempts lower-priority ones, read how to set pod priority for AI agent workloads in Kubernetes.
For the wider view of governing agents, start with the governing AI agents hub. For Nirmata’s own product in this area, see the AIControls platform page.
Frequently asked questions
Does a topology spread constraint stop two agent replicas from landing on one node?
Only if the constraint uses a node-level key. The Kubernetes docs give the case of two pods that should not share a node, because one node failure could take the workload offline. Set topologyKey to kubernetes.io/hostname and maxSkew to 1. The number of matching pods on the target node can then exceed the global minimum by at most 1. The default whenUnsatisfiable value, DoNotSchedule, keeps a replica Pending instead of breaking that limit.
What does maxSkew mean for agent replicas?
It is the largest allowed difference in matching pods between a topology domain and the global minimum. With DoNotSchedule it is a hard limit, so a replica that would exceed it stays Pending. With ScheduleAnyway the replica still runs, and the scheduler prefers nodes that reduce the skew. maxSkew must be greater than zero, and the page's example uses 1 for an even spread.
Will a topology spread constraint rebalance agent pods after a scale-down?
No guarantee is given. The page says the constraints are not guaranteed to stay satisfied when pods are removed, and that scaling a Deployment down can leave pods unevenly spread. It names the Descheduler as one way to rebalance. If zone balance matters for your agents, check the distribution after you scale down.
Do my nodes need topology labels for the constraint to work?
Yes. Spread constraints read node labels to find each node's domain, and nodes that lack the topologyKey are bypassed. The page recommends the registered keys topology.kubernetes.io/zone and topology.kubernetes.io/region over short keys such as zone. Most clusters populate kubernetes.io/hostname automatically, but the page says to check whether your cluster does.