<p>Found the answer.</p>
<p>When the deployment has a yaml with the following labels.</p>
<pre><code>kind: Deployment
metadata:
 name: revproxy
 labels:
  app: proxy
spec:
 selector:
   matchLabels:
     role: proxy
 template:
   metadata:
     labels:
       role: proxy
</code></pre>
<p>The matchLabels in topologySpreadConstraints with matchLabels 'revproxy' is wrong.</p>
<pre><code>topologySpreadConstraints:
     - maxSkew: 1
       topologyKey: kubernetes.io/hostname
       whenUnsatisfiable: ScheduleAnyway
       labelSelector:
         matchLabels:
           app: revproxy
</code></pre>
<p>When pointing to just 'proxy' in the labelSelector the pods get distributed over the 3 nodes evenly.</p>
<pre><code>     matchLabels:
       app: proxy
</code></pre>
<p>Am still confused about the 3x the same labels in a description, changing any of these breaks the deployment, but at least it works.</p>

CC BY-SA 4.0
2021-11-09T06:29:08.787Z
2021-11-09T06:29:08.787Z
Serve Laurijssen
<p>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.</p>
<p>Now we can use &quot;topologySpreadConstraints&quot; on kubernetes/hostName label for that but it doesn't always work.</p>
<pre><code>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
</code></pre>
<p>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.</p>
<p>How can I use topologySpreadConstraints to have pods evenly distributed over the nodes?</p>

<kubernetes><azure-kubernetes-service>
evenly distribute pods over nodes
Serve Laurijssen
# evenly distribute pods over nodes

(Asked by: username_0 on 2021-11-08)

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?

**Tags:** kubernetes, azure-kubernetes-service


---

## 1 answers

### Answer by username_0 on 2021-11-09. Score: 1 | Accepted answer

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.


---

