⎈ k8s knowledge compiler

Pod Topology Spread Constraints [page]deterministic

concepts

You can use _topology spread constraints_ to control how [Pods](#gloss:Pod) are spread across your cluster among failure-domains such as regions, zones, nodes, and other user-defined topology domains. This can help to achieve high availability as well as efficient resource utilization.

You can set [cluster-level constraints](#cluster-level-default-constraints) as a default, or configure topology spread constraints for individual workloads.

## Motivation

Imagine that you have a cluster of up to twenty nodes, and you want to run a [workload](#gloss:workload) that automatically scales how many replicas it uses. There could be as few as two Pods or as many as fifteen. When there are only two Pods, you'd prefer not to have both of those Pods run on the same node: you would run the risk that a single node failure takes your workload offline.

In addition to this basic usage, there are some advanced usage examples that enable your workloads to benefit on high availability and cluster utilization.

As you scale up and run more Pods, a different concern becomes important. Imagine that you have three nodes running five Pods each. The nodes have enough capacity to run that many replicas; however, the clients that interact with this workload are split across three different datacenters (or infrastructure zones). Now you have less concern about a single node failure, but you notice that latency is higher than you'd like, and you are paying for network costs associated with sending network traffic between the different zones.

You decide that under normal operation you'd prefer to have a similar number of replicas [scheduled](/docs/concepts/scheduling-eviction/) into each infrastructure zone, and you'd like the cluster to self-heal in the case that there is a problem.

Pod topology spread constraints offer you a declarative way to configure that.

## `topologySpreadConstraints` field

The Pod API includes a field, `spec.topologySpreadConstraints`. The usage of this field looks like the following:

```yaml --- apiVersion: v1 kind: Pod metadata: name: example-pod spec: # Configure a topology spread constraint topologySpreadConstraints: - maxSkew: <integer> minDomains: <integer> # optional topologyKey: <string> whenUnsatisfiable: <string> labelSelector: <object> matchLabelKeys: <list> # optional; beta since v1.27 nodeAffinityPolicy: [Honor|Ignore] # optional; beta since v1.26 nodeTaintsPolicy: [Honor|Ignore] # optional; beta since v1.26 ### other Pod fields go here ```

> Note: There can only be one `topologySpreadConstraint` for a given `topologyKey` and `whenUnsatisfiable` value. For example, if you have defined a `topologySpreadConstraint` that uses the `topologyKey` "kubernetes.io/hostname" and `whenUnsatisfiable` value "DoNotSchedule", you can only add another `topologySpreadConstraint` for the `topologyKey` "kubernetes.io/hostname" if you use a different `whenUnsatisfiable` value.

You can read more about this field by running `kubectl explain Pod.spec.topologySpreadConstraints` or refer to the [scheduling](/docs/reference/kubernetes-api/workload-resources/pod-v1/#scheduling) section of the API reference for Pod.

### Spread constraint definition

You can define one or multiple `topologySpreadConstraints` entries to instruct the kube-scheduler how to place each incoming Pod in relation to the existing Pods across your cluster. Those fields are:

  • maxSkew describes the degree to which Pods may be unevenly distributed. You must specify this field and the number must be greater than zero. Its semantics differ according to the value of `whenUnsatisfiable`:
  • if you select `whenUnsatisfiable: DoNotSchedule`, then `maxSkew` defines the maximum permitted difference between the number of matching pods in the target topology and the _global minimum_ (the minimum number of matching pods in an eligible domain or zero if the number of eligible domains is less …(trimmed)

Sources

concepts/scheduling-eviction/topology-spread-constraints.md · docPod Topology Spread Constraints

Related (19)

references Workloadworkload conf=1
references Nodenode conf=1
part_of Motivationdescribes conf=1
part_of `topologySpreadConstraints` fielddescribes conf=1
part_of Consistencydescribes conf=1
part_of Topology spread constraint examplesdescribes conf=1
part_of Implicit conventionsdescribes conf=1
part_of Cluster-level default constraintsdescribes conf=1
part_of Known limitationsdescribes conf=1
part_of {{% heading "whatsnext" %}}describes conf=1
part_of Spread constraint definitiondescribes conf=1
part_of Node labelsdescribes conf=1
api_for Poddocuments API object conf=1

← all Docs