📖 Scenario: You are setting up storage for a Kubernetes cluster. You need to create a PersistentVolume (PV) that pods can use to store data persistently, even if the pod restarts or moves.
🎯 Goal: Create a Kubernetes PersistentVolume (PV) YAML definition with specific storage capacity and access mode.
📋 What You'll Learn
Create a PersistentVolume named pv-demo
Set the storage capacity to 5Gi
Use the access mode ReadWriteOnce
Use hostPath as the storage backend with path /mnt/data
💡 Why This Matters
🌍 Real World
PersistentVolumes provide stable storage for applications running in Kubernetes, so data is not lost when pods restart or move.
💼 Career
Understanding PersistentVolumes is essential for Kubernetes administrators and DevOps engineers managing stateful applications.
Progress0 / 4 steps
1
Create the PersistentVolume skeleton
Create a YAML file with apiVersion set to v1, kind set to PersistentVolume, and metadata.name set to pv-demo.
Kubernetes
Hint
Start by defining the basic structure of a PersistentVolume YAML.
2
Add storage capacity and access mode
Add a spec section with capacity.storage set to 5Gi and accessModes set to ReadWriteOnce.
Kubernetes
Hint
Use spec.capacity.storage and spec.accessModes fields.
3
Add hostPath volume source
Inside spec, add hostPath with path set to /mnt/data.
Kubernetes
Hint
The hostPath field defines the directory on the node to use for storage.
4
Display the complete PersistentVolume YAML
Print the complete YAML content of the PersistentVolume named pv-demo.
Kubernetes
Hint
Review the full YAML to confirm all parts are included.
Practice
(1/5)
1. What is the main purpose of a PersistentVolume (PV) in Kubernetes?
easy
A. To manage container images
B. To define network policies for pods
C. To schedule pods on specific nodes
D. To provide storage that can persist beyond the lifecycle of a pod
Solution
Step 1: Understand the role of PersistentVolume
A PersistentVolume provides storage resources independent of pod lifecycle.
Step 2: Compare with other options
Network policies, pod scheduling, and image management are unrelated to PV.
Final Answer:
To provide storage that can persist beyond the lifecycle of a pod -> Option D
Quick Check:
PV = persistent storage [OK]
Hint: PV stores data beyond pod life, not network or scheduling [OK]
Common Mistakes:
Confusing PV with pod scheduling
Thinking PV manages network policies
Assuming PV handles container images
2. Which of the following is the correct minimal YAML snippet to define a PersistentVolume with 5Gi storage and ReadWriteOnce access mode?
A. hostPath volumes cannot have storageClassName set
B. accessModes value ReadWriteOnce is invalid
C. Missing volumeMode field
D. persistentVolumeReclaimPolicy Delete is not allowed
Solution
Step 1: Check hostPath volume restrictions
hostPath volumes do not support storageClassName because they are local paths.
Step 2: Validate other fields
accessModes and reclaim policy values are valid; volumeMode is optional.
Final Answer:
hostPath volumes cannot have storageClassName set -> Option A
Quick Check:
hostPath + storageClassName = error [OK]
Hint: hostPath volumes must not set storageClassName [OK]
Common Mistakes:
Assuming volumeMode is mandatory
Thinking accessModes ReadWriteOnce is invalid
Believing Delete reclaim policy is disallowed
5. You want to create a PersistentVolume that allows multiple pods to read and write simultaneously and automatically deletes the volume when released. Which YAML snippet correctly defines these requirements?