<p>So I have <a href="https://stackoverflow.com/questions/65313780/kubernetes-how-to-config-a-group-of-pods-to-be-deployed-on-the-same-node">this problem</a>, and try to implement <code>podAffinity</code> to solve it.</p>
<p>I have 3 nodes and want to deploy 2 pods on the same node. In the <code>Deployment</code> YAML files I have <code>service:git</code> under <code>metadata.labels</code>, and the following is the affinity setting:</p>
<pre><code>  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: service
            operator: In
            values:
            - git
        topologyKey: kubernetes.io/hostname
</code></pre>
<p>But the pods failed to deploy, I got the following error:</p>
<pre><code>0/3 nodes are available: 3 node(s) didn't match pod affinity rules, 3 node(s) didn't match pod affinity/anti-affinity.
</code></pre>
<p>Are there any problems with my configuration?</p>
<p>If not, I guess maybe it is because when the first pod is deployed, the system will try to find a node that contains a pod with the label <code>service: git</code> and fail (because it is the first one), and another pod also fail because of the same reason. Is this correct?</p>
<p>But then how to solve the problem (without resorting to workarounds)?</p>

<ul>
<li>You are using &quot;requiredDuringSchedulingIgnoredDuringExecution:&quot; so it will be looking a &quot;running(already)&quot; pod which has a label &quot;service: git&quot; and itseems you do not already have any pod with that label. so following a is a quick workaround where a test pod will be created with label &quot;service: git&quot; . so that podAffinity rule will find a destination node ( that would be the node where this testpod will be running)</li>
</ul>
<p><code>kubectl run testpod --image=busybox --labels=&quot;service=git&quot; -- sleep infinite</code></p>
<ul>
<li><p>Once above pod is UP .. all the pods in your deployment also should get created.
if not delete the deployment and re-apply it.</p>
</li>
<li><p>If you need a elegant solution then you can consider using &quot;preferredDuringSchedulingIgnoredDuringExecution&quot; instead of &quot;requiredDuringSchedulingIgnoredDuringExecution&quot;</p>
</li>
</ul>
