Managing volume snapshots in Kubernetes
You can create a snapshot of a Kubernetes volume to copy its contents at a specific moment in time. This can be useful for restoring volume data in case of data loss.
Volume snapshots are based on custom resource definitions (CRD), so you need to add them to your Kubernetes cluster first.
Prerequisites
- A persistent volume claim is created, as described in Dynamically provisioning persistent volumes.
To add custom resource definitions
Run the following commands:
# git clone https://github.com/kubernetes-csi/external-snapshotter/ cd ./external-snapshotter # git checkout release-5.0 # 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 # 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
The CSI snapshotter version must not be higher than release-5.0.
These commands will create CRDs for VolumeSnapshotClass, VolumeSnapshotContent, VolumeSnapshot, as well as required ClusterRole, ServiceAccount, ClusterRoleBinding, Role, RoleBinding, and finally the snapshot-controller deployment.
You can check that the required resources are successfully created by using Lens, an easy tool for managing Kubernetes clusters and resources.
To create a volume snapshot
-
Create the snapshot-class.yaml file that defines the
VolumeSnapshotClassobject:cat > snapshot-class.yaml <<\EOT apiVersion: snapshot.storage.k8s.io/v1 kind: VolumeSnapshotClass metadata: name: mysnapclass driver: cinder.csi.openstack.org deletionPolicy: Delete parameters: force-create: "true" EOT
This manifest describes the volume snapshot class
mysnapclasswith the deletion policyDelete. This policy allows deleting the underlying storage snapshot along with theVolumeSnapshotContentobject. To keep both the underlying snapshot andVolumeSnapshotContentwhen deleting theVolumeSnapshotobject, set the deletion policy toRetain. -
Create a snapshot class:
$ kubectl apply -f snapshot-class.yaml volumesnapshotclass.snapshot.storage.k8s.io/mysnapclass created
-
Create the snapshot.yaml file that defines the
VolumeSnapshotobject:cat > snapshot.yaml <<\EOT apiVersion: snapshot.storage.k8s.io/v1 kind: VolumeSnapshot metadata: name: mysnapshot spec: volumeSnapshotClassName: mysnapclass source: persistentVolumeClaimName: mypvc EOTThis manifest specifies the volume snapshot
mysnapshotthat uses the volume snapshot classmysnapclassand creates a snapshot of the previously created volume bound to the persistent volume claimmypvc. -
Create a volume snapshot:
$ kubectl create -f snapshot.yaml volumesnapshot.snapshot.storage.k8s.io/mysnapshot created
In the self-service panel, you can find the newly created snapshot
mysnapshoton the Compute -> Volumes -> <pv_name> -> Snapshots tab.
To delete a volume snapshot
Run the following command:
$ kubectl delete -f snapshot.yaml volumesnapshot.snapshot.storage.k8s.io "mysnapshot" deleted