Kubernetes Dynamic Volume Provisioning Example-DecodingDevOps

Kubernetes Dynamic Volume Provisioning-DecodingDevOps

kubernetes dynamic volume provisioning allows us to create a volume automatically which otherwise has to be done manually by creating underlying storage and attaching it to the persistentVolumeClaim. With dynamic volume provisioning, one can create many storageClass depending on the requirement. Which can be used to create volumes automatically.

Once a storageClass is created we need to reference it into the persistaentVolumeClaim instead of attaching a volume itself, which will assign a volume to PVC. But there's an issue with using dynamic volume provisioning, whenever you delete the pods or scale it down, the attached PVC will be deleted. However, you can change this behavior by using the reclamation policy to Retain.

Kubernetes Dynamic Volume Provisioning Example

Let's see an example of how you can automatically provision volumes.

In order to create a dynamic volume provisioning, we need to create storageClass which can then referenced in PVC.

Creating a storageClass

Create a file named - Storageclass.yaml

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: slow
provisioner: kubernetes.io/gce-pd
parameters:
  type: pd-standard

In type  you can select the type of storage you want to provision.

Use the command below to create a storage class.

kubectl create -f Storageclass.yaml

This will create a storage class. Now let’s create a PVC and reference the storage class in it.

Creating a PVC

Create a file named PersistentVolumeClaim.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: claim1
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: slow
  resources:
    requests:
      storage: 30Gi

Use the command below to create a storage class.

kubectl create -f PersistentVolumeClaim.yaml

Here we have specified storageClassName: slow which will refer to the storageClass by the name slow whenever it’s creating a volume. Now you can use this PVC in the deployment file as usual by using PVC name claim1.

  • Kubernetes Dynamic Volume Provisioning Example