Sub topic 18
Volume snapshots
This chapter assumes you know PersistentVolumeClaims, StorageClasses, and CSI drivers from the earlier chapters. A volume snapshot is a point-in-time copy of a volume's data. You can use it as a backup point, or as the starting content for a new volume.
What you need first
Snapshots are not built into core Kubernetes. Three things must be present:
- The snapshot CRDs (VolumeSnapshot, VolumeSnapshotContent, VolumeSnapshotClass).
- The snapshot controller, which most distributions or add-ons install.
- A CSI driver that supports snapshots. In-tree drivers and drivers without this capability cannot take them.
The three objects
The objects mirror the PV, PVC, and StorageClass split:
| Snapshot object | Counterpart | Role |
|---|---|---|
| VolumeSnapshotClass | StorageClass | Names the driver and the deletion policy |
| VolumeSnapshot | PersistentVolumeClaim | A namespaced request for a snapshot |
| VolumeSnapshotContent | PersistentVolume | The cluster-scoped record of the real snapshot |
Take a snapshot
First define a class. The driver must match your StorageClass provisioner.
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotClass
metadata:
name: ebs-snap
driver: ebs.csi.aws.com
deletionPolicy: DeleteThen request a snapshot of an existing claim in the same namespace:
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: demo-snap
spec:
volumeSnapshotClassName: ebs-snap
source:
persistentVolumeClaimName: demo-pvcCheck progress with kubectl get volumesnapshot. The snapshot is usable when READYTOUSE is true.
Restore into a new claim
A restore is a new PVC whose dataSource points at the snapshot:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: demo-restored
spec:
storageClassName: fast
dataSource:
name: demo-snap
kind: VolumeSnapshot
apiGroup: snapshot.storage.k8s.io
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1GiThe requested size must be at least the snapshot's restore size. The provisioner creates a new volume filled with the snapshot data. The original claim is not changed.
Common errors
- Snapshot never becomes ready. The CRDs or controller are missing, or the class names the wrong driver. Run
kubectl describe volumesnapshot demo-snapand read the events. - Restore claim stays Pending. The StorageClass must use the same driver that took the snapshot. Check the events on the claim as well.
- Snapshot vanishes with its source. With
deletionPolicy: Delete, deleting the VolumeSnapshot also deletes the backing snapshot. UseRetainif the data must outlive the Kubernetes object. - Inconsistent data. A snapshot of a busy database may capture a partial write. Quiesce the application first, or use database-level backups.
Exercise
A VolumeSnapshot has been created, but you cannot restore from it yet. Which field do you check first, and what do you set on the new PVC to restore?
Show answer
Check that READYTOUSE is true in kubectl get volumesnapshot. To restore, set spec.dataSource on the new PVC with kind VolumeSnapshot, the snapshot name, and apiGroup snapshot.storage.k8s.io.