HA in azure kubernetes using Podantiaffinity

arsh 0 Reputation points
2023-11-30T16:47:30.8466667+00:00

Hello,

To achieve HA for my application (3 replicas) do I need to use the podantiaffinity as preferredDuringSchedulingIgnoredDuringExecution or requiredDuringSchedulingIgnoredDuringExecution? Also if I go with topologykey = zone can I do a new deployment? we are using availability zone1, zone2 and zone3

preferredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: app.kubernetes.io/name
                    operator: In
                    values:
                      - abcd
              topologyKey: topology.kubernetes.io/zone
Azure Kubernetes Service
Azure Kubernetes Service
An Azure service that provides serverless Kubernetes, an integrated continuous integration and continuous delivery experience, and enterprise-grade security and governance.
2,456 questions
{count} votes

1 answer

Sort by: Most helpful
  1. shiva patpi 13,366 Reputation points Microsoft Employee Moderator
    2023-11-30T18:09:33.9533333+00:00

    @arsh ,

    That is correct! There is one more feature called "topology spread constraints" - that should also help out in evenly distributing your pods managed by deployment object on the nodes!

    Take a look at the below article how to implement that:

    https://www.danielstechblog.io/distribute-your-application-across-different-availability-zones-in-aks-using-pod-topology-spread-constraints/

    Kubernetes official documentation about that feature:

    https://kubernetes.io/docs/concepts/scheduling-eviction/topology-spread-constraints/

    I just tested with below Sample YAML file:

    with 3 replicas:

    User's image

    scaled to 6 replicas:(Distributed evenly on all the nodes)

    User's image

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          topologySpreadConstraints:
            - maxSkew: 1
              topologyKey: kubernetes.io/hostname
              whenUnsatisfiable: ScheduleAnyway
              labelSelector:
                matchLabels:
                  app: nginx
          containers:
          - name: nginx
            image: nginx
    
    																							
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.