<p>I have a kubernetes cluster spread across two zones, A and B. I am using nfs volumes for persistent storage. I have nfs volumes in both the zones. I am creating a stateful set of 2 replicas which will be spread across these zones (I used pod anti-affinity to achieve this). Now I want the pods in zone A to use the volumes in zone A and ones in zone B to use the volumes in zone B.</p>

<p>I can add labels to the persistent volumes and match the persistent volume claims with these labels. But how do I make sure that the pvc for a pod does not get bound to a pv in another zone?</p>

raiyan
<p>You can try to bind <code>persistent volume claims (PVCs)</code> to <code>persistent volumes (PVs)</code> and split Kubernetes pods across your cluster between two zones using the special built-in label <code>failure-domain.beta.kubernetes.io/zone</code>. If you create volumes manually, it is possible to label them with <code>failure-domain.beta.kubernetes.io/zone:zoneA</code> value, ensuring that pod is only scheduled to nodes in the same zone as the zone of the persistent volume.</p>

<p>For example, to set <code>label</code> for a Node and PV:</p>

<pre><code>kubectl label node &lt;node-name&gt; failure-domain.beta.kubernetes.io/zone=zoneA

kubectl label pv &lt;pv-name&gt; failure-domain.beta.kubernetes.io/zone=zoneA
</code></pre>

<p>Find some useful information from official Kubernetes <a href="https://kubernetes.io/docs/reference/kubernetes-api/labels-annotations-taints/#failure-domainbetakubernetesiozone" rel="nofollow noreferrer">documentation</a>.        </p>
