Kubernetes Handbook

    Kubernetes Volumes vs Persistent Volumes

    Introduction#

    Welcome to our super simple guide on Kubernetes Volumes and Persistent Volumes! These tools let your apps save information in a Kubernetes cluster, either for a short time or forever.

    Imagine writing a quick note on loose paper versus saving it in a diary. Loose paper gets lost easily, but a diary keeps your note safe. In Kubernetes, Kubernetes Volumes are like loose paper for temporary saves, and Persistent Volumes are like diaries for permanent saves. This blog will explain them with the easiest examples and clear steps.

    I once thought I could save info inside a Pod and be done. Big mistake—it got lost as the Pod restarted! 😅 Don’t worry, you’ll learn how to use Kubernetes Volumes and Persistent Volumes to save info the right way.

    Why Do We Need Persistent Volumes?#

    Apps in Kubernetes need to save info, but Pods are temporary—they can break, and their info disappears. Persistent Volumes (PV) keep info safe, no matter what happens.

    Why Persistent Volumes Are Key:#

    • Info Stays Safe: If a Pod breaks, the info is still there for a new Pod.
    • Works on Different Computers: Apps on any computer in the cluster can use the info.
    • Survives Big Problems: Info stays safe even if the whole cluster has issues.

    Persistent Volumes are like a diary that keeps your important notes, unlike loose paper that gets lost.

    Difference Between Volumes and Persistent Volumes#

    Kubernetes Volumes and Persistent Volumes both save info, but they work differently. Here’s a simple table to compare them:

    FeatureVolumesPersistent Volumes
    ScopeOnly for one Pod, like loose paper for one person.For the whole cluster, like a diary anyone can use.
    LifecycleGone when the Pod breaks, like loose paper thrown away.Stays even if Pods break, like a diary kept safe.
    Data PersistenceInfo disappears when the Pod is gone, like loose paper lost.Info stays forever, like a diary’s pages.

    Example: A Volume saves “Get apples” on loose paper, but it’s gone if the Pod breaks. A Persistent Volume saves “Met a friend” in a diary, and it stays safe.

    Types of Persistent Volumes#

    Persistent Volumes come in different types, like different ways to store your diary. Here are three common ones, explained simply:

    • hostPath: Saves info in a folder on one computer, good for testing but not real apps.
    • nfs: Saves info in a shared folder that many computers can use, great for sharing.
    • csi: Uses cloud storage, like Amazon’s EBS or Google’s Disk, perfect for real apps.

    We’ll use hostPath to test, but real apps should use nfs or csi for safety.

    Let’s Try It: Volumes and Persistent Volumes#

    Let’s see Kubernetes Volumes and Persistent Volumes in action with two super easy examples. No apps, just simple notes!

    Example 1: Saving a Grocery List with a Volume#

    Let’s make a Pod save a temporary grocery list, like “Get apples,” using an emptyDir Volume. This is like writing on loose paper that disappears if the Pod breaks.

    Here’s the setup:

    apiVersion: v1 kind: Pod metadata: name: grocery-pod namespace: simple-namespace spec: containers: - name: note-writer image: busybox command: ["/bin/sh", "-c", "echo 'Get apples' > /notes/grocery.txt; sleep 3600"] volumeMounts: - name: note-storage mountPath: /notes volumes: - name: note-storage emptyDir: {}

    Step-by-Step Breakdown of the YAML:

    1. Link the paper: volumeMounts: - name: note-storage, mountPath: /notesThis connects the loose paper to the Pod’s /notes folder, so the app can write there.
    2. Make loose paper: volumes: - name: note-storage, emptyDir: {}This creates a temporary space, like loose paper that disappears when the Pod breaks.
    3. Write the note: command: ["/bin/sh", "-c", "echo 'Get apples' > /notes/grocery.txt; sleep 3600"]This tells the Pod to save “Get apples” on the loose paper.

    Try It:

    Apply it:

    kubectl apply -f grocery-pod.yaml

    Output:

    pod/grocery-pod created

    Check the note:

    kubectl exec -it grocery-pod -n simple-namespace -- cat /notes/grocery.txt

    Output:

    Get apples

    This is perfect for temporary notes, like a grocery list for one day. But if the Pod breaks, the emptyDir Volume and its note are gone.

    Example 2: Saving a Diary Entry with a Persistent Volume#

    Now, let’s save a permanent diary entry, like “Met a friend,” using a Persistent Volume so it stays safe even if the Pod breaks. We’ll use a hostPath PV for testing (use nfs or csi for real apps).

    Here’s the PV setup:

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

    Step-by-Step Breakdown of the YAML:

    1. Make a diary: capacity: storage: 1GiThis Persistent Volume gives your app a diary with 1GiB to write in.
    2. Set access: accessModes: - ReadWriteOnceOnly one Pod on one computer can write in the diary at a time, like one person using it.
    3. Keep it safe: persistentVolumeReclaimPolicy: RetainEven if the Pod is gone, the diary’s notes are kept, like storing it in a drawer.
    4. Point to files: hostPath: path: /mnt/diaryThis uses a folder on the computer (node machine) for testing (not safe for real apps).

    Apply it:

    kubectl apply -f diary-pv.yaml

    Output:

    persistentvolume/diary-pv created

    Check the PV:

    kubectl get pv

    Output:

    NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE diary-pv 1Gi RWO Retain Bound simple-namespace/diary-claim 2m

    Now, let’s make a Pod use this PV (with a temporary PVC for demo, as PVCs come later):

    apiVersion: v1 kind: Pod metadata: name: diary-pod namespace: simple-namespace spec: containers: - name: note-writer image: busybox command: ["/bin/sh", "-c", "echo 'Met a friend' > /notes/diary.txt; sleep 3600"] volumeMounts: - name: note-storage mountPath: /notes volumes: - name: note-storage persistentVolumeClaim: claimName: diary-claim --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: diary-claim namespace: simple-namespace spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi

    Step-by-Step Breakdown of the YAML:

    1. Link the diary: volumeMounts: - name: note-storage, mountPath: /notesThis connects the diary to the Pod’s /notes folder, so the app can write there.
    2. Use the diary: volumes: - name: note-storage, persistentVolumeClaim: claimName: diary-claimThis links the Pod to the diary through a claim, like picking up the diary.
    3. Write the entry: command: ["/bin/sh", "-c", "echo 'Met a friend' > /notes/diary.txt; sleep 3600"]This tells the Pod to save “Met a friend” in the diary.
    4. Claim the diary: kind: PersistentVolumeClaim, name: diary-claimThis asks for a diary with 1GiB that one computer can use, matching the PV.

    Try It:

    Apply it:

    kubectl apply -f diary-pod.yaml

    Output:

    pod/diary-pod created persistentvolumeclaim/diary-claim created

    Check the PV:

    kubectl get pv

    Output:

    NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE diary-pv 1Gi RWO Retain Bound simple-namespace/diary-claim 2m

    Check the note:

    kubectl exec -it diary-pod -n simple-namespace -- cat /notes/diary.txt

    Output:

    Met a friend

    This PV is like a diary—if the Pod breaks, the note in /mnt/diary stays safe for a new Pod. It’s perfect for important info you want to keep.

    Access Modes#

    For PVs, Kubernetes provides three access modes: ReadWriteOnce, ReadOnlyMany, and ReadWriteMany.

    • ReadWriteOnce (RWO): The volume can be mounted as read/write by only a single node. This is ideal for single-instance applications or sharded database storage.
    • ReadOnlyMany (ROX): The volume can be mounted as read-only by multiple nodes simultaneously. This is suitable for use cases like database read replicas.
    • ReadWriteMany (RWX): The volume can be mounted as read/write by many nodes. This is useful for shared resources such as logging, data aggregation, or Network File System (NFS).

    FAQ: Easy Storage Questions#

    Here are some beginner questions answered simply:

    • What if my Pod breaks? A Volume loses its info, like loose paper getting lost. A Persistent Volume keeps your note safe, like a diary.
    • Is hostPath good for real apps? No, it’s tied to one computer and can fail. Use hostPath for testing and nfs or csi for real apps.

    Best Practices for Kubernetes Volumes and Persistent Volumes#

    Here are simple tips to use Kubernetes Volumes and Persistent Volumes well:

    • Use Volumes for Quick Notes: Use emptyDir for temporary stuff, like grocery lists, and hostPath for testing.
    • Use PVs for Important Notes: Save key info, like diary entries, in Persistent Volumes.
    • Keep Access Simple: Use ReadWriteOnce for one-computer apps, ReadWriteMany for sharing if possible.
    • Plan Cleanup: Use Retain for important info, Delete for test cleanup.
    • Test First: Try hostPath in a small cluster, then use nfs or csi for real apps.

    Conclusion#

    You’ve just mastered Kubernetes Volumes and Persistent Volumes, making Kubernetes storage as easy as writing a note! Volumes are like loose paper for quick saves, like grocery lists, and Persistent Volumes are like diaries for permanent saves, like special memories. With these clear examples, you’re ready to save info in your apps.

    Next in our Kubernetes Handbook, we’ll explore Persistent Volume Claims (PVC), where Pods grab storage from Persistent Volumes without any fuss. It’s like picking up a diary without making one!.

    Last updated on May 06, 2025