0
0
Kubernetesdevops~5 mins

Volumes vs Persistent Volumes in Kubernetes - CLI Comparison

Choose your learning style9 modes available
Introduction
When you run apps in Kubernetes, they often need to save data. Volumes let containers store data temporarily, but if the container stops, the data can be lost. Persistent Volumes provide a way to keep data safe even if containers restart or move.
When you want to share data between containers in the same pod temporarily.
When you need to keep data safe even if the pod or container restarts.
When you want to use storage that exists outside the Kubernetes cluster, like cloud disks.
When you want to manage storage separately from pods for better control and reuse.
When you want to back up or migrate data independently of your app lifecycle.
Config File - persistent-volume.yaml
persistent-volume.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: example-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /mnt/data
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: example-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
  - name: app
    image: busybox
    command: ["sleep", "3600"]
    volumeMounts:
    - mountPath: "/data"
      name: storage
  volumes:
  - name: storage
    persistentVolumeClaim:
      claimName: example-pvc

This file creates three things:

  • PersistentVolume (PV): A piece of storage in the cluster with 1Gi size, using a folder on the host machine.
  • PersistentVolumeClaim (PVC): A request for storage by a pod, asking for 1Gi with read-write access.
  • Pod: A pod that uses the PVC to mount the storage at /data inside the container.

This setup keeps data safe even if the pod restarts.

Commands
This command creates the PersistentVolume, PersistentVolumeClaim, and Pod defined in the YAML file. It sets up storage and a pod that uses it.
Terminal
kubectl apply -f persistent-volume.yaml
Expected OutputExpected
persistentvolume/example-pv created persistentvolumeclaim/example-pvc created pod/example-pod created
This command lists all PersistentVolumes in the cluster to check if the PV was created and its status.
Terminal
kubectl get pv
Expected OutputExpected
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE example-pv 1Gi RWO Retain Available 10s
This command lists PersistentVolumeClaims to verify if the PVC is bound to a PV and ready for use.
Terminal
kubectl get pvc
Expected OutputExpected
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE example-pvc Bound example-pv 1Gi RWO 10s
This command writes a file inside the pod's mounted volume and reads it back to confirm the volume is working.
Terminal
kubectl exec example-pod -- sh -c 'echo Hello > /data/hello.txt && cat /data/hello.txt'
Expected OutputExpected
Hello
This command deletes the pod to show that data in the PersistentVolume remains safe after pod removal.
Terminal
kubectl delete pod example-pod
Expected OutputExpected
pod "example-pod" deleted
Key Concept

If you remember nothing else from this pattern, remember: Volumes are temporary storage tied to pods, while Persistent Volumes keep data safe beyond pod lifetimes.

Common Mistakes
Using a volume without a PersistentVolumeClaim for data that must survive pod restarts.
The data will be lost when the pod stops because normal volumes are deleted with the pod.
Use a PersistentVolume and PersistentVolumeClaim to keep data safe beyond pod life.
Creating a PersistentVolumeClaim that requests more storage than the PersistentVolume provides.
The PVC will stay in Pending state because no PV matches the request size.
Ensure the PVC requests equal or less storage than available PV capacity.
Not matching access modes between PersistentVolume and PersistentVolumeClaim.
The PVC will not bind to the PV if access modes differ, causing storage to be unavailable.
Make sure the accessModes in PVC and PV are compatible, like both using ReadWriteOnce.
Summary
Create a PersistentVolume to define storage in the cluster.
Create a PersistentVolumeClaim to request storage for pods.
Mount the PVC in a pod to use persistent storage inside containers.
Verify PV and PVC status to ensure storage is ready.
Data in PersistentVolumes remains safe even if pods are deleted or restarted.