<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>

<p>Ok, I have a solution. It works simply by selecting the volume by using the matchLabels selector.</p>
<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: mongodb-data-volume
  labels:
    app: moderetic
    type: mongodb
    role: data
spec:
  storageClassName: hcloud-volumes
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  csi:
    volumeHandle: &quot;11099996&quot;
    driver: csi.hetzner.cloud
    fsType: ext4

---

---
apiVersion: mongodbcommunity.mongodb.com/v1
kind: MongoDBCommunity
metadata:
  name: system-mongodb
  labels:
    app: moderetic
    type: mongodb
spec:
  members: 1
  type: ReplicaSet
  version: &quot;4.2.6&quot;
  logLevel: INFO
  security:
    authentication:
      modes: [&quot;SCRAM&quot;]
  users:
    - name: moderetic
      db: moderetic
      passwordSecretRef:
        name: mongodb-secret
      roles:
        - name: clusterAdmin
          db: moderetic
        - name: userAdminAnyDatabase
          db: moderetic
      scramCredentialsSecretName: moderetic-scram-secret
  additionalMongodConfig:
    storage.wiredTiger.engineConfig.journalCompressor: zlib
  persistent: true
  statefulSet:
    spec:
      template:
        spec:
          containers:
            - name: mongod
              resources:
                requests:
                  cpu: 1
                  memory: 1Gi
                limits:
                  memory: 8Gi
            - name: mongodb-agent
              resources:
                requests:
                  memory: 50Mi
                limits:
                  cpu: 500m
                  memory: 256Mi
      volumeClaimTemplates:
        - metadata:
            name: data-volume
          spec:
            accessModes: [&quot;ReadWriteOnce&quot;]
            storageClassName: hcloud-volumes
            resources:
              requests:
                storage: 10Gi
            selector:
              matchLabels:
                app: moderetic
                type: mongodb
                role: data
        - metadata:
            name: logs-volume
          spec:
            accessModes: [&quot;ReadWriteOnce&quot;]
            storageClassName: hcloud-volumes
            resources:
              requests:
                storage: 10Gi
            selector:
              matchLabels:
                app: moderetic
                type: mongodb
                role: logs

</code></pre>
