<p>I'm deploying a mongodb cluster with the <a href="https://github.com/mongodb/mongodb-kubernetes-operator" rel="nofollow noreferrer">MongoDB Community Operator</a>. ReplicaSet configuration.</p>
<p>I followed <a href="https://github.com/mongodb/mongodb-kubernetes-operator/blob/master/config/samples/arbitrary_statefulset_configuration/mongodb.com_v1_hostpath.yaml" rel="nofollow noreferrer">this example</a> for my configuration. The difference is that I want a pod on each node with his pv on the node. At every deploy the same node location pod/pv.</p>
<p>I have deployed a mongodb statefulset with two replicas. I want that pods of the statefulset <code>pod-0</code>, <code>pod-1</code> lays on specific nodes: <code>node-0</code>, <code>node-1</code>.</p>
<p>For the two pods I deployed two persistent volumes of type <code>hostpath</code>. One for each node: <code>pv-0</code> and <code>pv-1</code>.</p>
<p>All seems fine but there is <strong>The problem</strong>:
Sometimes the PVC of <code>pod-0</code>(forced on node-0) is bounded to pv-1 (foce on node-1) or vice-versa. So the pod can't start because there is a node conflict</p>
<p>Is there a way to force <code>pod-0</code> on the same node of <code>pv-0</code>?</p>
<p>Maybe with the <code>MongoDBCommunity.spec.statefulSet.VolumeClameTemplates</code>, but I can't figure out how.</p>
<p>I read <a href="https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/" rel="nofollow noreferrer">HERE</a>, but I can't figure out how to apply to statefulset.</p>
<p>Follows my yamls. Satefulset:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: mongodbcommunity.mongodb.com/v1
kind: MongoDBCommunity
metadata:
  name: my-mongo
  labels:
    app: my-mongo
  namespace: mongo-system
spec:
  members: 2
  statefulSet:
    spec:
      template:
      volumeClaimTemplates:
      - metadata:
          name: data-volume
        spec:
          storageClassName: hostpath
          accessModes: [ &quot;ReadWriteOnce&quot; ]
          resources:
            requests:
              storage: 11Gi
          selector:
            matchLabels:
              type: data
</code></pre>
<p>PV</p>
<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: data-volume-db-0
  labels:
    type: data
spec:
  storageClassName: hostpath
  persistentVolumeReclaimPolicy: Retain
  capacity:
    storage: 11Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /data/volumes/db-data
    type: &quot;&quot;
  nodeAffinity:
    required:
      # This is just an example for matchexpression
      # This field is required depends on the specific
      # of the environment the resource is deployed in
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node-0

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: data-volume-db-1
  labels:
    type: data
spec:
  storageClassName: hostpath
  persistentVolumeReclaimPolicy: Retain
  capacity:
    storage: 11Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /data/volumes/db-data
    type: &quot;&quot;
  nodeAffinity:
    required:
      # This is just an example for matchexpression
      # This field is required depends on the specific
      # of the environment the resource is deployed in
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node-1
</code></pre>

Oscar Campo
<p>I resolved using the <code>claimRef</code> on the PV yaml</p>
<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: data-volume-db-1
  labels:
    type: data
spec:
  storageClassName: hostpath
  persistentVolumeReclaimPolicy: Retain
  capacity:
    storage: 11Gi
  claimRef:
    namespace: system-mongo
    name: data-volume-db-0
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /data/volumes/db-data
    type: &quot;&quot;
  nodeAffinity:
    required:
      # This is just an example for matchexpression
      # This field is required depends on the specific
      # of the environment the resource is deployed in
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node-1
</code></pre>
