Kubernetes Guides

How to back up and restore your Kubernetes cluster resources and persistent volumes?

Kubernetes, as we know, coordinates a highly available cluster of computers that are connected to work as a single unit. Kubernetes contains a number of abstractions that allows deployment of containerized applications to the cluster without attaching them to individual machines.

In short, Kubernetes is –

  • Portable: public, private, hybrid, multi-cloud
  • Extensible: modular, pluggable, hook able, composable
  • Self-healing: auto-placement, auto-restart, auto-replication, auto-scaling

In this post, we are going to take look at steps on how to back up and restore your Kubernetes cluster resources and persistent volumes using Velero open-source tool.

Why Backup?

First off, let’s understand the typical scenarios where you would need to use backup

  • In cases of recovery from disaster, it can reduce time for recovery.
  • Migration of Kubernetes resources from one cluster to another​ or to a newer version of Kubernetes.
  • Replication of the environment for debugging, development, etc.,

Now that we are clear on why backup is needed, we can list out objects to back up in the next section.

What to Backup ?

  1. Kubernetes resources are stored in etcd store. etcd is a consistent and highly-available key-value store used as Kubernetes’ backing store for all cluster data. You can find in-depth information about etcd in the official documentation.
  2. Application data i.e., persistent volumes, for stateful applications running on your cluster.

How Velero Works

Velero (formerly Heptio Ark) gives you tools to back up and restore your Kubernetes cluster resources and persistent volumes. Velero consists of:

  • A server that runs on your cluster
  • A command-line client that runs locally

Each Velero operation, for example, on-demand backup, scheduled backup, restore, etc., is a custom resource, defined with a Kubernetes Custom Resource Definition (CRD) and stored in etcd store.

Velero Backup workflow

When you run command velero backup create test-backup:

  1. The Velero client makes a call to the Kubernetes API server to create a Backup object.
  2. The BackupController looks the new Backup object and performs validation.
  3. The BackupController begins the backup operation. It collects the data to back up by querying the API server for resources.
  4. The BackupController makes a call to the object storage service e.g., AWS S3 to upload the backup file.

In the next section,we will take look at steps on how to back up and restore your Kubernetes cluster resources and persistent volumes.

Prerequisites

Following are the prerequisites that are required for this quick start

  1. A Kubernetes cluster with the latest stable release of Kubernetes
  2. Kubectl CLI

Before the installation, let us check if we have got the right Kubernetes version.

Image – Check Kubernetes cluster

Step #1.Download Velero

Download the latest release of Velero with the below command:

curl -LO https://get.helm.sh/helm-v3.0.0-linux-amd64.tar.gz

Image – Download Velero

I’m using Linux, for other platforms, see the releases page. Untar the download file and move the velero executable to /usr/local/bin or your path.

Create a Velero-specific credentials file (credentials-velero) in your local directory:

echo "[default]

aws_access_key_id = minio

aws_secret_access_key = minio123" > credentials-velero

Velero needs appropriate storage providers to store backup and snapshot data, For this demo, we are going to use Minio, an S3-compatible storage service that runs locally on the cluster. The above credentials would be used by Minio instance. Refer list of Supported storage providers.

Now that we have credentials and cluster ready, we can install the Velero server.

Step #2.Install Velero Server

In the below steps, we would be starting the server and the local storage service. In the Velero folder run the below commands:

Configure local storage service using below command:

kubectl apply -f examples/minio/00-minio-deployment.yaml
Image – Configure local storage service

Start Velero server by using below command:

velero install \
    --provider aws \
    --bucket velero \
    --secret-file ./credentials-velero \
    --use-volume-snapshots=false \
    --backup-location-config region=minio,s3ForcePathStyle="true",s3Url=http://minio.velero.svc:9000

 

Image – Start Velero server

For now, we are assuming Velero is running within a local cluster without a volume provider capable of snapshots.

Check if Velero deployments are successfully created.

Image – Verify Velero deployment

Step #3.Deploy Sample Application

Next step is to deploy sample nginx application on the cluster with the following command:

kubectl apply -f velero/examples/nginx-app/base.yaml

Image – Install Sample Application

Check if Sample application deployments are successfully created.

Image – Verify if the deployments are created

Step #4.Backup

Now we will be creating a backup for any object that matches the app=nginx label selector:

velero backup create nginx-backup --selector app=nginx

Image – Velero create backup

If you want to backup all objects except those matching thethen you can use 'backup notin (ignore)'option.

There are also options for creating scheduled backups based on a cron expression.

To verify if the backup has completed, use describe command as below:

velero backup describe nginx-backup

Image – Check if backup has completed

We now have backup operation completed, to test restore operation, we will be deleting the namespace.

Image – Delete Namespace

Verify if Nginx service and deployment are deleted:

Image – Verify if the deployments are deleted

It usually takes few minutes for the namespace to be fully cleaned up.

Step #5.Restore

To list the backups we have created, use the below command:

velero restore get

Image – List Velero backups

To restore the backup we have created, use the below command:

velero restore create --from-backup nginx-backup
Image – Restore Velero Backup

After the restore finishes, you can check if the restored deployments back in namespace:

Image – Verify if the deployments are back

If there are errors or warnings during the restore operation, you can use the below command to check the details:

velero restore describe <RESTORE_NAME>

Congrats! we have successfully made backup and restore them.

If for some limitations, you’re not able to use Velero then you can always use Kubectl CLI to export resource definitions from your existing Kubernetes cluster and then apply them to your target cluster. Following is the command to export objects.

kubectl get deployment -o=yaml --export > deployments.yaml

Limitations

Below are known limitations of Velero

  • Velero currently supports a single set of credentials per provider. It’s not yet possible to use different credentials for different locations.
  • Volume snapshots are limited by where your provider allows you to create snapshots. For example, AWS and Azure do not allow you to create a volume snapshot in a different region than where the volume is.
  • Each Velero backup has one BackupStorageLocation, and one VolumeSnapshotLocation per volume provider. It is not possible to send a single Velero backup to multiple backup storage locations simultaneously, or a single volume snapshot to multiple locations simultaneously.
  • Cross-provider snapshots are not supported.

Like this post? Don’t forget to share it!

Useful Resources

Summary
Article Name
How to back up and restore your Kubernetes cluster resources and persistent volumes?
Description
In this post,we are going to take look at steps on how to back up and restore your Kubernetes cluster resources and persistent volumes using Velero.
Author
Publisher Name
Upnxtblog
Publisher Logo
Karthik

Allo! My name is Karthik,experienced IT professional.Upnxtblog covers key technology trends that impacts technology industry.This includes Cloud computing,Blockchain,Machine learning & AI,Best mobile apps, Best tools/open source libs etc.,I hope you would love it and you can be sure that each post is fantastic and will be worth your time.

Share
Published by
Karthik

Recent Posts

Navigating Volatility: Investing in Crypto Derivatives and Risk Management Strategies

The cryptocurrency market is famed for its volatility, presenting each opportunity and demanding situations for…

2 weeks ago

How Game Developers Use AI in Mobile Games in 2024?

Games since time immemorial have been winning at captivating the users and teleporting them onto…

2 weeks ago

The Impact of AI on Software Development

We are living within an innovation curve wherein cutting-edge technologies are making a hustle and…

3 weeks ago

AI Tools for Research Paper Writing: Learn What They Can Do

Whether it’s the healthcare industry or the automobile sector, artificial intelligence has left its impact…

1 month ago

Embracing Innovation: 5 Ways AI is Transforming the Landscape in 2024

Facts only- The big Artificial Intelligence push is unraveling in 2024. No, it wasn’t merely…

1 month ago

The Startup Guide to Acquiring Exceptional Developers

In the fiercely competitive world of Hire Developers for Startup, success hinges not just on…

2 months ago

This website uses cookies.