<p>I have an EKS cluster which has following files.</p>
<p>1)mysql deployment file
2)pvc claim file
3)storageclass file</p>
<p>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.</p>
<p>Now everything in ebs volume gets deleted and there is no data.</p>
<p>I am trying to figure out how to make the data persistent when pod or deployment gets deleted and started again.</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>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</code></pre>
</div>
</div>
</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql-data
spec:
  storageClassName: ebs-sc
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi</code></pre>
</div>
</div>
</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>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</code></pre>
</div>
</div>
</p>

Vijay
<p>PVCs have a lifetime independent of pods.  <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/" rel="nofollow noreferrer">PV</a> 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 &quot;released&quot;. But it is not yet available for another claim because the previous claimant's data remains on the volume. See: <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/#retain" rel="nofollow noreferrer">pv-reclaim-policy-retain</a>.
However:</p>
<blockquote>
<p>An administrator can manually reclaim the volume with the following
steps.</p>
<ol>
<li>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.</li>
<li>Manually clean up the data on the associated storage asset accordingly.</li>
<li>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.</li>
</ol>
</blockquote>
<p>Read more: <a href="https://stackoverflow.com/questions/58418093/state-of-pv-pvc-after-pod-is-deleted-in-kubernetes">pv-reclaim-policy-example</a>, <a href="https://stackoverflow.com/questions/60606141/can-we-assign-pvc-to-a-pv-if-it-is-in-released-state">pv-reclaim-policy-retain-example</a>, <a href="https://letsdocloud.com/?p=854" rel="nofollow noreferrer">retain-reclaim-policy-fetch-data</a>.</p>
<p><strong>EDIT:</strong></p>
<p>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.</p>
