Kubernetes  

How to Configure Persistent Volumes in Kubernetes Step by Step

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

  • It exists independently of pods

  • It is managed by Kubernetes

  • It can be reused across different pods

  • It supports multiple storage types (NFS, cloud disks, local storage)

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

  • It requests specific storage size

  • It defines access modes

  • It binds to a matching Persistent Volume

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:

  • PV → Actual storage resource

  • PVC → Request for storage

  • Pod → Uses storage via PVC

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

  • capacity: Defines storage size (1Gi)

  • accessModes: Specifies how the volume can be accessed

  • hostPath: Uses local storage from the node

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

  • Requests 1Gi storage

  • Matches PV based on size and access mode

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

  • Status should show Bound

  • This means PVC is successfully connected to PV

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

  • volumeMounts: Mounts storage inside container

  • volumes: Links PVC to the pod

  • Data stored in /usr/share/nginx/html will persist

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

  • Data persistence across pod restarts

  • Decouples storage from compute

  • Supports scalable applications

  • Works with cloud and on-prem storage

Disadvantages of Persistent Volumes

  • Requires configuration and management

  • Can introduce complexity for beginners

  • Storage performance depends on backend

Real-World Use Cases

  • Databases (MySQL, PostgreSQL)

  • Logging systems

  • File storage services

  • Stateful applications

Best Practices

  • Use StorageClass for dynamic provisioning

  • Monitor storage usage

  • Use appropriate access modes

  • Secure sensitive data

  • Avoid using hostPath in production

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.