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/

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay