Kubernetes — Complete Guide to Kubernetes VolumeSnapshot, PVC Backup and Restore, and Automated PVC Backup for Data Management

Ibrahim Halil Koyuncu
4 min readApr 23, 2023

--

VolumeSnapshot General Architecture

Hi, in this article which is related with Kubernetes, I am going to explain below subjects and answer questions.

  • What is Kubernetes VolumeSnapshot ?
  • How to install CRD for Volumesnapshot, VolumeSnapshotContent and VolumeSnapshotClass resources ?
  • How to backup from PVC and restore it ?
  • How to automate PVC Backup according to given scheduling time ?

VolumeSnapshot

Kubernetes VolumeSnapshot is a feature that allows users to take a point-in-time snapshot of a Kubernetes volume. This feature enables data backup, restore, migration, cloning, and disaster recovery operations for Kubernetes workloads.

In simpler terms, a VolumeSnapshot is a way to capture the contents of a Kubernetes Persistent Volume at a specific moment in time, which can be used to restore the data in case of data loss or to migrate the data to a different cluster or storage system.

Install CRDs for Volumesnapshot, VolumeSnapshotContent and VolumeSnapshotClass Resources

VolumeSnapshot is implemented as a Kubernetes Custom Resource Definition (CRD), which means that it is an extension to the Kubernetes API that adds a new resource type. Users can create, read, update, and delete VolumeSnapshots using the Kubernetes API or command-line tools such as kubectl.

To check related resources exist in your cluster, run command below. If you see related resources in the output that means your cluster already has related CRDs.

kubectl api-resources | grep volumesnapshot

If output shows nothing you should install CRDs according to your cluster storage class. In my case I am using LongHorn to manage storage operations so latest version of Longhorn competible with CSI-external-snapshotter v5.0.1.

This is very important to choose right version of CSI-external-snapshotter, so to be sure that do not forget check documantation to use right version.

If you use Longhorn you can follow link in the below to install CRDs and snapshot controllers.

Longhorn version : 1.4.1(latest)

Competible CRDs

Competible Snapshot Controller

After download files just run command below for each resources definition.

kubectl apply -f <resource>.yaml

After applying all resources definitions you can check the resources with commands below.

kubectl api-resources | grep volumesnapshot
kubectl get pods -n kube-system | grep snapshot-controller

The last step related with creating VolumeSnapshotClass resource. To do that apply resource definition in the below.

kind: VolumeSnapshotClass
apiVersion: snapshot.storage.k8s.io/v1
metadata:
name: volume-snapshot-class
annotations:
snapshot.storage.kubernetes.io/is-default-class: "true"
driver: driver.longhorn.io # replace it with your csi drivers
deletionPolicy: Delete
parameters:
type: snap

While implementing this resources, you should be careful while setting the driver. Your driver will different from mine so to check it run below command and replace it with yours.

kubectl get csidrivers

Now you are ready to create a VolumeSnapshot resource.

Backup from PVC and Creating New PVC from VolumeSnapshot

To take a snapshot from existing PVC apply resource definition in the below.

apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: test-csi-volume-snapshot
namespace: your-namespace
spec:
volumeSnapshotClassName: volume-snapshot-class #change it with your volume snapshot class name
source:
persistentVolumeClaimName: target-pvc-name # change it with target pvc name

After applying, check the volumesnapshot resources. It also leads creation of volumesnapshotcontent resource so also check this resource type with command below.

kubectl get volumesnapshot # or vs
kubectl get volumesnapshotcontent # or vsc

When you list volumesnapshot, check also “READYTOUSE” parameter. It must be “true” to use the volumesnapshot.

Now you are ready to use this volumesnapshot resource to restore the data to new PVC resource. To do that apply resource definition in the below.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: <new-pvc-name>
namespace: <namespace-name>
spec:
storageClassName: <storageclass-name>
dataSource:
name: <VolumeSnapshotName>
kind: VolumeSnapshot
apiGroup: snapshot.storage.k8s.io
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi # Be careful, it must be same size of volumesnapshot

Automate PVC Backup

VolumeSnapshots can be taken manually or automatically at scheduled intervals. They are stored as objects in the Kubernetes API and can be used to create new PVs that are exact copies of the original PV at the time the snapshot was taken. In the previous step we have taken manually backup from PVC and restore it. Now we will automate the taking backup step. To do this we will use “Gemini” operator.

Gemini is a Kubernetes CRD and operator for managing VolumeSnapshots. This allows you to create a snapshot of data on your Persistent Volumes on a regular schedule, retire old snapshots, and restore snapshots with minimal downtime.

To quick installation I will use Helm so run commands in the below.

kubectl create ns gemini
helm repo add fairwinds-stable https://charts.fairwinds.com/stable
helm install gemini fairwinds-stable/gemini --namespace gemini

After installation, we should apply resource in the below to schedule volume snapshots.

apiVersion: gemini.fairwinds.com/v1beta1
kind: SnapshotGroup
metadata:
name: test-volume
spec:
persistentVolumeClaim:
claimName: postgres
schedule:
- every: 10 minutes
keep: 3
- every: hour
keep: 1
- every: day
keep: 1
- every: month
keep: 1
- every: year
keep: 1

This resource will create new snapshots every 10 minutes, always keep the last 3 snapshots, and preserve historical hourly, daily, monthly, and yearly snapshots. “keep” parameter specifies how many historical snapshots you want. Schedule objects can be one ore more, it is completely optional according to your use case.

After applying the SnapshotGroup resource you can check the VolumeSnapshot resources they are really created. To do that run command below.

kubectl get vs

Use Cases

It is very convenient way to take a backup from apps such as MinIO.

--

--

Ibrahim Halil Koyuncu
Ibrahim Halil Koyuncu

No responses yet