<p>We are finding that our Kubernetes cluster tends to have hot-spots where certain nodes get far more instances of our apps than other nodes.</p>
<p>In this case, we are deploying lots of instances of Apache Airflow, and some nodes have 3x more web or scheduler components than others.</p>
<p>Is it possible to use anti-affinity rules to force a more even spread of pods across the cluster?</p>
<p><strong>E.g. &quot;prefer the node with the least pods of label <code>component=airflow-web</code>?&quot;</strong></p>
<p>If anti-affinity does not work, are there other mechanisms we should be looking into as well?</p>

John Humphreys
<p>Try adding this to the Deployment/StatefulSet <code>.spec.template</code>:</p>
<pre><code>      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: &quot;component&quot;
                  operator: In
                  values:
                  - airflow-web
              topologyKey: &quot;kubernetes.io/hostname&quot;
</code></pre>
