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

# 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

# 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

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

--

--

--

Listener and reader

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A quick guide to deploying Django on AWS ec2 ubuntu Instance with Uwsgi and Nginx

Introduction to Micronaut Framework | Opencodez

InfluxDB Python integration

Why python is the most widely used language in data science?

Reading MS Access MDB Files on Mac

Introducing the HandyMiner-Goldshell-GUI

Multi-stage Builds, Docker Image Size Reduction

How to be a Web Developer in 2k19 Part 1: Laying the Groundwork

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
Programmer

Programmer

Listener and reader

More from Medium

Spin Up & Scale Selenium Grid Swiftly as A Containerised Application

No! You shall not pass! Kyverno’s here!

Kubernetes Architecture — Processes run on Master Node

SSHFS-based Docker volume mounts