0

I've got an on-premise cluster with 1 master and 3 workers. Now there's a deployment with 3 replica's and they MUST be evenly distributed over all the nodes.

Now we can use "topologySpreadConstraints" on kubernetes/hostName label for that but it doesn't always work.

apiVersion: apps/v1
kind: Deployment
metadata:
 name: revproxy
 labels:
  app: proxy
spec:
 replicas: 3
 selector:
   matchLabels:
     role: proxy
 template:
   metadata:
     labels:
       role: proxy
   spec:
     topologySpreadConstraints:
     - maxSkew: 1
       topologyKey: kubernetes.io/hostname
       whenUnsatisfiable: ScheduleAnyway
       labelSelector:
         matchLabels:
           app: revproxy

There's many times 2 pods running on the same node and 1 pod on another and zero pods on the 3rd. But when manually deleting a second pod then it ALWAYS gets scheduled on the empty node.

How can I use topologySpreadConstraints to have pods evenly distributed over the nodes?

1

Found the answer.

When the deployment has a yaml with the following labels.

kind: Deployment
metadata:
 name: revproxy
 labels:
  app: proxy
spec:
 selector:
   matchLabels:
     role: proxy
 template:
   metadata:
     labels:
       role: proxy

The matchLabels in topologySpreadConstraints with matchLabels 'revproxy' is wrong.

topologySpreadConstraints:
     - maxSkew: 1
       topologyKey: kubernetes.io/hostname
       whenUnsatisfiable: ScheduleAnyway
       labelSelector:
         matchLabels:
           app: revproxy

When pointing to just 'proxy' in the labelSelector the pods get distributed over the 3 nodes evenly.

     matchLabels:
       app: proxy

Am still confused about the 3x the same labels in a description, changing any of these breaks the deployment, but at least it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.