In Volume we discussed how we can create a PV and a PVC to bind to that PV and finally configure a pod to use the PVC to get a persistent volume.
The problem with this approach is that we need to manually provision the storage on a cloud provider or storage device before we can create a PV using it. This is called as static provisioning.
In dynamic provisioning, a provisioner is created which can automatically provision storage on the cloud or storage device and attach them to the pod when the claim is made. Dynamic provisioning is achieved by creating a StorageClass
object.
When using storage classes, we don’t need to create PVs manually. When a PVC is created with a storage class, the storage class uses a provisioner to automatically provision storage and create a PV to bind to the PVC.
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: gcp-storage
provisioner: kubernetes.io/gce-pd
provisioner
depends on the type of underlying storage being used (EBS, AzureDisk, etc.)
provisioner: kubernetes.io/no-provisioner
means dynamic provisioning is disabled.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: myclaim
spec:
storageClassName: gcp-storage
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
Depending on the provisioner
, there are some properties that we can specify to control the behavior of the underlying storage.
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: gcp-storage
provisioner: kubernetes.io/gce-pd
parameters:
type: pd-ssd
replication-type: regional-pd
Using these properties, we can create classes of storage such as silver, gold & platinum with increasing levels of replication and speed.
reclaimPolicy
defines the behavior of the PV when the PVC is deleted
Delete
- delete the PV when the PVC is deletedRetain
- retain the PV when the PVC is deletedvolumeBindingMode
defines when the volume should be created and bound to a PVC
WaitForFirstConsumer
- wait for a pod to use the PVCImmediate
- immediately create a volume and bind it to the PVCapiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: gcp-storage
provisioner: kubernetes.io/gce-pd
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer