Kubernetes Volumes and Persistent Volumes Explained

March 14, 2025 | 3 minutes read

Containers are ephemeral by nature, meaning data stored inside them disappears when a pod is restarted. To persist data in Kubernetes, we use Volumes and Persistent Volumes.

  1. Kubernetes Volumes: Basics

A Volume in Kubernetes provides storage that can be shared across containers within a pod. Unlike a container’s filesystem, a Kubernetes volume exists as long as the pod exists.

Types of Kubernetes Volumes:

Volume Type Description emptyDir Temporary storage that exists while the pod is running. hostPath Mounts a directory from the worker node’s filesystem. configMap / secret Mounts a ConfigMap or Secret as a file. persistentVolumeClaim Connects to a Persistent Volume (PV) for long-term storage.

  1. Example: Using an emptyDir Volume

An emptyDir volume is created when the pod starts and deleted when the pod stops.

apiVersion: v1
kind: Pod
metadata:
  name: emptydir-example
spec:
  containers:
    - name: app
      image: busybox
      command: ["sleep", "3600"]
      volumeMounts:
        - mountPath: "/data"
          name: my-volume
  volumes:
    - name: my-volume
      emptyDir: {}

Apply the configuration:

kubectl apply -f emptydir-pod.yaml
  1. Persistent Volumes (PVs) and Persistent Volume Claims (PVCs)

A Persistent Volume (PV) is a cluster-wide storage resource. A Persistent Volume Claim (PVC) requests storage from a PV.

Storage Classes:

Kubernetes supports dynamic storage provisioning using StorageClasses, which define how storage should be created.

Example: Persistent Volume (PV)

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

Apply the PV:

kubectl apply -f pv.yaml

Example: Persistent Volume Claim (PVC)

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

Apply the PVC:

kubectl apply -f pvc.yaml

Using a PVC in a Pod

apiVersion: v1
kind: Pod
metadata:
  name: pvc-pod
spec:
  containers:
    - name: app
      image: nginx
      volumeMounts:
        - mountPath: "/data"
          name: storage
  volumes:
    - name: storage
      persistentVolumeClaim:
        claimName: my-pvc

Apply the pod:

kubectl apply -f pvc-pod.yaml

Check the PVC status:

kubectl get pvc
  1. Persistent Volume Reclaim Policies

Kubernetes provides three reclaim policies for Persistent Volumes:

Reclaim Policy Description Retain Keeps the volume even after the PVC is deleted. Recycle Clears the volume but retains it for reuse. (Deprecated) Delete Deletes the storage when the PVC is removed (used for cloud-based storage).

  1. Choosing the Right Storage Option

Storage Requirement Recommended Volume Type Temporary storage within a pod emptyDir Shared storage between containers hostPath (not recommended for production) Configurations or credentials configMap or secret Persistent storage for databases or logs PersistentVolumeClaim

Kubernetes Volumes and Persistent Volumes allow applications to store and persist data beyond the lifecycle of a pod. Understanding when to use ephemeral volumes versus persistent storage is crucial for designing scalable applications in Kubernetes.

popular post

Automating PDF Link Testing Across Multiple Sites Using GitLab CI and Playwright

As a developer or QA engineer, you know the frustration of discovering broken …

Read More

Simplest Way to Deploy a Web App on Kubernetes (K8s)

If you’re looking to quickly deploy a web app and make it accessible via a URL …

Read More

How to Build an AI-Based Search System

In today’s digital landscape, AI-powered search systems are transforming how …

Read More