<p>I have a microservice that is working on my laptop. However, I am using docker compose. I am working to deploy to a kubernetes cluster which I have already set up. I am stuck on making data persistent. E.g here is my mongodb in docker-compose</p>
<pre><code>systemdb:
    container_name: system-db
    image: mongo:4.4.1
    restart: always
    ports:
      - '9000:27017'
    volumes:
      - ./system_db:/data/db
    networks:
      - backend
</code></pre>
<p>Since it is an on premise solution, I went with an NFS server. I have created a Persistent Volume and Persistent Volume Claim (pvc-nfs-pv1) which seem to work well when testing with nginx. However, I don't know how to deploy a mongodb statefulset to use the pvc. I am not implementing a replicaset.</p>
<p>Here is my yaml:</p>
<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongod
spec:
  serviceName: mongodb-service
  replicas: 1
  selector:
    matchLabels:
      role: mongo
  template:
    metadata:
      labels:
        role: mongo
        environment: test
    spec:
      terminationGracePeriodSeconds: 10
      containers:
        - name: mongod-container
          image: mongo
          resources:
            requests:
              cpu: &quot;0.2&quot;
              memory: 200Mi
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: pvc-nfs-pv1
              mountPath: /data/db
  volumeClaimTemplates:
  - metadata:
      name: pvc-nfs-pv1
       annotations:
         volume.beta.kubernetes.io/storage-class: &quot;standard&quot;
    spec:
      accessModes: [ &quot;ReadWriteOnce&quot; ]
      resources:
        requests:
          storage: 500Mi
</code></pre>
<p>How do i do it?</p>

Denn
<p><code>volumeClaimTemplates</code> are used for dynamic volume provisioning. So you're defining one volume claim template which will be used to create a <code>PersistentVolumeClaim</code> for each pod.</p>
<blockquote>
<p>The <code>volumeClaimTemplates</code> will provide stable storage using
<a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/" rel="nofollow noreferrer">PersistentVolumes</a>
provisioned by a PersistentVolume Provisioner</p>
</blockquote>
<p>So for  your use case you would need to create <code>storageclass</code> with nfs provisioner. <a href="https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner" rel="nofollow noreferrer">NFS Subdir external provisioner</a>  is an automatic provisioner that use your <em>existing and already configured</em> NFS server to support dynamic provisioning of Kubernetes Persistent Volumes via Persistent Volume Claims. Persistent volumes are provisioned as <code>${namespace}-${pvcName}-${pvName}</code>.</p>
<p>Here`s an example how to define storage class:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: managed-nfs-storage
provisioner: fuseim.pri/ifs # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
  pathPattern: &quot;${.PVC.namespace}/${.PVC.annotations.nfs.io/storage-path}&quot; # waits for nfs.io/storage-path annotation, if not specified will accept as empty string.
  onDelete: delete
</code></pre>
