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:
| Feature | Volumes | Persistent Volumes |
|---|---|---|
| Scope | Only for one Pod, like loose paper for one person. | For the whole cluster, like a diary anyone can use. |
| Lifecycle | Gone when the Pod breaks, like loose paper thrown away. | Stays even if Pods break, like a diary kept safe. |
| Data Persistence | Info 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:
Step-by-Step Breakdown of the YAML:
- Link the paper:
volumeMounts: - name: note-storage, mountPath: /notesThis connects the loose paper to the Pod’s/notesfolder, so the app can write there. - Make loose paper:
volumes: - name: note-storage, emptyDir: {}This creates a temporary space, like loose paper that disappears when the Pod breaks. - 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:
Output:
Check the note:
Output:
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:
Step-by-Step Breakdown of the YAML:
- Make a diary:
capacity: storage: 1GiThis Persistent Volume gives your app a diary with 1GiB to write in. - Set access:
accessModes: - ReadWriteOnceOnly one Pod on one computer can write in the diary at a time, like one person using it. - Keep it safe:
persistentVolumeReclaimPolicy: RetainEven if the Pod is gone, the diary’s notes are kept, like storing it in a drawer. - Point to files:
hostPath: path: /mnt/diaryThis uses a folder on the computer (node machine) for testing (not safe for real apps).
Apply it:
Output:
Check the PV:
Output:
Now, let’s make a Pod use this PV (with a temporary PVC for demo, as PVCs come later):
Step-by-Step Breakdown of the YAML:
- Link the diary:
volumeMounts: - name: note-storage, mountPath: /notesThis connects the diary to the Pod’s/notesfolder, so the app can write there. - 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. - 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. - 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:
Output:
Check the PV:
Output:
Check the note:
Output:
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
hostPathfor testing andnfsorcsifor 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
emptyDirfor temporary stuff, like grocery lists, andhostPathfor testing. - Use PVs for Important Notes: Save key info, like diary entries, in Persistent Volumes.
- Keep Access Simple: Use
ReadWriteOncefor one-computer apps,ReadWriteManyfor sharing if possible. - Plan Cleanup: Use
Retainfor important info,Deletefor test cleanup. - Test First: Try
hostPathin a small cluster, then usenfsorcsifor 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!.