peter (admin)   -  



 

Kubernetes Storage Classes and Dynamic Provisioning

In this section, we will learn how to check available StorageClasses, create a Persistent Volume Claim without manually creating a Persistent Volume, and verify how Kubernetes automatically provisions storage and creates a Persistent Volume dynamically for our request.

What Is a StorageClass?

A StorageClass tells Kubernetes how to automatically create storage when it is needed instead of requiring storage to be created manually each time.

In simple terms, a StorageClass defines the type of storage, such as fast disk, slow disk, or cloud storage, and how that storage should be created.

Previously, we had to create a Persistent Volume first and then connect it to a Persistent Volume Claim. This was a manual process.

With StorageClasses, Kubernetes can create the storage automatically when a storage request is made.

Relationship Between PV, PVC, and StorageClass

A Persistent Volume is the actual storage, while a Persistent Volume Claim is the request for that storage.

When using a StorageClass, we only need to create a PVC. Kubernetes can then automatically create a matching PV in the background.

This process is called dynamic provisioning.

For example, if an application needs 1 GiB of storage, it can create a PVC requesting that amount of storage. Kubernetes uses the StorageClass to automatically create a suitable PV and bind it to the PVC.

This makes storage management faster and easier because we do not need to create Persistent Volumes manually.

The relationship can be represented as:

Application / Pod
       |
       v
Persistent Volume Claim
       |
       | requests storage
       v
StorageClass
       |
       | dynamically provisions
       v
Persistent Volume
       |
       v
Underlying Storage

Checking the Kubernetes Cluster

For this demonstration, we are already logged into both the master node and the worker node.

Verify the cluster:

kubectl get nodes

Both the master and worker nodes should be in the Ready state. This confirms that the cluster is running and both nodes are properly connected.

Checking Available StorageClasses

First, check the available StorageClasses:

kubectl get storageclass

If no resources are found, there is currently no StorageClass configured in the cluster.

Without a StorageClass, Kubernetes cannot dynamically provision storage. Persistent Volumes therefore need to be created manually.

Creating a Dynamic PVC

Create a YAML file named:

pvc-dynamic.yaml

The purpose of this file is to define a Persistent Volume Claim that requests storage dynamically.

The PVC specifies how much storage is required and what access mode is needed. Kubernetes can then use a StorageClass to attempt to provision the required storage automatically.

Configuring the Dynamic PVC

The YAML configuration defines the Persistent Volume Claim.

The apiVersion specifies the Kubernetes API version.

The kind identifies the resource as a PersistentVolumeClaim.

The PVC is given the name:

dynamic-pvc

The access mode is set to:

accessModes:
  - ReadWriteOnce

This means that the storage can be mounted as read-write by one node at a time.

The PVC requests 1 GiB of storage:

resources:
  requests:
    storage: 1Gi

Because no Persistent Volume is being created manually, Kubernetes will attempt to create and assign one through a StorageClass. This is dynamic provisioning.

Example Dynamic PVC Configuration

The configuration can be written as:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: dynamic-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Save the file and exit the editor.

Creating the PVC

Apply the configuration:

kubectl apply -f pvc-dynamic.yaml

Kubernetes creates the Persistent Volume Claim and attempts to provision storage automatically.

Check the PVC:

kubectl get pvc

Initially, the dynamic-pvc claim may be in the Pending state.

This means that the claim is waiting for storage and has not yet been connected to a Persistent Volume.

In this example, the cluster does not yet have a StorageClass configured, so Kubernetes cannot dynamically create storage.

There is also no available Persistent Volume matching the request.

Checking Existing Persistent Volumes

Check the existing Persistent Volumes:

kubectl get pv

The existing my-pv volume was created manually in an earlier step. It is already in the Bound state with my-pvc, so it is not available for the new dynamic claim.

Therefore, the dynamic-pvc remains in the Pending state because Kubernetes has no suitable storage available.

Installing a Local Path Provisioner

For testing purposes, a local path provisioner can be installed into the cluster.

The local path provisioner creates a StorageClass and allows Kubernetes to automatically create storage using the local disk of a node.

This is useful in a local Kubernetes environment where no StorageClass or automatic storage provisioning is available by default.

After applying the provisioner's YAML configuration, several resources are created, including:

  • A namespace

  • A service account

  • Roles

  • Role bindings

  • A deployment

  • A StorageClass

The StorageClass allows the cluster to dynamically provision storage when a PVC requests it.

Verifying the StorageClass

Check the StorageClasses again:

kubectl get storageclass

A StorageClass named local-path should now be available.

The provisioner is:

rancher.io/local-path

This means that storage is provisioned using the local disk of the node.

The reclaim policy is:

Delete

This means that when the PVC is deleted, the dynamically provisioned storage will also be removed.

The volume binding mode is:

WaitForFirstConsumer

This means that storage is not provisioned until a pod actually uses the PVC.

The cluster is now ready for dynamic provisioning.

Assigning the StorageClass to the PVC

Edit the existing pvc-dynamic.yaml file and add a storageClassName:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: dynamic-pvc
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: local-path
  resources:
    requests:
      storage: 1Gi

The storageClassName tells Kubernetes which StorageClass to use.

In this example:

local-path

is the StorageClass provided by the local path provisioner.

Apply the updated configuration:

kubectl apply -f pvc-dynamic.yaml

Kubernetes can now use the StorageClass to dynamically provision a Persistent Volume for the claim.

Checking the Dynamic PVC

Check the PVC:

kubectl get pvc

The PVC should now show the local-path StorageClass.

It may initially remain in the Pending state because the StorageClass uses the WaitForFirstConsumer binding mode.

This means Kubernetes waits until a pod actually uses the PVC before creating the storage.

Creating a Pod That Uses the Dynamic PVC

Create a pod using a YAML file.

Using YAML allows the pod configuration and storage configuration to be defined in one place.

The pod is named:

dynamic-test

The container uses the NGINX image.

The storage is mounted inside the container at:

/data

The volume uses the dynamic-pvc claim.

An example configuration is:

apiVersion: v1
kind: Pod
metadata:
  name: dynamic-test
spec:
  containers:
    - name: nginx
      image: nginx
      volumeMounts:
        - name: dynamic-storage
          mountPath: /data
  volumes:
    - name: dynamic-storage
      persistentVolumeClaim:
        claimName: dynamic-pvc

This configuration connects the pod to the dynamically provisioned storage.

The container can now read and write data through /data.

Creating the Pod

Apply the pod configuration:

kubectl apply -f pod.yaml

Kubernetes creates the pod and connects it to the Persistent Volume Claim.

Because the StorageClass uses WaitForFirstConsumer, this causes Kubernetes to provision the storage.

If the PVC is still showing as Pending, wait a few seconds and check it again. If necessary, the pod can be recreated so Kubernetes can retry the provisioning process.

Verifying Dynamic Provisioning

Check the PVC:

kubectl get pvc

The dynamic-pvc should now be in the Bound state.

This means Kubernetes has successfully created a Persistent Volume for the claim using the StorageClass.

The generated volume name will be different from the manually created my-pv, demonstrating that Kubernetes created the volume dynamically.

The PVC should show:

  • Status: Bound

  • Capacity: 1Gi

  • Access mode: RWO

  • StorageClass: local-path

The storage is being provided by the local disk of the node.

Verifying the Dynamically Created Persistent Volume

Check the Persistent Volumes:

kubectl get pv

There should now be two volumes.

The first volume is the manually created my-pv, which is already bound to my-pvc.

The second volume has a generated name and was automatically created by Kubernetes for dynamic-pvc.

The dynamically created volume should show:

StorageClass: local-path
Capacity:     1Gi
Access Mode:  RWO
Reclaim Policy: Delete

The generated name and local-path StorageClass confirm that this Persistent Volume was created using dynamic provisioning.

The Delete reclaim policy means that the dynamically provisioned storage will be removed when the associated PVC is deleted.

Summary

StorageClasses allow Kubernetes to automatically provision Persistent Volumes when applications request storage.

In this example:

  • We checked the available StorageClasses.

  • Initially, no StorageClass was configured.

  • A dynamic PVC was created requesting 1Gi of storage.

  • The PVC initially remained Pending.

  • A local path provisioner was installed.

  • The local-path StorageClass became available.

  • The PVC was configured to use local-path.

  • A pod was created using the PVC.

  • Kubernetes dynamically created a Persistent Volume.

  • The PVC changed to the Bound state.

  • The dynamically created PV received a generated name.

  • The dynamically created storage uses the local disk of the node.

This demonstrates how to check and create StorageClasses, assign a StorageClass to a Persistent Volume Claim, and allow Kubernetes to dynamically provision a Persistent Volume without manually creating the PV.

 


Comments (0)

There are no comments yet. Be the first to comment.

Log in or register to leave a comment. all comments are subject to admin approval