<p>We have a deployment with a large replicas number ( &gt; 1 ) that we must deploy in the same zone.</p>
<p>We stumbled upon this documentation section: <a href="https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#an-example-of-a-pod-that-uses-pod-affinity" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#an-example-of-a-pod-that-uses-pod-affinity</a></p>
<p>which explains how to schedule pods in zones that already have other pods that match certain labels.</p>
<p>however, there are no other pods that our deployment depends upon. all other workloads are replicated and spread across multiple zones, and this is the first deployment that we would like to keep in a single zone.</p>
<p>also, we thought about explicitly setting the zone for this deployment, but in case of zone failure, it will become unavailable until we notice and explicitly set it to another zone. so setting the exact zone won't work here.</p>
<p>any insights here? and thanks!</p>

mdnfiras
<p>Pod Affinity affects how the pod is scheduled based on the presence or absence of other pods within the node. That would probably not serve the purpose you're trying to achieve.</p>
<p>You're probably better off using node affinity (it's on the same link you provided)</p>
<p>That would allow you to force to a zone, because each GKE node will have a <code>failure-domain</code> label which you can get doing this and looking through the results:</p>
<pre><code>kubectl get node {name-of-node} -o json | jq &quot;.metadata.labels&quot;
</code></pre>
<p>The labels will read something like this:</p>
<pre><code>  &quot;failure-domain.beta.kubernetes.io/region&quot;: &quot;europe-west2&quot;,
  &quot;failure-domain.beta.kubernetes.io/zone&quot;: &quot;europe-west2-b&quot;,
</code></pre>
<p>You can then combine this with nodeAffinity in your deployment yaml (parts snipped for brevity):</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    ...
  annotations:
    ...
  name: my-deployment
spec:
  replicas: 1
  strategy: {}
  selector:
    matchLabels:
      ...
  template:
    metadata:
      annotations:
        ...
      labels:
        ...
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: failure-domain.beta.kubernetes.io/zone
                operator: In
                values:
                - europe-west2-b
</code></pre>
<p>This will force the pods generated by the deployment to all go onto nodes sitting in europe-west2-b</p>
<p>I could change this and make it like this:</p>
<pre><code>    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: failure-domain.beta.kubernetes.io/zone
                operator: In
                values:
                - europe-west2-b
                - europe-west2-c
</code></pre>
<p>To allow it schedule in two zones (but it would not be able to schedule on to the <code>europe-west2-a</code> zone as a consequence)</p>
