Mysql data in persistent volume of EBS volume gets deleted on pod recreation

3/4/2021

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.

<!-- begin snippet: js hide: false console: true babel: false --><!-- language: lang-html -->
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
<!-- end snippet --><!-- begin snippet: js hide: false console: true babel: false --><!-- language: lang-html -->
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql-data
spec:
  storageClassName: ebs-sc
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
<!-- end snippet --><!-- begin snippet: js hide: false console: true babel: false --><!-- language: lang-html -->
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
<!-- end snippet -->
-- Vijay
amazon-eks
kubernetes
persistent-volumes

1 Answer

3/5/2021

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
Source: StackOverflow