Search code examples
kuberneteskubernetes-helmkubernetes-pod

Kubernetes pod affinity - Scheduling pods on different nodes


We are running our application with 3 pods on a 3 node kubernetes cluster. When we deploy out application, sometimes, pods are getting scheduled to the same kubernetes node.

We want our pods to scheduled in such a way that it spread our pods across nodes ( no 2 pods of the same application should be same node). Infact, as per documentation(https://kubernetes.io/docs/concepts/configuration/assign-pod-node/), kubernetes already does a good job in this. However, if it doesn't find resources, it schedules it to the same node. How do it make it a hard constraint ?

Requirement: We want the deployment to fail or be in pending state if the pods don't obey the constraints (no 2 pods of the same application should be same node)


Solution

  • i think this one will work

    affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - <VALUE>
            topologyKey: "kubernetes.io/hostname"
    

    For more reference you can visit : https://thenewstack.io/implement-node-and-pod-affinity-anti-affinity-in-kubernetes-a-practical-example/