Create a PersistentVolumeClaim (PVC) in Kubernetes
📖 Scenario: You are setting up storage for a Kubernetes application. You need to create a PersistentVolumeClaim (PVC) to request storage from the cluster.
🎯 Goal: Learn how to write a YAML definition for a PersistentVolumeClaim (PVC) that requests storage with specific size and access mode.
📋 What You'll Learn
Create a YAML dictionary with apiVersion, kind, metadata, and spec fields
Set apiVersion to v1
Set kind to PersistentVolumeClaim
Add metadata with a name my-pvc
In spec, set accessModes to ReadWriteOnce
In spec, set resources.requests.storage to 1Gi
💡 Why This Matters
🌍 Real World
Kubernetes applications often need persistent storage to save data beyond the life of a pod. PVCs let you request this storage easily.
💼 Career
Understanding PVCs is essential for Kubernetes administrators and DevOps engineers to manage stateful applications and storage resources.
Progress0 / 4 steps
1
Create the basic PVC structure
Create a YAML dictionary with apiVersion set to v1, kind set to PersistentVolumeClaim, and metadata with name set to my-pvc.
Kubernetes
Hint
Start by defining the top-level keys and the metadata name.
2
Add access mode to the spec
Add a spec section with accessModes set to a list containing ReadWriteOnce.
Kubernetes
Hint
Remember that accessModes is a list under spec.
3
Add storage request to the spec
Under spec, add resources.requests.storage set to 1Gi to request 1 gigabyte of storage.
Kubernetes
Hint
Indent resources, then requests, then set storage to 1Gi.
4
Display the complete PVC YAML
Print the complete YAML definition of the PersistentVolumeClaim.
Kubernetes
Hint
Show the full YAML content as the final output.
Practice
(1/5)
1. What is the main purpose of a PersistentVolumeClaim (PVC) in Kubernetes?
easy
A. To configure network policies
B. To define a container image for a pod
C. To schedule pods on specific nodes
D. To request storage resources from the cluster
Solution
Step 1: Understand PVC role
A PVC is used to request storage resources in Kubernetes, separating storage use from storage details.
Step 2: Compare options
Options A, B, and D relate to other Kubernetes features, not storage requests.
Final Answer:
To request storage resources from the cluster -> Option D
Quick Check:
PVC = Storage request [OK]
Hint: PVC always requests storage, not images or network [OK]
Common Mistakes:
Confusing PVC with Pod or Deployment definitions
Thinking PVC schedules pods
Mixing PVC with network policies
2. Which of the following is the correct minimal YAML snippet to define a PVC requesting 5Gi of storage with ReadWriteOnce access mode?
Kubernetes expects storage size units like Gi, Mi, not GB. '2GB' is invalid.
Step 2: Verify other fields
accessModes and metadata name are correct. apiVersion for PVC is v1, not apps/v1.
Final Answer:
storage size unit should be Gi, not GB -> Option C
Quick Check:
Use Gi for storage size units [OK]
Hint: Use Gi, Mi units for storage size, not GB [OK]
Common Mistakes:
Using GB instead of Gi for storage size
Wrong apiVersion for PVC
Changing accessModes unnecessarily
5. You want to create a PVC that requests 20Gi storage with ReadWriteOnce access and uses a storage class named "premium-ssd". Which YAML snippet correctly defines this PVC?
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: premium-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
storageClassName: premium-ssd correctly sets storageClassName to premium-ssd and uses Gi units. Other options have wrong accessModes, use limits instead of requests, or wrong units (GB).
Final Answer:
Correct PVC with 20Gi, ReadWriteOnce, and premium-ssd storage class -> Option A