Understanding Volumes vs Persistent Volumes in Kubernetes
📖 Scenario: You are working on a Kubernetes cluster where you need to store data generated by your applications. You want to understand the difference between simple Volumes and Persistent Volumes (PV) to manage data storage effectively.
🎯 Goal: Build a simple Kubernetes setup that defines a Pod using a basic Volume and then extend it to use a Persistent Volume and Persistent Volume Claim (PVC). This will help you see how data storage works in Kubernetes and the difference between ephemeral and persistent storage.
📋 What You'll Learn
Create a Pod manifest with an emptyDir Volume
Add a Persistent Volume manifest with specific storage capacity
Create a Persistent Volume Claim manifest to request storage
Modify the Pod manifest to use the Persistent Volume Claim
Print the Pod specification to verify the volume configuration
💡 Why This Matters
🌍 Real World
In real Kubernetes clusters, applications often need to store data that outlives the Pod lifecycle. Understanding Volumes and Persistent Volumes helps manage data persistence and sharing.
💼 Career
DevOps engineers and Kubernetes administrators must know how to configure storage correctly to ensure application data durability and availability.
Progress0 / 4 steps
1
Create a Pod with an emptyDir Volume
Create a Kubernetes Pod manifest named pod.yaml with a Pod called my-pod. Inside the Pod spec, add a container named app-container using the image nginx. Add a Volume named temp-storage of type emptyDir. Mount this volume inside the container at /usr/share/nginx/html.
Kubernetes
Hint
Use emptyDir: {} under volumes to create a temporary storage volume.
2
Define a Persistent Volume with storage capacity
Create a Persistent Volume manifest named pv.yaml with kind PersistentVolume. Name it pv-storage. Set the storage capacity to 1Gi under spec.capacity.storage. Use hostPath with path /mnt/data as the volume source. Set the access mode to ReadWriteOnce.
Kubernetes
Hint
Use hostPath to simulate persistent storage on the node.
3
Create a Persistent Volume Claim and update Pod to use it
Create a Persistent Volume Claim manifest named pvc.yaml with kind PersistentVolumeClaim. Name it pvc-storage. Request 1Gi storage with access mode ReadWriteOnce. Then, update the Pod manifest to remove the emptyDir volume and instead use a volume named persistent-storage that references the claim pvc-storage. Mount this volume inside the container at /usr/share/nginx/html.
Kubernetes
Hint
Use persistentVolumeClaim.claimName in the Pod volume to link the PVC.
4
Print the final Pod manifest to verify volume usage
Print the complete Pod manifest named my-pod to verify it uses the Persistent Volume Claim volume named persistent-storage mounted at /usr/share/nginx/html.
Kubernetes
Hint
Use kubectl get pod my-pod -o yaml to print the Pod manifest.
Practice
(1/5)
1. What is the main difference between a Volume and a Persistent Volume in Kubernetes?
easy
A. Volumes are temporary and tied to pod lifecycle; Persistent Volumes are durable and independent.
B. Volumes are used only for databases; Persistent Volumes are for logs.
C. Volumes require manual creation; Persistent Volumes are created automatically.
D. Volumes can be shared across clusters; Persistent Volumes cannot.
Solution
Step 1: Understand Volume lifecycle
Volumes exist only as long as the pod runs, so they are temporary storage tied to pod lifecycle.
Step 2: Understand Persistent Volume purpose
Persistent Volumes exist independently of pods and provide durable storage that remains after pods are deleted.
Final Answer:
Volumes are temporary and tied to pod lifecycle; Persistent Volumes are durable and independent. -> Option A
Hint: Remember: Volumes die with pods; Persistent Volumes live beyond pods [OK]
Common Mistakes:
Thinking Volumes are durable storage
Confusing Persistent Volumes as pod-specific
Assuming Volumes can be shared across clusters
2. Which of the following is the correct way to declare a Persistent Volume Claim (PVC) in a pod spec?
easy
A. "volumes:\n - name: mypvc\n persistentVolumeClaim:\n claimName: myclaim"
B. "volumes:\n - name: mypvc\n emptyDir: {}"
C. "volumes:\n - name: mypvc\n hostPath:\n path: /data"
D. "volumes:\n - name: mypvc\n configMap:\n name: myconfig"
Solution
Step 1: Identify PVC syntax in pod spec
Persistent Volume Claims are referenced under volumes with persistentVolumeClaim.claimName.
Step 2: Check other volume types
emptyDir is temporary storage, hostPath mounts host directory, configMap mounts config data, none are PVCs.
Final Answer:
volumes:
- name: mypvc
persistentVolumeClaim:
claimName: myclaim -> Option A
Quick Check:
PVC uses persistentVolumeClaim.claimName [OK]
Hint: PVCs use persistentVolumeClaim with claimName in volumes [OK]
Common Mistakes:
Using emptyDir or hostPath instead of persistentVolumeClaim
Missing claimName field
Confusing configMap with PVC
3. Given this pod spec snippet, what type of storage is being used?
volumes:
- name: temp-storage
emptyDir: {}
medium
A. Persistent Volume storage
B. HostPath volume storage
C. Temporary Volume storage tied to pod lifecycle
D. Persistent Volume Claim storage
Solution
Step 1: Identify volume type from spec
The volume uses emptyDir, which is a temporary directory created when the pod starts.
Step 2: Understand emptyDir lifecycle
emptyDir storage is deleted when the pod is removed, so it is temporary and tied to pod lifecycle.
Final Answer:
Temporary Volume storage tied to pod lifecycle -> Option C
Quick Check:
emptyDir = temporary pod storage [OK]
Hint: emptyDir means temporary storage deleted with pod [OK]
Common Mistakes:
Assuming emptyDir is persistent storage
Confusing emptyDir with hostPath
Thinking emptyDir uses Persistent Volume Claims
4. You created a pod with a volume referencing a Persistent Volume Claim, but the pod fails to start with an error about volume not found. What is the most likely cause?
medium
A. The Persistent Volume is deleted automatically when pod is deleted.
B. The pod spec uses emptyDir instead of persistentVolumeClaim.
C. The pod is missing a container image.
D. The Persistent Volume Claim does not exist or is not bound to a Persistent Volume.
Solution
Step 1: Analyze error cause
If the pod cannot find the volume from PVC, likely the PVC does not exist or is not bound to any Persistent Volume.
Step 2: Check other options
Using emptyDir would not cause volume not found error; missing container image causes different error; Persistent Volumes are independent of pods.
Final Answer:
The Persistent Volume Claim does not exist or is not bound to a Persistent Volume. -> Option D
Quick Check:
Volume not found = missing or unbound PVC [OK]
Hint: Check PVC existence and binding if volume not found error occurs [OK]
Common Mistakes:
Assuming emptyDir causes volume not found
Ignoring PVC binding status
Confusing pod image errors with volume errors
5. You want to ensure data stored by your pod remains even if the pod is deleted and recreated. Which combination should you use?
hard
A. Use a Volume with emptyDir in the pod spec.
B. Use a Persistent Volume with a Persistent Volume Claim referenced in the pod spec.
C. Use a hostPath volume pointing to a temporary directory.
D. Use a ConfigMap volume to store data files.
Solution
Step 1: Identify storage durability requirement
Data must persist beyond pod lifecycle, so temporary volumes like emptyDir or hostPath to temp dirs won't work.
Step 2: Choose correct storage type
Persistent Volumes with PVCs provide durable storage independent of pods, ensuring data remains after pod deletion.
Final Answer:
Use a Persistent Volume with a Persistent Volume Claim referenced in the pod spec. -> Option B
Quick Check:
Persistent Volume + PVC = durable storage [OK]
Hint: For durable data, always use Persistent Volume with PVC [OK]
Common Mistakes:
Using emptyDir for persistent data
Using hostPath to temporary directories
Confusing ConfigMap with storage for data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.