I have setup a self managed kubernetes cluster together with longhorn 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:
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
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:
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?