DEV Community

Cover image for Pod Topology Spread Constraints
JackTT
JackTT

Posted on

Pod Topology Spread Constraints

Pod Topology Spread Constraints

Pod Topology Spread Constraints - a.k.a topologySpreadConstraints is a field of Pod.spec

It allows you to define how Pods should be spread across different topological domains, such as zones or nodes, to achieve better availability, fault tolerance, and resource utilization.

Main attributes

  • topologyKey: is a string representing a node label key. Kubernetes uses this key to identify and group nodes into topological domains.

    • Common values: kubernetes.io/hostname | topology.kubernetes.io/region
  • maxSkew: Specifies the maximum difference in the number of Pods across topological domains. A lower skew ensures a more even distribution.

  • labelSelector: is used to find matching Pods. Pods that match this label selector are counted to determine the number of Pods in their corresponding topology domain.

  • whenUnsatisfiable: indicates how to deal with a Pod if it doesn't satisfy the spread constraint.

    • DoNotSchedule (default) tells the scheduler not to schedule it.
    • ScheduleAnyway tells the scheduler to still schedule it while prioritizing nodes that minimize the skew.

Benefits of Using TSC

  • Improved Fault Tolerance: By spreading Pods across zones or nodes, the impact of a failure in one domain is minimized.

  • Better Resource Utilization: Ensures even distribution of workloads, preventing overloading specific domains.

  • Customizable Scheduling: Fine-tune how workloads are distributed based on application requirements.

Example

The following spec prefer spreading pods across nodes based on kubernetes.io/hostname.

topologySpreadConstraints:
- maxSkew: 1
  topologyKey: kubernetes.io/hostname
  whenUnsatisfiable: ScheduleAnyway
  labelSelector:
    matchLabels:
      app: hello-world
Enter fullscreen mode Exit fullscreen mode

Reference

https://kubernetes.io/docs/concepts/scheduling-eviction/topology-spread-constraints/

Top comments (0)