How to guarantee even spread pod distribution across AZ in Kubernetes StatefulSet

Viewed 530

I deployed a K8s StatefulSet with 30 replicas (or N replicas, where N is multiple of 3) in EKS Cluster.

EKS cluster is with 3 nodes, one node for one AZ, and I want to guarantee with Kubernetes Affinity/AntiAffinity the equal distribution of pods across different AZ.

us-west-2a (n nodes) -> N/3 pods
us-west-2b (m nodes) -> N/3 pods
us-west-2c (o nodes) -> N/3 pods

Thanks

2 Answers

While this is too possible with node affinity, a straight forward way is the use of topologySpreadContraints, here's the k8s documentation, diagrams and examples to see it in action.

You can always use selectors and default labels which you get from AWS. A simple pod spec example is here

topologySpreadConstraints:
  - maxSkew: 1
    topologyKey: "topology.kubernetes.io/zone"
    whenUnsatisfiable: DoNotSchedule
    labelSelector:
      matchLabels:
        app: myapp

You can include skew and other options based on the need as described here: https://kubernetes.io/docs/concepts/workloads/pods/pod-topology-spread-constraints/

Related