Data Protection

K8s backup & restore using Velero

Srinivasa Vasu
Geek Culture
8 min readApr 11, 2021

--

Velero is an open-source backup tool to save and restore the Kubernetes cluster state. The K8s cluster state, including the mounted data, is persisted in an external object store used to restore it either in the same cluster if lost/deleted by accident or in a new cluster elsewhere in a different cloud. It takes volume snapshots using the cloud providers’ native capability and facilitates vendor-neutral backups using restic.

Context: Let’s look at leveraging Velero to move data between K8s clusters running on two different clouds. This would use the restic integration instead of the native cloud provider snapshot capability.

Pre-reqs: Running K8s clusters in two different clouds’/dcs’ and an external pre-configured S3 compatible object store.

Init: Source is a Tanzu Kubernetes Grid(TKG) cluster provisioned on AWS via Tanzu Mission Control (TMC) and target is a native GKE cluster and Minio as the external object store.

Let’s install a Spring reactive r2dbc app with an external state dependency on a Postgres DB. The Postgres storage is mounted as a PVC on an EBS volume. The source and K8s resource manifest to deploy to the TKG cluster are available here.

Source Ops

Create a new namespace kubectl create ns todo, clone the above repo to the local filesystem, and deploy the app to this namespace using a tool like skaffold-cli/waypoint. Using these tools, build and deploy are integrated and done via a single manifest definition.

App-Deploy

Navigate to the target k8s folder in the cloned source repo and run the above command to build and deploy the application to TKG. Ensure to update the image reference in the deployment and skaffold manifests to your registry and repo reference.

Let’s send some POST/PUT requests to the todo app to make changes to the backing Postgres persistent store using httpie or any tool of your choice.

Data Persistence

Jump back to check the datastore state,

Init Velero

Let’s install velero using the CLI,

Check the status of the installation,

Init back-up

Initiate the velero backup for the todonamespace,

Check the backup status,

The S3 minio bucket would have created the appropriate structure with the artifacts for the initiated backup.

minio object-store

Target Ops

Let’s switch the context to the GKE target cluster and install velero the same way as above using the CLI. Check the cluster state,

Init restore

Restore the todonamespace in this cluster,

Check the restore status,

Check the datastore state,

Restore is completed successfully in the target cluster with the same state as the source. All these happen without making any changes to the cluster files/definitions. By default, it uses restic for the persistence as instructed during the installation.

Clean up the namespace kubectl delete ns todo

Incremental backups

Let’s switch the context to the source TKG cluster and initiate a scheduled backup flow. Velero takes incremental backups for the schedule created. Cron schedule is created depending on the RTO/RPO requirements. In this case, cron runs every 5 minutes to backup the specified namespace resource.

Let’s POST more updates,

Jumping over to the minio console, you would notice the incremental backup updates.

scheduled incremental back-ups

Init restore again

Let’s switch the context back to the target cluster to initiate the restore process.

Now restore the todo namespace data from the incremental backups,

Check the restore status,

Recheck the datastore state,

Velero has restored the namespace state fully with all the resources and data intact. This is a common use case where Velero is used to schedule regular backups of K8s clusters for disaster recovery.

--

--

Srinivasa Vasu
Geek Culture

Aspiring Software Artist | views expressed on this blog are solely mine |