Stage 5 · Platform
Stateful Storage Operations
CSI Snapshots & Clones
VolumeSnapshotClass, snapshot controllers, PVC cloning, and restore validation.
Snapshot Overview
CSI snapshots capture the state of a volume at a point in time. They are used for backups, data protection, and creating test environments from production data. Snapshots are managed through VolumeSnapshot and VolumeSnapshotClass CRDs.
1. Create VolumeSnapshotClass (defines how to snapshot)
2. Create VolumeSnapshot (triggers snapshot creation)
3. CSI driver creates snapshot in storage system
4. Snapshot is ready — can be used for restore
5. Create PVC from snapshot (restore)Snapshots are incremental in most CSI drivers — only changed blocks are stored. This makes them fast and space-efficient. Snapshots can be restored to new PVCs for point-in-time recovery.
VolumeSnapshotClass
VolumeSnapshotClass defines how snapshots are created. It specifies the CSI driver, deletion policy, and driver-specific parameters. Similar to StorageClass but for snapshots.
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotClass
metadata:
name: csi-snapclass
driver: ebs.csi.aws.com
deletionPolicy: Retain
parameters:
encrypted: "true"
tagSpecification1: "Environment=production"deletionPolicy: Retain preserves snapshots even after the VolumeSnapshot CR is deleted. Delete removes the snapshot from the storage system. Use Retain for critical data, Delete for temporary snapshots.
Creating Snapshots
VolumeSnapshot triggers a snapshot of a PVC. The CSI driver creates the snapshot asynchronously. The snapshot is ready when the readyToUse field is true. Snapshot creation is usually fast (seconds) because most CSI drivers use incremental snapshots.
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: db-snapshot-daily
namespace: production
spec:
volumeSnapshotClassName: csi-snapclass
source:
persistentVolumeClaimName: postgres-data
---
# Scheduled snapshot with CronJob
apiVersion: batch/v1
kind: CronJob
metadata:
name: daily-snapshot
spec:
schedule: "0 1 * * *"
jobTemplate:
spec:
template:
spec:
serviceAccountName: snapshot-creator
containers:
- name: snapshot
image: bitnami/kubectl:latest
command:
- /bin/sh
- -c
- |
TIMESTAMP=$(date +%Y%m%d-%H%M)
kubectl create volume-snapshot \
db-snapshot-$TIMESTAMP \
--source=pvc/postgres-data \
--snapshot-class=csi-snapclass
restartPolicy: OnFailureThe CronJob creates a timestamped snapshot daily. The snapshot-creator ServiceAccount needs RBAC permissions to create VolumeSnapshots. Retention can be managed with a separate cleanup job.
Restoring from Snapshots
Restore a snapshot by creating a PVC with a dataSource pointing to the VolumeSnapshot. The CSI driver creates a new volume initialized with the snapshot data. The restored volume must be at least as large as the snapshot source.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-restored
namespace: production
spec:
accessModes:
- ReadWriteOnce
storageClassName: fast-ssd
resources:
requests:
storage: 100Gi
dataSource:
name: db-snapshot-daily
kind: VolumeSnapshot
apiGroup: snapshot.storage.k8s.ioThe PVC uses dataSource to specify the snapshot. The CSI driver creates a new volume with the snapshot data. The restored PVC must be at least as large as the original snapshot source PVC.
PVC Cloning
PVC cloning copies an existing PVC to a new PVC. It uses the same dataSource mechanism but points to an existing PVC instead of a snapshot. Clones are useful for creating test environments from production data.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-clone
namespace: staging
spec:
accessModes:
- ReadWriteOnce
storageClassName: fast-ssd
resources:
requests:
storage: 100Gi
dataSource:
name: postgres-data
kind: PersistentVolumeClaimThe clone PVC uses dataSource pointing to the source PVC. The CSI driver copies the data to a new volume. The clone must be the same size or larger than the source. Clones are CSI-driver dependent — not all drivers support them.
Snapshot Validation
Validate snapshots after creation and before restore. Check readyToUse status, verify snapshot content, and test restore to ensure data integrity. Automated validation prevents using corrupted snapshots.
# Check snapshot status
kubectl get volumesnapshot db-snapshot-daily -o yaml | jq .status
# Verify readyToUse
kubectl get volumesnapshot db-snapshot-daily \
-o jsonpath='{.status.readyToUse}'
# List snapshot content
kubectl get volumesnapshotcontent
# Test restore — create PVC from snapshot and verify data
kubectl create -f restore-pvc.yaml
kubectl exec -it test-pod -- ls -la /dataAlways verify readyToUse is true before using a snapshot. Check the snapshot size matches expectations. Test a restore to verify data integrity before relying on snapshots for disaster recovery.
Manage snapshot retention with TTL (time-based) or count-based policies. Create a CronJob to delete old snapshots. For EBS snapshots, use AWS lifecycle policies. For CSI snapshots, use the VolumeSnapshot's deletionPolicy.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.