1

I'm trying to configure mongodb deployment inside k8s world. My mongo deployment file looks like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: panel-admin-mongo-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: panel-admin-mongo
  template:
    metadata:
      labels:
        component: panel-admin-mongo
    spec:
      volumes:
        - name: panel-admin-mongo-storage
          persistentVolumeClaim:
            claimName: database-persistent-volume-claim
      containers:
        - name: panel-admin-mongo
          image: mongo
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: panel-admin-mongo-storage
              mountPath: /data/db

Mongo service file:

apiVersion: v1
kind: Service
metadata:
  name: panel-admin-mongo-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: panel-admin-mongo
  ports:
    - port: 27017
      targetPort: 27017

And my persistent volume claim:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: database-persistent-volume-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

When I'm entering the mongodb container using: kubectl exec -it panel-admin-mongo-deployment-6dcfc5b8c7-mk8d5 sh and I'm saving some users email and password inside the collection (f.ex. users) everything works fine. But when I shot down the pod and container inside of it, boot up again, the data is gone. Shouldn't be independent of life-cycle of pod? And if yes what am I missing?

Murakami
  • 3,474
  • 7
  • 35
  • 89
  • I've reproduced your yaml files on Minikube and GKE and it works as expected. Can you add information about how to reproduce this problem? How are you injecting this data to database? – Mark Watney Dec 30 '19 at 10:41
  • So as I understand you are able to delete mongo deployment file and boot it up again and the data will be there? I'm injecting the data entering the pod with `kubectl exec -it [pod-name] sh`, then `mongo`, then `use [mydbname]` and f.ex. `db.users.save({ email: "test@test.com" })` – Murakami Dec 30 '19 at 11:59
  • Yes, for me it hust works fine. I'm deploying your `PersistentVolumeClaim`, than your `Deployment`. I'm not changing anything, copy and paste. – Mark Watney Dec 30 '19 at 14:57

2 Answers2

0

I am not a k8s expert, but your problems is that you are not using Kubernetes statfulsets, have a look here https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/

AFAIK, for any persistent deployment you need to make your pods using statefulsets.

Karim Tawfik
  • 1,286
  • 1
  • 11
  • 21
0

First, make sure that you pod success usage PVC:

kubectl describe po/${POD_NAME}

and check Volumes section:

Volumes:
  prometheus-operator-db:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  prometheus-operator-db-0
    ReadOnly:   false

If you success usage PVC, need check reclaim-policy for you PV, this value should be persistentVolumeReclaimPolicy":"Retain".

Arslanbekov Denis
  • 1,674
  • 12
  • 26
  • When I'm checking reclaim-policy it show `delete` with status of `terminating` and it stucked like this. When I want to delete pvc it stucks as well – Murakami Dec 27 '19 at 23:38
  • try deleting it using flags `--force --grace-period=0` – Arslanbekov Denis Dec 27 '19 at 23:52
  • I've finally deleted it using the first answer https://stackoverflow.com/questions/51358856/kubernetes-cant-delete-persistentvolumeclaim-pvc – Murakami Dec 28 '19 at 08:40
  • I've change reclaim policy to `retain` with ```kubectl patch pv -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'``` and indeed not it is `retain` when inspecting `pv`. But again the same story happens: I save some data in db, then I delete mongo deployment and mongo pod, I boot up again mongo deployment and when I enter into the db inside the pod I can't see data there. When I delete mu mongo deployment I can see that `pv` exists and it has indeed `retain policy` – Murakami Dec 28 '19 at 14:09
  • Followed this before reading the docs. Retain does not make data persist. It just retains the volume for later manual recovery of data. Kubernetes will still create a new PVC with default Delete policy and bind it to the mongo service. It will set status to Release on the original one but keep it around instead of deleting it. – isimmons Jul 27 '22 at 03:15