Stage 7 · Master
CKA — Certified Kubernetes Administrator
Storage (10%)
PersistentVolumes, PersistentVolumeClaims, StorageClasses, and reclaim policies.
PV and PVC Concepts
PersistentVolumes (PV) represent storage in the cluster. PersistentVolumeClaims (PVC) are requests for that storage. PVs are cluster resources. PVCs are namespace-scoped requests that bind to PVs.
| Resource | Scope | Purpose |
|---|---|---|
| PersistentVolume (PV) | Cluster | Actual storage resource |
| PersistentVolumeClaim (PVC) | Namespace | Request for storage |
| StorageClass | Cluster | Defines provisioner and parameters |
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: /data/pv
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5GiThe PVC requests 5Gi of storage. It will bind to a PV that has at least 5Gi and supports ReadWriteOnce access. The PV has 10Gi, which is sufficient.
Reclaim Policies
Reclaim policies determine what happens to a PV when its PVC is deleted. This is critical for data management and cost control.
| Policy | Behavior | Use Case |
|---|---|---|
| Retain | PV is kept with data preserved | Critical data, manual management |
| Delete | PV and underlying storage are deleted | Ephemeral data, cost optimization |
| Recycle | PV is cleared and made available again | Deprecated, do not use |
StorageClasses
StorageClasses define how storage is dynamically provisioned. They specify the provisioner, parameters, and reclaim policy. When a PVC references a StorageClass, the system dynamically creates a PV.
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp3
fsType: ext4
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: trueWaitForFirstConsumer delays PV creation until a Pod using the PVC is scheduled. This ensures the PV is created in the correct availability zone for the Pod.
On the CKA exam, you may be asked to create a PVC that uses a specific StorageClass. Make sure the StorageClass exists and its provisioner is available in the cluster.
Dynamic Provisioning
Dynamic provisioning eliminates the need to manually create PVs. When a PVC references a StorageClass, the provisioner automatically creates a PV that matches the request. This is the standard approach in production.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-dynamic-pvc
spec:
accessModes:
- ReadWriteOnce
storageClassName: fast
resources:
requests:
storage: 20GiThis PVC references the 'fast' StorageClass. The provisioner will automatically create a PV matching the requested size and access mode.
Volume Types
- emptyDir — Ephemeral storage, deleted when Pod is removed.
- hostPath — Mounts a file or directory from the node filesystem.
- configMap — Mounts a ConfigMap as a volume.
- secret — Mounts a Secret as a volume.
- persistentVolumeClaim — Mounts a PVC.
- CSI driver — Container Storage Interface for external storage.
hostPath mounts are a security risk — they can expose the host filesystem to containers. Use PersistentVolumes or CSI drivers instead. On the CKA exam, use hostPath only when specifically instructed.
Key Commands
kubectl get pv
kubectl get pvc -A
kubectl get storageclass
kubectl describe pv my-pv
kubectl describe pvc my-pvc -n my-ns
kubectl patch pv my-pv -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'
kubectl delete pvc my-pvc -n my-nsUse kubectl patch pv to change the reclaim policy on an existing PV. This is useful when you want to switch from Delete to Retain to prevent data loss.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.