how to assign affinity to kubernetes pods for distributing the pods across all ads?

8/5/2020

What rules should be used to assign affinity to Kubernetes pods for distributing the pods across all Availability Zones? I have a region with 3 Availability Zones and Nodes in each of these. I want to make sure that each of the 3 pods are spread across all the 3 Availability Zones.

-- user4202236
kubernetes

1 Answer

8/5/2020

You should be able to use the label topology.kubernetes.io/zone (for e.g. topologyKey) and add anti-affinity rules.

This is part of the anti-affinity example:

    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        podAffinityTerm:
          labelSelector:
            matchExpressions:
            - key: security
              operator: In
              values:
              - S2
          topologyKey: failure-domain.beta.kubernetes.io/zone

the result of the example is documented as

The pod anti-affinity rule says that the pod cannot be scheduled onto a node if that node is in the same zone as a pod with label having key "security" and value "S2".

Instead of the label security in the example, you can use e.g. app-name: <your-app-name> as label and use that in your matchExpression.

-- Jonas
Source: StackOverflow