<p>I have a app where two pods needs to have access to the same volume. I want to be able to delete the cluster and then after apply to be able to access the data that is on the volume.</p>
<p>So for example:</p>
<pre><code>apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: retaining
provisioner: csi.hetzner.cloud
reclaimPolicy: Retain
volumeBindingMode: WaitForFirstConsumer 

---

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: media
spec:
  #storageClassName: retaining
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-php
  labels:
    app: myapp-php
    k8s-app: myapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapp-php
  template:
    metadata:
      labels:
        app: myapp-php
        k8s-app: myapp
    spec:
      containers:
      - image: nginx:1.17
        imagePullPolicy: IfNotPresent
        name: myapp-php
        ports:
        - containerPort: 9000
          protocol: TCP
        resources:
          limits:
            cpu: 750m
            memory: 3Gi
          requests:
            cpu: 750m
            memory: 3Gi
        volumeMounts:
        - name: media
          mountPath: /var/www/html/media
      volumes:
      - name: media
        persistentVolumeClaim:
          claimName: media
      nodeSelector:
        mytype: main

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-web
  labels:
    app: myapp-web
    k8s-app: myapp
spec:
  selector:
    matchLabels:
      app: myapp-web
  template:
    metadata:
      labels:
        app: myapp-web
        k8s-app: myapp
    spec:
      containers:
      - image: nginx:1.17
        imagePullPolicy: IfNotPresent
        name: myapp-web
        ports:
        - containerPort: 9000
          protocol: TCP
        resources:
          limits:
            cpu: 10m
            memory: 128Mi
          requests:
            cpu: 10m
            memory: 128Mi
        volumeMounts:
        - name: media
          mountPath: /var/www/html/media
      volumes:
      - name: media
        persistentVolumeClaim:
          claimName: media
      nodeSelector:
        mytype: main

</code></pre>
<p>If I do these:</p>
<pre><code>k apply -f pv-issue.yaml 
k delete -f pv-issue.yaml 
k apply-f pv-issue.yaml 
</code></pre>
<p>I want to connect the same volume.</p>
<p>What I have tried:</p>
<ol>
<li>If I keep the file as is, the volume will be deleted so the data will be lost.</li>
<li>I can remove the pvc declaration from the file. Then it works. My issue that on the real app I am using kustomize and I don't see a way to exclude resources when doing <code>kustomize build app | kubectl delete -f -</code></li>
<li>Tried using retain in the pvc. It retains the volume on delete, but on the apply a new volume is created.</li>
<li>Statefulset, however I don't see a way that to different statefulsets can share the same volume.</li>
</ol>
<p>Is there a way to achieve this?
Or should I just do regular backups, and restore the volume data from backup when recreating the cluster?</p>

justadev
<p><strong>Is there a way to achieve this? Or should I just do regular backups, and restore the volume data from backup when recreating the cluster?</strong></p>
<p>Cluster deletion will make all your local volumes to be deleted. You can achieve this by storing the data outside the cluster. <code>Kubernetes</code>  has a wide variety of storage providers to help you deploy data on a variety of storage types.</p>
<p>You may want to think also that you can keep the data locally on nodes with usage of <code>hostPath</code> but that is also not a good solution since it will require you to pin the pod to the specific  node to avoid data loss. And if you delete you cluster in a way that all of you <code>VM</code> are gone, then this will be also gone.</p>
<p>Having some network-attached storage would be right way to go here. Very good example of those are <a href="https://cloud.google.com/compute/docs/disks#pdspecs" rel="nofollow noreferrer">Persistence disks</a> which durable network storage devices that you instances can access. They're located independently from you virtuals machines and they <a href="https://cloud.google.com/kubernetes-engine/docs/how-to/deleting-a-cluster" rel="nofollow noreferrer">are not being deleted</a> when you delete the cluster.</p>
