Kubernetes PVC data persistent

8/22/2019

I have a simple kubernetes cluster setup on GKE. To persist the data for my express web app, I have a mongodb deployment, cluster-ip-service for the mongodb deployment and persistent volume claim running in the cluster.

Users data are being stored and everything works fine until I deleted the mongodb deployment on GKE console. When I try to bring the mongodb deployment back with the command:

kubectl apply -f mongodb-deployment.yaml

The mongodb deployment and PVC are running again but all the previous data was lost.

My mongodb deployment yaml file:

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

My persistent volume claim yaml file:

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

Since the data is be stored in persistent volume which is out of the cluster's lifecycle.

Shouldn't the previous data persist and become available when the database deployment is up and running again?

I think I might be missing something here.

-- Jack
google-kubernetes-engine
kubernetes
persistent-volume-claims
persistent-volumes

2 Answers

8/22/2019

If you want to preserve data even if PVC can be deleted, change reclaim policy to RETAIN. Then even PVC will be deleted your PV will be marked as RELEASED.

-- Oles Rid
Source: StackOverflow

8/22/2019

Yes it is possible with the reclaim setting. Please refer this documentation

-- Bimal
Source: StackOverflow