# How can I retain a PersistenceVolume

**URL:** <https://forums.suse.com/t/how-can-i-retain-a-persistencevolume/17030>\
**Category:** SUSE Storage\
**Created:** [April 2, 2020, 6:09am UTC](https://forums.suse.com/t/how-can-i-retain-a-persistencevolume/17030 "2020-04-02T06:09:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsoika](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/rsoika/32/6141_2.png) [@rsoika](https://forums.suse.com/u/rsoika)\
**Post date:** [April 2, 2020, 6:09am UTC](https://forums.suse.com/t/how-can-i-retain-a-persistencevolume/17030/1 "2020-04-02T06:09:00Z")

</div>

I have setup a self managed kubernetes cluster together with [longhorn](https://longhorn.io/docs/users-guide/) as a distributed storage.

I have created a storage class with the ‘Retrain’ policy:

```
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: longhorn
  annotations:
    # make this class the default storage class
    storageclass.kubernetes.io/is-default-class: "true"

provisioner: driver.longhorn.io
reclaimPolicy: Retain
allowVolumeExpansion: true
parameters:
  numberOfReplicas: "3"
  staleReplicaTimeout: "2880" # 48 hours in minutes
  fromBackup: ""

```

I can create and bind a new PersistenceVolume to my pod (e.g a postgres server) by defining a PersistenceVolumeClaim as followed:

```auto
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: appdata
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: longhorn
  resources:
    requests:
      storage: 2Gi

```

Now after I deployed my pod I can see that a PV was created (and of course it is working 😉

```auto
kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-803d3061-457c-4023-9864-636c7a3f626a 2Gi RWO Retain Bound default/dbdata longhorn 10d

```

When I delete my postgreSQL pod my PV still exists thanks to the ‘Retain’ policy. But when I recreate my pod again, a new PersistenceVolume is created. So now I have two PVs:

```auto
kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-803d3061-457c-4023-9864-636c7a3f626a 2Gi RWO Retain Released default/dbdata longhorn 10d
pvc-9385b1ad-4c06-4bf2-b595-db6681334c3d 2Gi RWO Retain Bound default/dbdata longhorn 10d

```

What I did not understand so far - and this is my question - is how can I create/claim a PersistenceVolume with a custom name (without the random UUID). And how can I tell my POD to reuse this PV? Or asked the other way: how can I tell longhorn to create a PersistenceVolume with a given name and not with a random UUID? Or do I need to work with labels to support the matching mechanisms running in the background?

---

<div class="post-metadata">

**Author:** ![rsoika](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/rsoika/32/6141_2.png) [@rsoika](https://forums.suse.com/u/rsoika)\
**Post date:** [April 2, 2020, 2:02pm UTC](https://forums.suse.com/t/how-can-i-retain-a-persistencevolume/17030/2 "2020-04-02T14:02:40Z")

</div>

As I asked this question also on [Stackoverflow](https://stackoverflow.com/questions/60976472/how-can-i-retain-a-persistencevolume-in-longhorn/60986463#60986463) I want to extend my question.

I think the argument that a released PV (created dynamically) may contain sensitive data and should not be rebound to a POD makes sense to me.

So as far as I understand the problem now:  
In case I want a durable volume storing my database SQL data I should create the PV by myself manually and than bind it to my PVC. Is this right? Can someone give me a short example of a .yaml file how this setup should look like? I did not found an example in the longhorn documentation.
