Persistent Volume Claim
Persistent Volumes and Persistent Volume Claims are two separate objects in the Kubernetes namespace. An Administrator creates a set of Persistent Volumes and a user creates Persistent Volume Claims to use to storage.
Once the Persistent Volume Claims are created, Kubernetes binds the Persistent Volumes to Claims based on the request and properties set on the volume.
Every Persistent Volume Claims is bound to single Persistent volume. During the binding process Kubernetes tries to find a persistent volume that has sufficient capacity as requested by the claim and any other request properties such as access modes volume modes storage class etc.
However if there are multiple possible matches for a single claim and you would like to specifically use a particular volume you could still use labels and selectors to bind to the right volumes.
Finally note that a smaller claim may get bound to a larger volume if all the other criteria matches and there are no better options.
There is a one to one relationship between claims and volumes so no other claims can utilize the remaining capacity in the volume.
If there are no volumes available the persistent volume claim will remain in a pending state until newer volumes are made available to the cluster once newer volumes are available.
The claim would automatically be bound to the newly available volume. Let us now create a persistent volume claim.
kind: PersistentVolumeClaim apiVersion: v1 metadata: name: myclaim spec: accessModes: [ "ReadWriteOnce" ] resources: requests: storage: 500Mi
kind: PersistentVolume apiVersion: v1 metadata: name: pv-vol spec: accessModes: [ "ReadWriteOnce" ] capacity: storage: 1Gi hostPath: path: /home/ashok/data
To create the persistent volume claim run the following command.
$ kubectl create -f pvc-definition.yaml persistentvolumeclaim/myclaim created
To view the create persistent volume claim run the following command.
$ kubectl get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE myclaim Pending
Here we see the claim in a pending state. When the claim is created, Kubernetes looks at the volume created previously. The access Modes match. The capacity requested is 500 Megabytes but the volume is configured with 1 GB of storage.
Since there are no other volumes available. The persistent volume claim is bound to persistent volume when we run to get volumes command again.
$ kubectl get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE myclaim Bound pv-vol 1Gi RWO 1min
We see the claim is bound to the persistent volume we created perfect. To delete a PVC run the following command
$ kubectl delete pvc myclaim
But what happens to the underlying persistent volume when the claim is deleted.? You can choose what is to happen to the volume.
By default, It is set to retain meaning the persistent volume will remain until it is manually deleted by the administrator. It is not available for reuse by any other claims or it can be deleted automatically.
This way as soon as the claim is deleted the volume will be deleted as well thus freeing up storage on the end storage device or a third option is to recycle. In this case the data in the data volume will be scrubbed before making it available to other claims.