Kubernetes: Taming Volume Snapshot

Many times one would have a need to template-ize a volume and would like to clone it for every new pod creation. ‘Volume Snapshot’ is the name for this.

In this article I have put together the steps required to

  • create a master template as PVC
  • create a volume snapshot
  • restore a volume snapshot as PVC

In order to use VolumeSnapshot one needs to enable CSI in their kubernetes. In this attempt of my article I have tested this on GKE. And one requires to run atleast 1.17.x version of kubernetes master plane.

Note (1): I have biased this article for GKE. Hope to make it generic enough so that it can work for other cloud vendors.

Note (2): At the time of writing this article, the GKE documentation on https://cloud.google.com/kubernetes-engine/docs/how-to/persistent-volumes/volume-snapshots did NOT work for me (root caused to being defaulted to )

Note (3): One needs to enable CSI Driver in their GKE using https://cloud.google.com/kubernetes-engine/docs/how-to/persistent-volumes/gce-pd-csi-driver

Step 1: Creating a master template

Here, we create a to adhere to CSI. Note, if the PVC for master-template is not based CSI storage, the VolumeSnapsot creation would fail.

This is applied in kubernetes…

The next step is to load data into this Volume using a throw-away pod.

This is applied using

Once the above pod is ready, one can connect to the pod and copy files using

After which the pod can be destroyed using

Step 2: Create Volume Snapshot

Here, we create a resource based on the PVC created from Step 1.

It can be noted that, the deletion policy of the instance of the VolumeSnapshot’s storage is once the cloned PVC is deleted.

Step 3: Using VolumeSnapshot in another pod

This is the restore step, where a pod uses the VolumneSnapshot using a PVC.

Using PVC in pod / deployment /…

Executed using

The mount in the PVC can be verified to see if the contents are same as what was setup in

Alternatives

CSI Volume Cloning — https://kubernetes.io/docs/concepts/storage/volume-pvc-datasource/ is a potential alternative to VolumeSnapshot.

Also using bucket to copy files into the init step is another option. But, can cause slowness if a large burst of pods are created.

Listener and reader