Persistent Volume Claims
Persistent Volume Claimsadd storage to a node.Persistent VolumesandPersistent Volume Claimsare two separate objects in the Kubernetes namespace.- An Administrator creates
Persistent Volumesand a user makes thePersistent Volume Claims. - Once created, Kubernetes binds the set
Persistent Volumeand itsPersistent Volume Claimtogether. - Kubernetes tries to find a
Persistent Volume, that has the right capacity asked for by the claim.- Kubernetes looks for the following:
Sufficient CapacityAccess ModesVolume ModesStorage Class
- Kubernetes looks for the following:
- If there are multiple
Persistent Volume Claimsaimed at onePersistent Volume, then you can uselabelsandselectorsfor that. For example with aPersistent Volume:labels: name: my-pw - For the
Persistent Volume Claim:selector: matchLabels: name: my-pv - A smaller
Persistent Volume Claim, may be bound to a largerPersistent Volume, if there are no better options available. - There is a 1:1 relationship with
Persistent VolumesandPersistent Volume Claims, so even if there is leftover space on aPersistent Volume, no otherPersistent Volume Claimscan use it. - If there are no other
Persistent Volumesavailable and aPersistent Volume Claimis looking for aPersistent Volumeto bind with, thatPersistent Volume Claimwill go into aPendingstate.- When newer
Persistent Volumesare available, thePersistent Volume Claimcan then be bound successfully.
- When newer
- Example
Persistent Volume Claimofpvc-definition.yaml: ``` apiVersion: v1 kind: PersistentVolumeClaim metdata: name: myclaim spec: accessModes:- ReadWriteOnce resources: requests: storage: 500Mi ```
- Create the
Persistent Volume Claimdefinition file with this command:kubectl create -f pvc-definition.yaml - Check the
Persistent Volume Claimswith:kubectl get persistentvolumeclaim - When a
PVCis created, Kubernetes looks at the volume that was made previously. - Previously the following
Persistent Volumewas made available. Pairing this with thePersistent Volume Claimabove will allow it to be bound (albeit wasting 500MB of storage).apiVersion: v1 kind: PersistentVolume metadata: name: pv-vol1 spec: accessModes: - ReadWriteOnce capacity: storage: 1Gi awsElasticBlockStore: volumeID: <volume-id> fsType: ext4 - Running the
kubectl get persistentvolumeclaimcommand again shows the following:Name STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE myclaim Bound pv-vol1 1Gi RWO 43m - To delete a PVC, run the following:
kubectl delete persistentvolumeclaim myclaim - What happens to the underlying
Persistent Volumewhen the claim is deleted?- By default, the
persistentVolumeReclaimPolicyis set toRetain persistentVolumewill remain until it is manually deleted.- Not available for reuse by any other claims!
- By default, the
- Else it can be deleted with the following:
persistentVolumeReclaimPolicy: Delete - The above deletes the
Persistent Volume Claimautomatically. - A third option is:
persistentVolumeReclaimPolicy: Recycle - For the above, the data in the data volume will be scrubbed, before it is available to other claims.