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.


To add custom resource definitions

Run the following commands:

# git clone
cd ./external-snapshotter
# git checkout release-5.0
# kubectl apply -f client/config/crd/
# kubectl apply -f client/config/crd/
# kubectl apply -f client/config/crd/
# 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

  1. Create the snapshot-class.yaml file that defines the VolumeSnapshotClass object:

    cat > snapshot-class.yaml <<\EOT
    kind: VolumeSnapshotClass
      name: mysnapclass
    deletionPolicy: Delete
      force-create: "true"

    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.

  2. Create a snapshot class:

    $ kubectl apply -f snapshot-class.yaml created
  3. Create the snapshot.yaml file that defines the VolumeSnapshot object:

    cat > snapshot.yaml <<\EOT
    kind: VolumeSnapshot
      name: mysnapshot
      volumeSnapshotClassName: mysnapclass
        persistentVolumeClaimName: mypvc

    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.

  4. Create a volume snapshot:

    $ kubectl create -f snapshot.yaml 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 "mysnapshot" deleted