<p>In Kubernetes, are <code>podAffinity</code> and <code>podAntiAffinity</code> weights compared to each other? Or independently? What about <code>podAffinity</code> and <code>nodeAffinity</code>? Would <code>podAntiAffinity</code> outweight the <code>podAffinity</code> in the below example? And what if <code>nodeAffinity</code> was added to the mix as well.</p>
<pre><code> affinity:
  podAffinity:
    preferredDuringSchedulingIgnoredDuringExecution:
    - weight: 50
      podAffinityTerm:
        labelSelector:
          matchExpressions:
          - key: app.kubernetes.io/name
            operator: In
            values:
            - test-1
        topologyKey: kubernetes.io/hostname

  podAntiAffinity:
    preferredDuringSchedulingIgnoredDuringExecution:
    - weight: 100
      podAffinityTerm:
        labelSelector:
          matchExpressions:
          - key: app.kubernetes.io/name
            operator: In
            values:
            - test-2          
        topologyKey: kubernetes.io/hostname
</code></pre>

<p>All affinity settings for both pods and nodes are weighed the same way if you don't overwrite the default individual weight of 1.</p>
<p>During execution the occurrence of affinity conditions on a node is multiplied by the weight of the condition.</p>
<pre><code>pod a has an affinity to b
pod a has an anti-affinity to c

new pod of type &quot;a&quot; to be scheduled
---
node1 runs 3 &quot;b&quot; pods and 1 &quot;c&quot; pod --&gt; affinity score: 2 (3 &quot;b&quot; pods - 1 &quot;c&quot; pod)
node2 runs 1 &quot;b&quot; pod --&gt; affinity score 1: 1 (1 &quot;b&quot; pod)
</code></pre>
<p>Based on the affinity calculation the pod will be scheduled on node1 despite it already running pod &quot;c&quot; as 3 &quot;b&quot; pods outweigh 1 &quot;c&quot; pod.</p>
<p>In your example the anti-affinity would outweigh the affinity settings if there is a 1:1 ratio of <code>test-1</code> to <code>test-2</code> on a node. With a 2:1 ratio of <code>test-1</code> to <code>test-2</code> on a node the (anti-)affinities would cancel each other out.</p>
<p><a href="https://github.com/kubernetes/kubernetes/blob/a5489431cfc0598dad421fccd2d713f84bf520bd/pkg/scheduler/framework/plugins/interpodaffinity/scoring.go#L79" rel="nofollow noreferrer">Source</a></p>
