<p>I want that my api deployment pods will be spread to the whole cluster's nodes.
So I came up with this:</p>
<pre><code>spec:
  affinity:
    podAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values:
            - api
        topologyKey: &quot;kubernetes.io/hostname&quot;
</code></pre>
<p>But this allows exactly one pod in each node, and no more.
My problem is when I want to rollout an update, kubernetes remains the new creating pod under &quot;pending&quot; state.</p>
<p>How can I change the <code>requiredDuringSchedulingIgnoredDuringExecution</code> to <code>preferredDuringSchedulingIgnoredDuringExecution</code>?</p>
<p>I have tried, but I got many errors since the <code>preferredDuringSchedulingIgnoredDuringExecution</code> probably requires different configurations from the <code>requiredDuringSchedulingIgnoredDuringExecution</code>.</p>

<p>This is the right implementation:</p>
<pre><code>  affinity:
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution: 
      - weight: 100  
        podAffinityTerm:
          labelSelector:
            matchExpressions:
            - key: app 
              operator: In 
              values:
              - api
          topologyKey: kubernetes.io/hostname
</code></pre>
<p>This will spread the pods evenly in the nodes and will allow more than one in each node. So basically you can deploy 6 replicas to cluster of 3 nodes without a problem. Also, you can rollout an update even though it creates a new extra pod before shutting down the old one.</p>
