Backup Azure Kubernetes Services (AKS) cluster with Velero
Tags: cloud, azure, kubernetes, velero
Published: 2021-09-11 15:38:35

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l90qiqfrmg479o5b37xu.png)

In the past, I was in charge of backup a MongoDB instance that runs in Azure Kubernetes Services (AKS). This database has at least 2 TB of data that was being persisted in an Azure Managed Disk with Kubernetes Persistent Volume (PV).

I could not run the “mongodump” without issues. This was overloading the database and take a long time to complete the backup process. The same behavior happened when I tried to restore the database backup.

> Mongodump is a utility tool that allows us to create MongoDB backups. You can check [**this tool here**](https://docs.mongodb.com/database-tools/mongodump/).

Then, instead of run the “mongodump” tool, I applied options below that I found in Microsft Documentation:

*   Take a backup from the Azure Managed Disk with Azure Backup.
*   Create a scheduler backup to MongoDb’s PVC with the Velero tool.

> You can see the documentation about AKS backup in this [**link from Microsoft Documentation**.](https://docs.microsoft.com/en-us/azure/aks/operator-best-practices-storage#secure-and-back-up-your-data)

The first option basically, allows you to take a snapshot from the disk that is being used by Kubernetes PVC. Later, you can restore this snapshot if you need it.

For the second option, you can use a tool called [**_Velero_**](https://velero.io/) to backup the whole AKS cluster, or just some Kubernetes resources, like Deployments, Services, persistent volume claims (PVC), PV, etc.

The backup destination created by Velero could be set up with plugins, which have support for the principal cloud providers, like Azure, AWS, and Google Cloud. In the case of Azure, the backup will be stored in **Azure Account Storage**.

In the end, what I did was create a backup from PV that MongoDB is using to persist its data.

So, in this article, I will present how to install Velero and how to use it to create and restore backups.

#### Setup Velero

The setup of Velero in the environment is very simple. Beyond the Azure account, you will need Azure Account Storage and AKS cluster.

After the resources created in Azure, you can apply the steps below to setup Velero in your environment:

*   Install **_Azure CLI_** and **_kubectl_** tool in the machine that will apply the next steps.
*   Connect to the AKS cluster. You can [**use this link**](https://docs.microsoft.com/en-us/azure/aks/kubernetes-walkthrough#connect-to-the-cluster) to connect to your cluster.
*   Create environment variables to store Azure Account Storage configurations.

{% gist https://gist.github.com/luis-alexandre/dc97456d02525270990f363673e70eb7 %}

*   Create a credential file. This file will be used to configure the Azure Account Storage credentials in Velero.

{% gist https://gist.github.com/luis-alexandre/79e3eaefb43aa4d2e5c74ed17b330dd8 %}

*   Install **Velero client**. You can find the Velero client on its [**home page**](https://velero.io/docs/v1.6/basic-install/)**.**
*   Finally, install Velero in the AKS cluster. After that, we can create backups and restore them.

{% gist https://gist.github.com/luis-alexandre/dd83345618c856ca75777d00bfc7beb6 %}

The Velero installation will create a set of new Kubernetes resources in the AKS cluster. Velero will use the Kubernetes namespace **_“velero”_**. Then, to remove Velero from the AKS cluster, apply the command below:

{% gist https://gist.github.com/luis-alexandre/75759376ba6945532950b832d61193e5 %}

Also, like any Kubernetes resources, we can update it to achieve your requirements. For example, we can update resource limits from its Deployment:

{% gist https://gist.github.com/luis-alexandre/9f02ffccedf6a5b75a62672a4c9f5350 %}

#### Create and Restore Backups

Now that you have Velero in the AKS cluster, you can create and schedule backups, and restore them with the **Velero client**.

Below, there are some commands to help you create and restore backups:

*   Create a new backup:

{% gist https://gist.github.com/luis-alexandre/23d82096485f03234e11b83ca86068de %}

*   Create a new schedule:

{% gist https://gist.github.com/luis-alexandre/158cd2e7a5f92a0e7bd2828e5e92ada2 %}

*   Restore backup:

{% gist https://gist.github.com/luis-alexandre/ab65fc7a47625cbb690baa1b21078e28 %}

> You can find more commands and options in the Velero’s home page. [Click here to check them](https://velero.io/docs/v1.6/).

When the commands above are executed by Velero, new folders are created in its Azure Blob Storage. Here is an example:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o3z59harl5gfwjufgi6v.png)

Beyond backup and restore the cluster, Velero can be useful in the migration process.

We can use that to migrate our AKS cluster to a new AKS cluster or a new Kubernetes cluster from another Cloud Provider.

For now, that’s all. I hope that can be helpful for you!