<p>I have a pod that i want to run on the same node if possible as another one or at least to run in the same availability zone at first i thought that i can use podAffinity with preferredduringschedulingignoredduringexecution and two podAffinityTerm but it doesn't seem to work properly so i thought if i can run two podAffinity so that if the first one (run on the same host) is not possible so the second one (run on the same AZ) will take place.</p>
<p>Here is my deployment manifest :</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-test-deployment
spec:
  selector:
    matchLabels:
      app: app-test-deployment
  replicas: 1
  template:
    metadata:
      labels:
        app: app-test-deployment
    spec:
      affinity:
        podAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - pod-test
              topologyKey: &quot;kubernetes.io/hostname&quot;
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - pod-test
              topologyKey: &quot;failure-domain.beta.kubernetes.io/zone&quot;
      containers:
      - name: app-test
        image: app-test:1.14.2
        ports:
        - containerPort: 80
</code></pre>

touati ahmed
<ol>
<li>Set weight for <code>/zone</code> to 99</li>
<li>Replace <code>preferredDuringSchedulingIgnoredDuringExecution</code> to <code>requiredDuringSchedulingIgnoredDuringExecution</code> and when it fails - you will see what is causing it to go to another AZ. Once it works set back to <code>preferred</code>.</li>
</ol>
<p>Also, are you trying to link the Affinity to the same deployment you posted? If so - your labels have a mismatch: <code>app: app-test-deployment</code> in deployment VS <code>app: pod-test</code> in podAffinityTerm.</p>
