Day 29/40 Kubernetes Volume, Persistent Volume, Persistent Volume Claim & Storage Class

Kubernetes (K8s) is an open-source container orchestration platform that helps manage and scale containerized applications. One of the most critical aspects of managing containerized apps is handling data, and this is where Kubernetes Volumes come into play.

When containers are ephemeral—meaning they’re short-lived—data generated inside a container can be lost when the container stops or restarts. Kubernetes provides Volumes to ensure that data is persisted and available even after container restarts. This blog simplifies the core concepts of Kubernetes Volumes, diving into Persistent Volumes (PV), Persistent Volume Claims (PVC), and Storage Classes.

Understanding Kubernetes Volumes

In Kubernetes, a Volume is a way to persist data beyond the lifecycle of a single container. Unlike Docker, where volumes are tied to the container, Kubernetes Volumes are mounted to the Pod (a collection of containers). As long as the Pod is alive, the volume stays attached, providing storage for all containers within the Pod.

Kubernetes supports various types of volumes, such as:

  • emptyDir: Temporary storage; data is erased when the Pod is deleted.

  • hostPath: Allows sharing storage between containers and the host system.

  • configMap and secret: Specialized volumes for configuration and sensitive data.

While these basic volumes are useful, they aren’t sufficient for stateful applications requiring durable storage that survives beyond the Pod’s lifecycle. This brings us to Persistent Volumes (PV).

What is a Persistent Volume (PV)?

A Persistent Volume (PV) is a piece of storage in the Kubernetes cluster that an admin provisions. It represents physical storage (like an NFS share, cloud storage, or a block device), abstracted away from the underlying implementation.

Key Features of Persistent Volumes:

  • Lifecycle independence: PVs exist independently of Pods. Even if the Pod using the PV dies, the data remains available.

  • Cluster-wide resource: PVs are resources within the Kubernetes cluster, making them accessible to all Pods that claim them.

  • Storage types: PVs can be backed by a variety of storage backends, such as AWS EBS, Google Cloud Persistent Disks, or NFS.

What is a Persistent Volume Claim (PVC)?

A Persistent Volume Claim (PVC) is a request for storage by a Pod. Users request a PV by creating a PVC, and Kubernetes will match the PVC to an available PV that meets the requested criteria (like size and access modes).

PVC Characteristics:

  • Size & access mode: When creating a PVC, users specify the desired size and the type of access (e.g., read-write).

  • Binding: Kubernetes will automatically find and bind a suitable PV to the PVC based on the requirements.

Persistent Volume Binding

The binding process involves matching a PVC to an available PV. Kubernetes does this automatically:

  1. Static Binding: An admin manually creates PVs, and the PVC requests storage from these pre-created volumes.

  2. Dynamic Binding: When there are no pre-created PVs that match the PVC’s request, Kubernetes can dynamically provision a PV using a StorageClass (we’ll cover this next).

Once bound, the PVC stays linked to the PV, and data will be accessible to the Pods using the PVC.

What is a Storage Class?

A Storage Class in Kubernetes defines how PVs should be provisioned dynamically. It is an abstraction that provides flexibility and automation, allowing Kubernetes to create PVs on demand based on storage requirements.

Key Features:

  • Dynamic provisioning: Storage classes enable the automatic creation of PVs, which reduces the manual overhead.

  • Backend storage: Each StorageClass references a specific type of backend storage (such as AWS EBS, Google Persistent Disk, or NFS).

  • Parameters: You can define parameters in a StorageClass that are specific to the storage provider, such as the type of disk, replication factor, and so on.

1. Create the PersistentVolume (pv-demo)

yamlCopy codeapiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-demo
spec:
  capacity:
    storage: 512Mi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: /data/config
  persistentVolumeReclaimPolicy: Retain

2. Create the PersistentVolumeClaim (pvc-demo)

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-demo
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 256Mi
  storageClassName: ""  # Empty string for storageClass

3. Create the Pod (app) with the PVC mounted

apiVersion: v1
kind: Pod
metadata:
  name: app
spec:
  containers:
  - name: nginx-container
    image: nginx:latest
    volumeMounts:
    - mountPath: /var/app/config
      name: config-volume
  volumes:
  - name: config-volume
    persistentVolumeClaim:
      claimName: pvc-demo

4. Apply the YAML Files

First, you need to create the resources in your Kubernetes cluster. Save each YAML snippet in separate files (e.g., pv.yaml, pvc.yaml, and pod.yaml), and then apply them in order using kubectl:

kubectl apply -f pv.yaml
kubectl apply -f pvc.yaml
kubectl apply -f pod.yaml

5. Verify the PVC is Bound to the PV

You can verify that the PVC has been properly bound to the PV by running the following command:

kubectl get pvc pvc-demo

You should see the status as Bound:

NAME       STATUS   VOLUME     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc-demo   Bound    pv-demo    512Mi      RWX            <none>         ...

6. Open an Interactive Shell to the Pod

Once the Pod is running, you can open an interactive shell inside the nginx container to create a file in the /var/app/config directory:

kubectl exec -it app -- /bin/bash

Once inside the Pod’s shell, navigate to the /var/app/config directory and create a file:

cd /var/app/config
echo "Hello, Kubernetes!" > testfile.txt

7. Verify the File Exists

After creating the file, you can check that it was successfully created by listing the contents of the directory:

ls /var/app/config

You should see testfile.txt listed.

Congratulations! You've now successfully created a PersistentVolume, PersistentVolumeClaim, and mounted it to a Pod, while ensuring that data persistence is handled correctly.

Conclusion

Kubernetes Volumes, especially Persistent Volumes (PVs) and Persistent Volume Claims (PVCs), are crucial for building and managing stateful applications in a containerized world. They offer a clean way to separate storage management from container lifecycles, providing durability and flexibility. Meanwhile, Storage Classes make storage provisioning even more powerful by enabling dynamic allocation, ensuring that applications get the storage they need without manual intervention.

Reference

https://www.youtube.com/watch?v=2NzYX8_lX_0&list=PLl4APkPHzsUUOkOv3i62UidrLmSB8DcGC&index=30

0
Subscribe to my newsletter

Read articles from Rahul Vadakkiniyil directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rahul Vadakkiniyil
Rahul Vadakkiniyil