Search code examples
kubernetesamazon-eksstatefulset

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


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


Solution

  • 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/