DEV Community

Cover image for Pod Topology Spread Constraints
JackTT
JackTT

Posted on

1

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/

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free