Introduction

When working with Kubernetes, one common challenge developers face is handling data persistence. By default, containers are ephemeral, meaning any data stored inside a container is lost when the container is restarted or deleted.

In real-world applications such as databases, logging systems, and file storage services, data must be preserved even if the container lifecycle changes. This is where Kubernetes Persistent Volumes (PV) and Persistent Volume Claims (PVC) come into play.

This article provides a detailed, step-by-step guide on how to configure persistent volumes in Kubernetes, along with explanations, examples, use cases, and best practices.

What is a Persistent Volume (PV)?

A Persistent Volume (PV) is a piece of storage in a Kubernetes cluster that has been provisioned either manually by an administrator or dynamically using a StorageClass.

Key Characteristics of PV

Real-World Example

Consider a MySQL database running inside a Kubernetes pod. If the pod crashes and restarts, the data inside the container will be lost unless it is stored in a Persistent Volume.

What is a Persistent Volume Claim (PVC)?

A Persistent Volume Claim (PVC) is a request for storage by a user or application.

Key Characteristics of PVC

Real-World Analogy

Think of PV as a physical hard drive and PVC as a request to use a portion of that drive.

Kubernetes Storage Architecture

The relationship between components:

This separation allows better flexibility and management of storage resources.

Step 1: Create a Persistent Volume (PV)

First, define a Persistent Volume using a YAML configuration file.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"

Code Explanation

Apply the configuration:

kubectl apply -f pv.yaml

Step 2: Create a Persistent Volume Claim (PVC)

Now create a PVC to request storage.

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

Code Explanation

Apply the configuration:

kubectl apply -f pvc.yaml

Step 3: Verify PV and PVC Binding

Run the following command:

kubectl get pv
kubectl get pvc

Explanation

Step 4: Use PVC in a Pod

Now attach the PVC to a pod.

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: nginx
    volumeMounts:
    - mountPath: "/usr/share/nginx/html"
      name: storage
  volumes:
  - name: storage
    persistentVolumeClaim:
      claimName: my-pvc

Code Explanation

Apply the configuration:

kubectl apply -f pod.yaml

Step 5: Test Data Persistence

Execute into the pod:

kubectl exec -it my-pod -- /bin/bash

Create a file:

echo "Hello Kubernetes" > /usr/share/nginx/html/test.txt

Delete the pod:

kubectl delete pod my-pod

Recreate the pod and check the file again.

Explanation

If the file still exists, it confirms that data is stored in the Persistent Volume.

Access Modes in Kubernetes

ModeDescription
ReadWriteOnceMounted as read-write by a single node
ReadOnlyManyMounted as read-only by many nodes
ReadWriteManyMounted as read-write by multiple nodes

Advantages of Persistent Volumes

Disadvantages of Persistent Volumes

Real-World Use Cases

Best Practices

Summary

Persistent Volumes in Kubernetes provide a reliable way to store data beyond the lifecycle of containers. By using PV and PVC, you can decouple storage from applications, ensuring data safety and scalability. Proper configuration and understanding of storage concepts are essential for building production-ready Kubernetes applications.