Skip to main content

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

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 VolumeSnapshotClass object:
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 mysnapclass with the deletion policy Delete. This policy allows deleting the underlying storage snapshot along with the VolumeSnapshotContent object. To keep both the underlying snapshot and VolumeSnapshotContent when deleting the VolumeSnapshot object, set the deletion policy to Retain.

  • 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 VolumeSnapshot object:
cat > snapshot.yaml <<\EOT
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: mysnapshot
spec:
volumeSnapshotClassName: mysnapclass
source:
persistentVolumeClaimName: mypvc
EOT

This manifest specifies the volume snapshot mysnapshot that uses the volume snapshot class mysnapclass and creates a snapshot of the previously created volume bound to the persistent volume claim mypvc.

  • 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 mysnapshot on 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
Was this page helpful?