0
0
Kubernetesdevops~3 mins

Volumes vs Persistent Volumes in Kubernetes - When to Use Which

Choose your learning style9 modes available
The Big Idea

What if your app's data vanished every time it restarted? Persistent Volumes stop that nightmare.

The Scenario

Imagine you run a small bakery and keep all your recipes on sticky notes. Every time you bake, you rewrite the notes and sometimes lose them. Now, imagine scaling to many bakeries, each rewriting and losing recipes independently.

The Problem

Manually managing storage inside containers is like rewriting sticky notes each time. Data disappears when containers stop, and sharing data between containers is messy and error-prone. This slows down work and causes frustration.

The Solution

Volumes and Persistent Volumes in Kubernetes act like a shared recipe book that stays safe and accessible no matter which bakery (container) is open. Volumes provide temporary storage tied to a container's life, while Persistent Volumes keep data safe beyond container restarts, making data management reliable and easy.

Before vs After
Before
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: app
    image: myapp
    volumeMounts:
    - mountPath: /data
      name: temp-storage
  volumes:
  - name: temp-storage
    emptyDir: {}
After
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: app
    image: myapp
    volumeMounts:
    - mountPath: /data
      name: persistent-storage
  volumes:
  - name: persistent-storage
    persistentVolumeClaim:
      claimName: my-pvc
What It Enables

It enables reliable, reusable, and shared storage that survives container restarts and scales with your applications effortlessly.

Real Life Example

A developer working on a web app uses Persistent Volumes to store user uploads so that even if the app restarts or moves to another server, the files remain safe and accessible.

Key Takeaways

Volumes provide temporary storage tied to container life.

Persistent Volumes keep data safe beyond container restarts.

Using Persistent Volumes makes data management reliable and scalable.