Kubernetes: Taming Volume Snapshot

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

Step 1: Creating a master template

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

# 01_step.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: pd-storage-standard
provisioner: pd.csi.storage.gke.io
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true
parameters:
type: pd-standard
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc-template
spec:
accessModes:
- ReadWriteOnce
storageClassName: pd-storage-standard
resources:
requests:
storage: 20Gi
kubectl apply -f 01_step.yaml
# copy_files.yaml
apiVersion: v1
kind: Pod
metadata:
name: copy-files
spec:
volumes:
- name: src-volume
persistentVolumeClaim:
claimName: pvc-template-1-test #one can use an existing source here.
- name: template-volume
persistentVolumeClaim:
claimName: pvc-template
containers:
- name: copy-files
image: nginx
volumeMounts:
- mountPath: "/source"
name: src-volume
- mountPath: "/template"
name: template-volume
nodeSelector:
node-type: 1-core
kubectl apply -f copy_files.yaml
kubectl exec -it pod/copy-files -- /bin/bash
kubectl delete -f copy_files.yaml

Step 2: Create Volume Snapshot

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

# create_volume.yaml
apiVersion: snapshot.storage.k8s.io/v1beta1
kind: VolumeSnapshotClass
metadata:
name: pd-snapshot
driver: pd.csi.storage.gke.io
deletionPolicy: Delete
---
apiVersion: snapshot.storage.k8s.io/v1beta1
kind: VolumeSnapshot
metadata:
name: snap-template
spec:
volumeSnapshotClassName: pd-snapshot
source:
persistentVolumeClaimName: pvc-template
kubectl apply -f create_volume.yaml

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 /…
# restore_test.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: restore-pvc-1
namespace: pipeline
spec:
accessModes:
- ReadWriteOnce
storageClassName: pd-storage-standard
resources:
requests:
storage: 20Gi
dataSource:
name: snap-template
kind: VolumeSnapshot
apiGroup: snapshot.storage.k8s.io
---
apiVersion: v1
kind: Pod
metadata:
name: template-instance-test-1
spec:
volumes:
- name: my-storage
persistentVolumeClaim:
claimName: restore-pvc-1
readOnly: false
containers:
- name: task-pv-container
image: nginx
ports:
- containerPort: 80
name: "test-port1"
volumeMounts:
- mountPath: "/template"
name: my-storage
kubectl apply -f restore_test.yaml

Alternatives

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store