1. Executive Summary
Kubernetes supports volume snapshots, allowing you to capture the contents of a Persistent Volume at a specific point in time.
- Snapshots are useful for restoring volume data in case of accidental deletion, corruption, or data loss.
- Volume snapshots are implemented as Custom Resource Definitions (CRDs) and must be installed on the cluster before use.
- β Creating a snapshot does not modify or move the source volumeβs data.
2. Prerequisites
Before starting, ensure the following:
π¦ Persistent Volume Claim
- A PersistentVolumeClaim (PVC) must already exist.
- See βDynamically Provisioning Persistent Volumesβ for details.
π CSI Snapshotter Version
- The CSI snapshotter version used must not be higher than
release-5.0.
π§ Required Components
- VolumeSnapshotClass, VolumeSnapshotContent, and VolumeSnapshot CRDs
- Snapshot-controller RBAC resources (ClusterRole, ServiceAccount, ClusterRoleBinding, Role, RoleBinding)
- Snapshot-controller deployment
3. Installing the Custom Resource Definitions
This method follows Infrastructure as Code (IaC) best practices using manifests from the upstream external-snapshotter repository.
πΉ Phase A: Clone the Repository
- Clone the
external-snapshotterrepository and check out the supported release:
git clone https://github.com/kubernetes-csi/external-snapshotter/
cd ./external-snapshotter
git checkout release-5.0
πΉ Phase B: Apply the CRDs
- Apply the VolumeSnapshotClass, VolumeSnapshotContent, and VolumeSnapshot CRDs:
kubectl apply -f client/config/crd/snapshot.storage.k8s.io_volumesnapshotclasses.yaml
kubectl apply -f client/config/crd/snapshot.storage.k8s.io_volumesnapshotcontents.yaml
kubectl apply -f client/config/crd/snapshot.storage.k8s.io_volumesnapshots.yaml
πΉ Phase C: Deploy the Snapshot Controller
- Apply the RBAC rules and the snapshot-controller deployment in the
kube-systemnamespace:
kubectl apply -f deploy/kubernetes/snapshot-controller/rbac-snapshot-controller.yaml -n kube-system
kubectl apply -f deploy/kubernetes/snapshot-controller/setup-snapshot-controller.yaml -n kube-system
These commands create:
- CRDs for
VolumeSnapshotClass,VolumeSnapshotContent, andVolumeSnapshot - The required
ClusterRole,ServiceAccount,ClusterRoleBinding,Role, andRoleBinding - The
snapshot-controllerdeployment
4. Creating a Volume Snapshot
πΉ Phase A: Define a VolumeSnapshotClass
- Create a
snapshot-class.yamlfile:
vi snapshot-class.yaml
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotClass
metadata:
name: mysnapclass
driver: cinder.csi.openstack.org
deletionPolicy: Delete
parameters:
force-create: "true"
- This defines the volume snapshot class
mysnapclasswith the deletion policyDelete. Deleteremoves the underlying storage snapshot along with theVolumeSnapshotContentobject when theVolumeSnapshotis deleted.- Set the policy to
Retaininstead to keep both the underlying snapshot and theVolumeSnapshotContentobject after theVolumeSnapshotis deleted.
- Apply the snapshot class:
kubectl apply -f snapshot-class.yaml
Expected output:
volumesnapshotclass.snapshot.storage.k8s.io/mysnapclass created
πΉ Phase B: Define a VolumeSnapshot
- Create a
snapshot.yamlfile:
vi snapshot.yaml
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: mysnapshot
spec:
volumeSnapshotClassName: mysnapclass
source:
persistentVolumeClaimName: mypvc
- This defines the volume snapshot
mysnapshot, based on themysnapclasssnapshot class. - It creates a snapshot of the volume bound to PVC
mypvc.
- Create the volume snapshot:
kubectl create -f snapshot.yaml
Expected output:
volumesnapshot.snapshot.storage.k8s.io/mysnapshot created
π‘ In the self-service panel, the new snapshot appears under Compute β Volumes β [Volume] β Snapshots.
- Also you can verify the snapshots with below command
kubectl get vs
5. Deleting a Volume Snapshot
Run the following command:
kubectl delete -f snapshot.yaml
Expected output:
volumesnapshot.snapshot.storage.k8s.io "mysnapshot" deleted
β οΈ Whether the underlying storage snapshot is also removed depends on the deletionPolicy set on the associated VolumeSnapshotClass (Delete vs. Retain).
6. Safety & Recovery
π Data Integrity
- Creating a snapshot does not alter, move, or delete data on the source volume.
- Snapshots capture volume state at the moment of creation and can be used to restore data later.
β οΈ Version Compatibility
- Using a CSI snapshotter version higher than
release-5.0is not supported and may cause the CRDs or snapshot-controller to behave unexpectedly.
π Best Practice
- Take snapshots on a regular schedule, especially before major changes or upgrades to workloads using the volume.
- Use the
Retaindeletion policy for critical volumes so the underlying snapshot survives accidental deletion of theVolumeSnapshotobject.
β Summary
- Volume snapshots let you capture PVC data at a point in time for recovery purposes.
- Requires:
- External-snapshotter CRDs and snapshot-controller (version β€
release-5.0) - A
VolumeSnapshotClass - A
VolumeSnapshotreferencing an existing PVC - Delete snapshots with
kubectl delete -f snapshot.yamlwhen no longer needed.