Implement Snapshot Feature

To implement the snapshot feature, a CSI driver needs to support controller capabilities CREATE_DELETE_SNAPSHOT and LIST_SNAPSHOTS, and implement controller RPCs CreateSnapshot, DeleteSnapshot, and ListSnapshots. For details, see the CSI spec here.

Here are some example CSI plugins that have implemented the snapshot feature:

You can find more sample and production CSI drivers here. Please note that drivers may or may not have implemented the snapshot feature.

Snapshot APIs

The volume snapshot APIs are implemented as CRDs here. Once you deploy the CSI sidecar containers which includes the external snapshotter in your cluster, the external-snapshotter will pre-install the Snapshot CRDs.

Enable VolumeSnapshotDataSource Feature Gate

Since volume snapshot is an alpha feature in Kubernetes v1.12, you need to enable a new alpha feature gate called VolumeSnapshotDataSource in API server binary.

--feature-gates=VolumeSnapshotDataSource=true

Deploy External-Snapshotter with CSI Driver

The snapshot controller is implemented as a sidecar helper container called External-Snapshotter. External-Snapshotter watches VolumeSnapshot and VolumeSnapshotContent API objects and triggers CreateSnapshot and DeleteSnapshot operations.

It is recommended that sidecar containers External-Snapshotter and External-Provisioner be deployed together with CSI driver in a StatefulSet. See this example yaml file which deploys External-Snapshotter and External-Provisioner with the Hostpath CSI driver. Run the following command to start the sidecar containers and the CSI driver:

kubectl create -f setup-csi-snapshotter.yaml

Test Snapshot Feature

Use the following example yaml files to test the snapshot feature.

Create a StorageClass:

kubectl create -f storageclass.yaml

Create a PVC:

kubectl create -f pvc.yaml

Create a VolumeSnapshotClass:

kubectl create -f snapshotclass.yaml

Create a VolumeSnapshot:

kubectl create -f snapshot.yaml

Create a PVC from a VolumeSnapshot:

kuberctl create -f restore.yaml

PVC not Bound

If a PVC is not bound, the attempt to create a volume snapshot from that PVC will fail. No retries will be attempted. An event will be logged to indicate that the PVC is not bound.

Note that this could happen if the PVC spec and the VolumeSnapshot spec are in the same yaml file. In this case, when the VolumeSnapshot object is created, the PVC object is created but volume creation is not complete and therefore PVC is not bound yet. You need to wait until the PVC is bound and try to create the snapshot again.