<p>I have a <code>kubernetes</code> cluster and I have set up an <code>NFS</code> server as persistent volume for a <code>mongodb</code> deployment.</p>
<p>And I have set the <code>PeristentVolume</code> and <code>PersistentVolumeClaim</code> as below:</p>
<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    name: mynfs 
spec:
  storageClassName: manual 
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  nfs:
    server: &lt;nfs-server-ip&gt;
    path: &quot;/srv/nfs/mydata&quot; 
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany 
  resources:
    requests:
      storage: 1Gi
</code></pre>
<p>Everything works fine but the only problem is, I can't run more than 1 <code>mongodb</code> pods because I get the following error.</p>
<blockquote>
<p>{&quot;t&quot;:{&quot;$date&quot;:&quot;2020-10-15T15:16:39.140+00:00&quot;},&quot;s&quot;:&quot;E&quot;,  &quot;c&quot;:&quot;STORAGE&quot;,  &quot;id&quot;:20557,   &quot;ctx&quot;:&quot;initandlisten&quot;,&quot;msg&quot;:&quot;DBException in initAndListen, terminating&quot;,&quot;attr&quot;:{&quot;error&quot;:&quot;DBPathInUse: Unable to lock the lock file: /data/db/mongod.lock (Resource temporarily unavailable). Another mongod instance is already running on the /data/db directory&quot;}}</p>
</blockquote>
<p>That pod is always in <code>CrashLoopBackOff</code> and restarts and again to the same status.</p>
<p><strong>I think the problem here is the same volume path mentioned in the <code>mongodb</code> deployment is trying to access by the two pods at the same time and when one pod is already have the lock, other pod failed.</strong></p>
<p>Here's the <code>mongodb</code> deployment yaml.</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongodb-deployment    
  labels:
    name: mongodb
spec:
  replicas: 2
  selector:
    matchLabels:
      app: mongodb
  template:
    metadata:
      labels: 
        app: mongodb
    spec:
      containers:
      - name: mongodb
        image: mongo
        ports:
        -  containerPort: 27017
        env: 
        - name: MONGO_INITDB_ROOT_USERNAME
          valueFrom:
            secretKeyRef:
              name: mongodb-secret
              key: mongo-username
        - name: MONGO_INITDB_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mongodb-secret
              key: mongo-password
        volumeMounts:
        - name: data  
          mountPath: /data/db
      volumes: 
        - name: data
          persistentVolumeClaim: 
            claimName: task-pv-claim  
</code></pre>
<p>can someone please help me fix this?</p>
<p>Thank you.</p>

<h2>Issue</h2>
<p>This log entry already tells you what is the issue</p>
<blockquote>
<p>{&quot;t&quot;:{&quot;$date&quot;:&quot;2020-10-15T15:16:39.140+00:00&quot;},&quot;s&quot;:&quot;E&quot;, &quot;c&quot;:&quot;STORAGE&quot;, &quot;id&quot;:20557, &quot;ctx&quot;:&quot;initandlisten&quot;,&quot;msg&quot;:&quot;DBException in initAndListen, terminating&quot;,&quot;attr&quot;:{&quot;error&quot;:&quot;DBPathInUse: Unable to lock the lock file: /data/db/mongod.lock (Resource temporarily unavailable). Another mongod instance is already running on the /data/db directory&quot;}}</p>
</blockquote>
<p>All members access the same volume and data.</p>
<p>AFAIK you cannot have multiple instances of MongoDB pointing to the same path, each MongoDB instance needs to have exclusive access to its own data files.</p>
<h2>Solution</h2>
<p>You can run your application as <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="nofollow noreferrer">StatefulSet</a> with <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#stable-storage" rel="nofollow noreferrer">volumeClaimTemplate</a> which ensures that each replica will mount its own volume. There is great <a href="https://stackoverflow.com/questions/50237572/kubernetes-trouble-with-statefulset-and-3-persistentvolumes/50249935#50249935">answer</a> about that.</p>
