<p>I'm working on a bare-metal inst of k8s, and am trying out local persistent volume using k8s v1.14.  The purpose is to allow me to create an HA postgres deployment using <a href="https://github.com/zalando/postgres-operator" rel="nofollow noreferrer">postgres-operator</a>.  Since I'm on bare metal, I cannot make use of dynamic PVCs, as seems to be normal in the tutorials.</p>

<p>To begin with, I created some PVs bound to manually created volumes on the host nodes. The PVs are assigned, using <code>nodeAffinity</code>, to specific nodes.  i.e. <code>primary-vol</code> PV is assigned to <code>node1</code>, and <code>replica-vol-1</code> is assigned to <code>node2</code> and so on. </p>

<p>I am then binding my pod to the PV using a PVC as documented <a href="https://kubernetes.io/blog/2019/04/04/kubernetes-1.14-local-persistent-volumes-ga/" rel="nofollow noreferrer">here</a>.</p>

<p>What I've found is that the k8s scheduler has placed my pod (which is bound to a PV on <code>node1</code>) on <code>node2</code> rather than <code>node1</code> as I expected.</p>

<p>Is there a way, using affinity on the pod, to ensure that the pod is created on the <strong>same node</strong> as the PV it is bound to?</p>

<p><strong>EDIT</strong>: to simplify the question (<em>with apologies to artists and architects everywhere</em>)</p>

<p>How can the pod know what node the PV is assigned to, when it doesn't even know what PV it is bound to?</p>

<p><a href="https://i.stack.imgur.com/WQbT9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WQbT9.png" alt="How can the pod know what node the PV is assigned to, when it doesn&#39;t even know what PV it is bound to?"></a></p>

Andrew Matthews
<p>Yes, you can achieve it using the claimRef in your PV definition. By using <code>claimRef</code> in your PV, you're binding a PVC with specific name to that PV and you can use that PVC name in your pod definition in <code>persistentVolumeClaim</code>.</p>

<p>You should have PV definition like following:</p>

<pre><code>{
  "kind": "PersistentVolume",
  "apiVersion": "v1",
  "metadata": {
    "name": "pv-data-vol-0",
    "labels": {
      "type": "local"
    }
  },
  "spec": {
    "capacity": {
      "storage": "10Gi"
    },
    "accessModes": [
      "ReadWriteOnce"
    ],
    "storageClassName": "local-storage",
    "local": {
      "path": "/prafull/data/pv-0"
    },
    "claimRef": {
      "namespace": "default",
      "name": "data-test-sf-0"
    },
    "nodeAffinity": {
      "required": {
        "nodeSelectorTerms": [
          {
            "matchExpressions": [
              {
                "key": "kubernetes.io/hostname",
                "operator": "In",
                "values": [
                  "ip-10-0-1-46.ec2.internal"
                ]
              }
            ]
          }
        ]
      }
    }
  }
}
</code></pre>

<p>In the above json file, the <code>claimRef</code>, <code>name</code> should be the name of the PVC you want to bind that PV to and <code>namespace</code> should be the namespace in which the <code>PVC</code> reside. </p>

<p>Note: <code>namespace</code> is mandatory field as PV's are independent of namespace and PVC are bound in namespace and hence PV should know in which namespace it should look for PVC.</p>

<p>So once you're able to bind the specific PV to specific PVC, you can bind that specific PVC to the specific POD and hence that pod will always come on the same node where PV is present.</p>

<p>For reference, please have a look at my following answer:</p>

<blockquote>
  <p><a href="https://stackoverflow.com/questions/52948124/is-it-possible-to-mount-different-pods-to-the-same-portion-of-a-local-persistent/52952505#52952505">Is it possible to mount different pods to the same portion of a local persistent volume?</a></p>
</blockquote>

<p>Hope this helps</p>
