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.

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

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.

Result: A Longhorn snapshot is created. The 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.

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.

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

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.

  • Longhorn volume must be in the attached state to create/restore snapshot using CSI VolumeSnapshot mechanism.