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

12/6/2021

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

-- falberto
amazon-eks
kubernetes
statefulset

2 Answers

12/6/2021

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/

-- user2039152
Source: StackOverflow

12/6/2021

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.

-- gohm'c
Source: StackOverflow