<p>I am trying to get my deployment to only deploy replicas to nodes that aren't running rabbitmq (this is working) and also doesn't already have the pod I am deploying (not working).</p>
<p>I can't seem to get this to work. For example, if I have 3 nodes (2 with label of app.kubernetes.io/part-of=rabbitmq) then all 2 replicas get deployed to the remaining node. It is like the deployments aren't taking into account their own pods it creates in determining anti-affinity. My desired state is for it to only deploy 1 pod and the other one should not get scheduled.</p>
<pre><code>kind: Deployment
metadata:
  name: test-scraper
  namespace: scrapers
  labels:
    k8s-app: test-scraper-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: testscraper
  template:
    metadata:
      labels:
        app: testscraper
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app.kubernetes.io/part-of
                operator: In
                values:
                - rabbitmq
              - key: app
                operator: In
                values:
                - testscraper
            namespaces: [scrapers, rabbitmq]
            topologyKey: &quot;kubernetes.io/hostname&quot;
      containers:
        - name: test-scraper
          image: #######:latest```
</code></pre>

J Kent
<p>I think Thats because of the <code>matchExpressions</code> part of your manifest , where it requires pods need to have both the labels  <code>app.kubernetes.io/part-of: rabbitmq</code> <strong>and</strong>  <code>app: testscraper</code> to satisfy the antiaffinity rule.</p>
<p>Based on deployment yaml you have provided , these pods will have only <code>app: testscraper</code> but <strong>NOT</strong> <code>pp.kubernetes.io/part-of: rabbitmq</code> hence both the replicas are getting scheduled on same node</p>
<p>from Documentation (<strong>The requirements are ANDed.</strong>):</p>
<pre><code>kubectl explain pod.spec.affinity.podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecution.labelSelector

...
FIELDS:
   matchExpressions     &lt;[]Object&gt;
     matchExpressions is a list of label selector requirements.
     **The requirements are ANDed.**
</code></pre>
