Prefer dispatch replicas among nodes

10/30/2017

I'm running a Kubernetes cluster of 3 nodes with GKE. I ask Kubernetes for 3 replicas of backend pods. The 3 pods are not well dispatched among the nodes to provide a high-availability service, they are usually all on 2 nodes. I would like Kubernetes to dispatch the pods as much as possible to have a pod on each node, but not fail the deployment/scale-up if they are more backend pods than nodes.

Is it possible to do that with preferredDuringSchedulingIgnoredDuringExecution?

-- Sony
google-cloud-platform
google-kubernetes-engine
high-availability
kubernetes

1 Answer

10/31/2017

Try setting up an preferred antiAffinity rule like so:

affinity:
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
        - labelSelector:
            matchExpressions:
              - key: "app"
                operator: In
                values: 
                - "my_app_name"
          topologyKey: "kubernetes.io/hostname"

This will try to schedule pods onto nodes which do not already have a pod of the same label running on them. After that it's a free for all (so it won't evenly spread them after making sure at least 1 is running on each node). This means that after scaling up you might end up with a node with 5 pods, and other nodes with 1 pod each.

-- vascop
Source: StackOverflow