CSI VolumeSnapshot Associated with Longhorn Snapshot

Snapshot in Longhorn is an object that represents content of a Longhorn volume at a particular moment. It is stored inside the cluster.

To programmatically create Longhorn snapshots, you can use the generic Kubernetes CSI VolumeSnapshot mechanism. To learn more about the CSI VolumeSnapshot mechanism, click here.

Prerequisite: CSI snapshot support needs to be enabled on your cluster. If your kubernetes distribution does not provide the kubernetes snapshot controller as well as the snapshot related custom resource definitions, you need to manually deploy them. For more information, see Enable CSI Snapshot Support.

Create A CSI VolumeSnapshot Associated With Longhorn Snapshot

To create a CSI VolumeSnapshot associated with a Longhorn snapshot, you first need to create a VolumeSnapshotClass object with the parameter type set to snap as follow:

  1. kind: VolumeSnapshotClass
  2. apiVersion: snapshot.storage.k8s.io/v1
  3. metadata:
  4. name: longhorn-snapshot-vsc
  5. driver: driver.longhorn.io
  6. deletionPolicy: Delete
  7. parameters:
  8. type: snap

For more information about VolumeSnapshotClass, see the kubernetes documentation for VolumeSnapshotClasses.

After that, create a Kubernetes VolumeSnapshot object with volumeSnapshotClassName points to the name of the VolumeSnapshotClass (longhorn-snapshot-vsc) and the source points to the PVC of the Longhorn volume for which a Longhorn snapshot should be created.

  1. apiVersion: snapshot.storage.k8s.io/v1
  2. kind: VolumeSnapshot
  3. metadata:
  4. name: test-csi-volume-snapshot-longhorn-snapshot
  5. spec:
  6. volumeSnapshotClassName: longhorn-snapshot-vsc
  7. source:
  8. persistentVolumeClaimName: test-vol

Result: A Longhorn snapshot is created. The VolumeSnapshot object creation leads to the creation of a VolumeSnapshotContent Kubernetes object. The VolumeSnapshotContent refers to a Longhorn snapshot in its VolumeSnapshotContent.snapshotHandle field with the name snap://volume-name/snapshot-name.

Viewing the Longhorn Snapshot

To see the snapshot, click Volume in the top navigation bar and click the volume mentioned in the VolumeSnapshotContent.snapshotHandle. Scroll down to see the list of all volume snapshots.

How the CSI Mechanism Works in this Scenario

When the VolumeSnapshot object is created with kubectl, the VolumeSnapshot.uuid field is used to identify a Longhorn snapshot and the associated VolumeSnapshotContent object.

This creates a new Longhorn snapshot named snapshot-uuid and the CSI request returns.

Afterwards a VolumeSnapshotContent object named snapcontent-uuid is created with the VolumeSnapshotContent.readyToUse flag is set to true.

Restore PVC from CSI VolumeSnapshot Associated With Longhorn Snapshot

Create a PersistentVolumeClaim object where the dataSource field points to an existing VolumeSnapshot object that is associated with Longhorn snapshot.

The csi-provisioner will pick this up and instruct the Longhorn CSI driver to provision a new volume with the data from the associated Longhorn snapshot.

An example PersistentVolumeClaim is below. The dataSource field needs to point to an existing VolumeSnapshot object.

  1. apiVersion: v1
  2. kind: PersistentVolumeClaim
  3. metadata:
  4. name: test-restore-pvc
  5. spec:
  6. storageClassName: longhorn
  7. dataSource:
  8. name: test-csi-volume-snapshot-longhorn-snapshot
  9. kind: VolumeSnapshot
  10. apiGroup: snapshot.storage.k8s.io
  11. accessModes:
  12. - ReadWriteOnce
  13. resources:
  14. requests:
  15. storage: 5Gi

Note that the spec.resources.requests.storage value must be the same as the size of VolumeSnapshot object.


© 2019-2024 Longhorn Authors | Documentation Distributed under CC-BY-4.0