Snapshots & Cloning
Volume snapshots capture the state of a PersistentVolume at a point in time, allowing you to create backups, restore data, or clone volumes for testing. Volume cloning creates a new volume from an existing volume or snapshot, useful for quickly provisioning similar storage environments.
Volume Snapshots
A volume snapshot represents a point-in-time copy of the data in a PersistentVolume. Snapshots are created using the VolumeSnapshot API and can be used to restore the original volume or create new volumes from the snapshot.
VolumeSnapshot API
Volume snapshots use Kubernetes’ VolumeSnapshot API, which is part of the snapshot.storage.k8s.io API group. This API requires a CSI driver that supports snapshots and a snapshot controller running in your cluster.
Key resources:
- VolumeSnapshot - Represents a snapshot of a volume
- VolumeSnapshotClass - Defines snapshot parameters (similar to StorageClass)
- VolumeSnapshotContent - Represents the actual snapshot in the storage system
Creating a Snapshot
To create a snapshot, you need:
- A VolumeSnapshotClass (defines how snapshots are created)
- A VolumeSnapshot (references a PVC and VolumeSnapshotClass)
Example VolumeSnapshotClass:
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotClass
metadata:
name: csi-snapshot-class
driver: pd.csi.storage.gke.io # CSI driver name
deletionPolicy: Delete # Delete snapshot when VolumeSnapshot is deleted
Example VolumeSnapshot:
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: db-snapshot
spec:
volumeSnapshotClassName: csi-snapshot-class
source:
persistentVolumeClaimName: database-pvc # Snapshot this PVC
Snapshot Lifecycle
Restoring from a Snapshot
You can restore a volume from a snapshot by creating a new PVC that references the snapshot:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: restored-db-pvc
spec:
dataSource:
name: db-snapshot
kind: VolumeSnapshot
apiGroup: snapshot.storage.k8s.io
accessModes:
- ReadWriteOnce
storageClassName: fast-ssd
resources:
requests:
storage: 100Gi # Must be >= original volume size
The restored volume contains the data from the snapshot at the time it was created.
Volume Cloning
Volume cloning creates a new volume from an existing volume or snapshot. Cloning is faster than creating a snapshot and restoring, but requires both volumes to exist simultaneously.
Clone from Volume
Create a new PVC that references an existing PVC:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: cloned-pvc
spec:
dataSource:
name: source-pvc # Clone from this PVC
kind: PersistentVolumeClaim
accessModes:
- ReadWriteOnce
storageClassName: fast-ssd
resources:
requests:
storage: 100Gi # Must be >= source volume size
Important: The source PVC must be bound and in use for cloning to work. Some CSI drivers support cloning from idle PVCs.
Clone from Snapshot
Create a new PVC from a VolumeSnapshot:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: cloned-from-snapshot
spec:
dataSource:
name: db-snapshot
kind: VolumeSnapshot
apiGroup: snapshot.storage.k8s.io
accessModes:
- ReadWriteOnce
storageClassName: fast-ssd
resources:
requests:
storage: 100Gi
Snapshot vs Cloning Comparison
Snapshots:
- Point-in-time copies
- Original volume unchanged
- Can be stored separately
- Useful for backups
- Can create multiple clones
Cloning:
- Direct copy of current state
- Faster than snapshot + restore
- Requires source volume to exist
- Useful for quick provisioning
- Single clone operation
Complete Example: Database Backup and Restore
Here’s a complete example showing how to backup and restore a database:
1. Create a snapshot:
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotClass
metadata:
name: db-snapshot-class
driver: pd.csi.storage.gke.io
deletionPolicy: Retain # Keep snapshot even if VolumeSnapshot is deleted
---
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: db-backup-2024-01-15
spec:
volumeSnapshotClassName: db-snapshot-class
source:
persistentVolumeClaimName: postgres-data
2. Restore from snapshot:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-data-restored
spec:
dataSource:
name: db-backup-2024-01-15
kind: VolumeSnapshot
apiGroup: snapshot.storage.k8s.io
accessModes:
- ReadWriteOnce
storageClassName: fast-ssd
resources:
requests:
storage: 200Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres-restored
spec:
template:
spec:
containers:
- name: postgres
image: postgres:14
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
volumes:
- name: data
persistentVolumeClaim:
claimName: postgres-data-restored
Use Cases
Backup and Recovery
Create regular snapshots for backup purposes:
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: daily-backup-$(date +%Y%m%d)
spec:
volumeSnapshotClassName: backup-snapshot-class
source:
persistentVolumeClaimName: critical-data
Testing and Development
Clone production data for testing:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: test-db-data
spec:
dataSource:
name: prod-db-snapshot
kind: VolumeSnapshot
apiGroup: snapshot.storage.k8s.io
accessModes:
- ReadWriteOnce
storageClassName: standard
resources:
requests:
storage: 100Gi
Data Migration
Use snapshots to migrate data between clusters or storage systems:
- Create snapshot in source cluster
- Export snapshot data
- Import snapshot in destination cluster
- Create PVC from snapshot
Snapshot Deletion Policy
VolumeSnapshotClass has a deletionPolicy field that controls what happens when a VolumeSnapshot is deleted:
- Delete - The snapshot in the storage system is deleted when the VolumeSnapshot is deleted
- Retain - The snapshot in the storage system is retained when the VolumeSnapshot is deleted (manual cleanup required)
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotClass
metadata:
name: backup-snapshot-class
driver: pd.csi.storage.gke.io
deletionPolicy: Retain # Keep snapshot data even if VolumeSnapshot deleted
Checking Snapshot Status
Check snapshot status and details:
# List snapshots
kubectl get volumesnapshot
# Get snapshot details
kubectl describe volumesnapshot db-snapshot
# Check snapshot ready status
kubectl get volumesnapshot db-snapshot -o jsonpath='{.status.readyToUse}'
Best Practices
- Regular snapshots - Create snapshots on a schedule for backups
- Test restores - Regularly test restoring from snapshots
- Use appropriate deletion policy - Retain for backups, Delete for temporary snapshots
- Monitor snapshot storage - Snapshots consume storage space
- Document snapshot procedures - Document how to create and restore snapshots
- Use cloning for quick provisioning - Use cloning when you need copies quickly
- Verify CSI driver support - Ensure your CSI driver supports snapshots/cloning
- Size cloned volumes correctly - Cloned volumes must be >= source volume size
Limitations and Considerations
- CSI driver support - Not all CSI drivers support snapshots and cloning
- Storage costs - Snapshots consume storage space
- Performance impact - Creating snapshots may have a performance impact
- Size requirements - Cloned volumes must be at least as large as the source
- Consistency - Snapshots capture data at a point in time; application consistency may require coordination
Troubleshooting
Snapshot Stuck in Pending
If a snapshot is stuck in Pending:
- Check VolumeSnapshotClass exists:
kubectl get volumesnapshotclass - Verify CSI driver supports snapshots
- Check snapshot controller is running:
kubectl get pods -n kube-system | grep snapshot - Review VolumeSnapshot events:
kubectl describe volumesnapshot <name>
Clone Fails
If cloning fails:
- Verify source PVC is bound:
kubectl get pvc <source-pvc> - Check if CSI driver supports cloning
- Ensure cloned volume size >= source volume size
- Review PVC events:
kubectl describe pvc <cloned-pvc>
See Also
- PVs & PVCs - Creating and using persistent volumes
- CSI Persistent Volumes - Container Storage Interface
- Backup & Restore - Data backup and restore strategies