0

I have an EKS cluster which has following files.

1)mysql deployment file 2)pvc claim file 3)storageclass file

when I run all three files, dynamically ebs volume is created. Then I make an entry in mysql table and try to delete and recreate the pod.

Now everything in ebs volume gets deleted and there is no data.

I am trying to figure out how to make the data persistent when pod or deployment gets deleted and started again.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      application: mysql
  replicas: 1
  template:
    metadata:
      labels:
        application: mysql
    spec:
      containers:
      - name: mysql
        image: vjk94/data-admin2:version2
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: mysql-data
      volumes:
        - name: mysql-data
          persistentVolumeClaim:
            claimName: mysql-data

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql-data
spec:
  storageClassName: ebs-sc
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: ebs-sc
provisioner: kubernetes.io/aws-ebs
parameters:
  type: gp2
  fsType: ext4
reclaimPolicy: Retain
allowVolumeExpansion: true
mountOptions:
  - debug
volumeBindingMode: WaitForFirstConsumer
rkosegi
  • 14,165
  • 5
  • 50
  • 83
Vijay
  • 1

1 Answers1

2

PVCs have a lifetime independent of pods. PV still exists because it has ReclaimPolicy set to Retain in which case it won't be deleted even if PVC is gone, however while you are starting your pod again new PV and PVC is created that is why you are seeing empty - With reclaim policy Retain when the PersistentVolumeClaim is deleted, the PersistentVolume still exists and the volume is considered "released". But it is not yet available for another claim because the previous claimant's data remains on the volume. See: pv-reclaim-policy-retain. However:

An administrator can manually reclaim the volume with the following steps.

  1. Delete the PersistentVolume. The associated storage asset in external infrastructure (such as an AWS EBS, GCE PD, Azure Disk, or Cinder volume) still exists after the PV is deleted.
  2. Manually clean up the data on the associated storage asset accordingly.
  3. Manually delete the associated storage asset, or if you want to reuse the same storage asset, create a new PersistentVolume with the storage asset definition.

Read more: pv-reclaim-policy-example, pv-reclaim-policy-retain-example, retain-reclaim-policy-fetch-data.

EDIT:

If you will add subpath: mysql parameter in deployment everything will work properly. Data will persist even if you will delete deployment and restart again.

Malgorzata
  • 6,409
  • 1
  • 10
  • 27
  • However I tested for redis deployment and nginx deployment with dynamic pvc creation. When the pod gets recreated, i can see the old data in the volume. But not in case of mysql – Vijay Mar 05 '21 at 14:45
  • Also, I have tested deleting the mysql pod and I can see the newly created pod is mounting to the old PV, but I couldn't found the data. Then If I use mountpath as /var/lib/mysql in deployment, it fails creating the deployment but currently I am using the path as /data – Vijay Mar 05 '21 at 15:05
  • I have added the subpath: mysql parameter in deployment and its completely working fine. Data is getting persisted even if i delete deployment and restart again. Thanks – Vijay Mar 08 '21 at 18:14
  • I have edited my answer with information provided by you. – Malgorzata Mar 16 '21 at 08:50