<p>I have created my own k8s cluster using Kubeadm with two Ubuntu virtual servers - one master and one worker node. I also deployed a Springboot app with MongoDB persistence and it works absolutely fine. Below are the nodes in my k8s cluster</p>
<p><a href="https://i.stack.imgur.com/Ng1KH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ng1KH.png" alt="enter image description here" /></a></p>
<p>Below are the contents of my yaml file which creates storage class, persistence volume, persistence volume claim, mongo deployment and mongo service</p>
<pre><code>apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
   name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true
reclaimPolicy: Retain
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mongo-pv
spec:
  capacity:
    storage: 5Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  hostPath:
    path: &quot;/home/moviepopcorn/mongodb/data&quot;
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values: 
          - node1
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mongo-pvc
spec:
  volumeName: mongo-pv
  volumeMode: Filesystem
  storageClassName: local-storage
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongo
  labels:
    app: mongo
spec:
   selector:
      matchLabels:
         app: mongo
   template:
      metadata:
         labels:
            app: mongo
      spec:
        containers:
        - name: mongo
          image: mongo:4.4.11
          ports:
          - containerPort: 27017 
          volumeMounts:
          - name: mongo-persistent-storage
            mountPath: /home/moviepopcorn/mongodb/data
        volumes:
          - name: mongo-persistent-storage
            persistentVolumeClaim:
              claimName: mongo-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: mongo-service
spec:
  selector:
    app: mongo
  ports:
    - protocol: TCP 
      port: 27017
      targetPort: 27017
</code></pre>
<p><a href="https://i.stack.imgur.com/or3i5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/or3i5.png" alt="enter image description here" /></a></p>
<p><a href="https://i.stack.imgur.com/uwn8J.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uwn8J.png" alt="enter image description here" /></a></p>
<p>Data stored from my Springboot into mongodb is not retained once I reboot the nodes in my k8s cluster. Could you please help me understand on what mistake I am doing?</p>

<p>May you have updated the <strong>PVC</strong> mount path and due to that mongo not aware of where to store or save the data</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: mongo
  name: mongo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongo
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: mongo
    spec:
      containers:
      - image: mongo
        name: mongo
        args: [&quot;--dbpath&quot;,&quot;/data/db&quot;]
        livenessProbe:
          exec:
            command:
              - mongo
              - --disableImplicitSessions
              - --eval
              - &quot;db.adminCommand('ping')&quot;
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 6
        readinessProbe:
          exec:
            command:
              - mongo
              - --disableImplicitSessions
              - --eval
              - &quot;db.adminCommand('ping')&quot;
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 6
        env:
        - name: MONGO_INITDB_ROOT_USERNAME
          valueFrom:
            secretKeyRef:
              name: mongo-creds
              key: username
        - name: MONGO_INITDB_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mongo-creds
              key: password
        volumeMounts:
        - name: &quot;mongo-data-dir&quot;
          mountPath: &quot;/data/db&quot;
      volumes:
      - name: &quot;mongo-data-dir&quot;
        persistentVolumeClaim:
          claimName: &quot;pvc&quot;
</code></pre>
<p>Try the above example or check the <strong>arg</strong> and <strong>mount path</strong> for reference.</p>
