Skip to main content

Command Palette

Search for a command to run...

12: Persistent Volumes in Kubernetes: A Practical Guide with Examples

Updated
4 min read
12: Persistent Volumes in Kubernetes: A Practical Guide with Examples

Kubernetes (K8s) enables powerful storage management for applications deployed in a cluster. Persistent storage is essential for stateful applications that need to retain data beyond the lifecycle of pods. This blog will focus on Kubernetes Persistent Volumes (PV), Persistent Volume Claims (PVC), and Storage Classes (SC) with real-world examples, highlighting how shared storage like NFS, ISCSI, Ceph, or Portworx integrates into a production environment.


Understanding Persistent Storage in Kubernetes

In Kubernetes, persistent storage bridges the gap between dynamic containers and stable data storage. This setup ensures that application data survives pod restarts, scaling, and migrations across nodes.

The persistent storage system in Kubernetes includes:

  1. Persistent Volume (PV) – A cluster-level resource that abstracts storage provisioned by the storage team.

  2. Persistent Volume Claim (PVC) – A request by applications to access a PV.

  3. Storage Class (SC) – Defines the storage type and parameters for dynamic volume provisioning.


How Persistent Storage Works in Production

In a production cluster with shared storage (e.g., NFS, ISCSI, Ceph, Portworx), the flow typically looks like this:

  • The storage team provisions a storage volume on the shared storage system.

  • For instance, on NFS, they export a directory like /app1 and share it using /etc/exports.

  • Kubernetes connects to this shared storage via a Persistent Volume (PV).

  • A Persistent Volume Claim (PVC) is created by the application to request access to the PV.

  • The application uses the PVC to persist its data.

The hierarchy:
PVC → PV → Shared Storage


Access Modes

Each PV supports specific access modes, and the PVC requesting the PV must match its access mode:

  • ReadOnlyMany (ROM): Multiple nodes can read from the volume simultaneously.

  • ReadWriteOnce (RWO): Only one node can read and write to the volume.

  • ReadWriteMany (RWX): Multiple nodes can read and write to the volume simultaneously.

Note: A volume can only use one access mode at a time, even if it supports multiple modes.


Data Reclaim Policies

When a PV is released, its data management depends on the Reclaim Policy:

  • Retain: Keeps the data after the PV is released. Manual cleanup is required.

  • Recycle: Deletes data and makes the volume available for reuse. (Deprecated)

  • Delete: Deletes both the data and the PV.


Steps to Set Up Persistent Storage in Kubernetes

1. Verify Shared Storage

Assume the storage team has set up NFS and provided the server IP and exported directory. For example:

showmount -e 10.0.0.200

Output:

Export list for 10.0.0.200:
/app1

2. Create Persistent Volume (PV)

The PV defines the shared storage and its configuration. Example:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: app1-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany # RWX mode
  persistentVolumeReclaimPolicy: Retain
  nfs:
    path: /app1
    server: 10.0.0.200

3. Create Persistent Volume Claim (PVC)

The PVC requests access to the PV. Ensure the requested access mode matches the PV. Example:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: app1-pvc
spec:
  accessModes:
    - ReadWriteMany # Same as PV
  resources:
    requests:
      storage: 10Gi

4. Use the PVC in a Deployment

Attach the PVC to a pod to persist data. Example:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app1-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: app1
  template:
    metadata:
      labels:
        app: app1
    spec:
      containers:
      - name: app1-container
        image: nginx
        volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: app1-storage
      volumes:
      - name: app1-storage
        persistentVolumeClaim:
          claimName: app1-pvc

Key Points to Remember

  • A PVC’s access mode must match the PV's access mode.

  • Check the volume’s compatibility with the desired access mode.

  • Use kubectl describe pvc <pvc-name> to verify the PVC’s status.

  • Use a reclaim policy appropriate for your use case (e.g., retain for critical data).


Conclusion

Persistent storage is crucial for Kubernetes applications that need stateful operations. By leveraging shared storage like NFS, ISCSI, Ceph, or Portworx, and managing it through PVs, PVCs, and SCs, you can achieve robust data management in your clusters.

Start implementing persistent volumes today, and ensure your applications have the reliable storage they need!

More from this blog

DevOps Journey with M Hassan

174 posts

I am writing these blogs because I recently completed a comprehensive DevOps course where I gained in-depth knowledge of the topics mentioned. As I progressed through the course, I realized the importance of having a concise and accessible resource to revise and reinforce my understanding of each topic. Therefore, I decided to create cheat sheets in the form of blog posts. These cheat sheets will not only serve as a handy reference for myself but also benefit others who are also interested in mastering DevOps concepts. By documenting each topic and providing concise explanations, I aim to create a valuable resource that simplifies complex concepts and facilitates hands-on practice. This way, I can solidify my own understanding while helping others on their DevOps journey.