K8S backup and restore with Velero

In short
  • Persistent volumes make container backups necessary.
  • Velero backs up and restores Kubernetes objects, including an entire namespace with its volumes.
  • Deployment and testing of the solution.
  • Maintain consistency and maintainability in your backup policy.

Introduction

The transition to containerization has somewhat changed some administrative tasks.

Initially designed for handling ephemeral data, backing up a container, outside of its configuration, has not always been a priority.

But today, with the use of volumes mapped within containers, it is necessary to consider backing up the data hosted within them.

Several strategies are possible, starting with application backups. For example, if you are running a database container, nothing prevents you from executing classic dump commands to generate backups of the databases on an external space to the container, such as a network share.

It is also possible, as in the world of VMs, to use specialized backup solutions that will take care of backing up all of your containers, including persistent volumes.

What I recommend is to mix the two approaches as much as possible.

You can benefit from a backup directly related to your application, close to the data. The disadvantage is that it is more complex to manage when you are not a specialist in the application in question.

Then you can have a "more packaged" backup, integrated into a centralized solution that is transverse to your applications. Delegation of operations and maintenance (O&M) is easier to implement. But in this case, you introduce additional dependencies and a more complex implementation cost.

In this article, we will discuss a backup (and restore) based on a specialized solution.

Velero

When we talk about Kubernetes and the need to back up your applications, including persistent volumes (Persistent Volumes (PV)) based on different CSI (Container Storage Interface)), the product Velero often comes up.

Velero logo

Click the image to enlarge.

Velero is open source and designed for backing up, restoring, and migrating Kubernetes resources.

It was originally created by the company Heption, whose founders were members of the original Kubernetes development team (Joe Beda and Craig McLuckie).

In 2018, VMware (now Broadcom…, sniff) acquired the solution. However, Velero is still open source today and remains the leading solution for Kubernetes backups.

Many other platforms that integrate K8S backup functions are actually based on Velero. For example, OpenShift. One of the most popular alternatives to Velero is Kasten, which was acquired by the Veeam, a leader in data protection, in 2020. However, this is a commercial solution (and apparently quite expensive) that has received good reviews.

Before you start, it's important to note that Velero remains modular within the Kubernetes ecosystem.

Its usage and operation depend heavily on the configuration of your K8S cluster.

Velero provides a base, which can then be extended to support specific functions and/or particular CSI (Container Storage Interface) drivers.

Therefore, there are several prerequisites to meet. In my article, I will be based on my cluster, the installation of which I described in my cookbook, which I invite you to review. It is particularly recommended to familiarize yourself with the section dealing with the VMware CSI, and also with the concept of persistent volumes mentioned in this article.

Velero is designed to store its backup images outside of your cluster.

It can use several storage targets, but one of the most common examples is an S3 bucket.

In my case, I use Minio, a solution that provides compatible S3-like storage internally. The combination of Velero with Minio is quite common. Feel free to review my article on setting up and using Minio.

Objectives

As usual, I will start by summarizing the technical objective of this article.

The goal is to deploy Velero in a dedicated namespace.

Then, I will add the necessary dependencies for using the vSphere CSI driver and enabling snapshots.

The objective will be to back up another namespace running an NGINX server attached to a block-type persistent volume based on the vSphere CSI.

The backup image must include all K8S objects in the namespace and the data present on the persistent volume.

It will be hosted on an S3 bucket provided by my Minio instance.

Once backed up, we will destroy the test namespace, then restore it to ensure that the configuration is working correctly.

Principle diagram

Click the image to enlarge.

Principles

Now that we know where we want to go, it is still necessary to understand the operating principle of Velero for this use case. I invite you to familiarize yourself with the following points to better understand what follows.

The backup logic is:

  • Velero and its dependencies run in a namespace; it has access to the entire cluster and access to the S3 bucket via a secret containing authentication information.
  • When the backup is triggered, Velero scans the objects contained in the namespace to be backed up. If the correct options are specified, it includes the persistent volumes (PVs).
  • The configuration of each identified object is saved and exported to the bucket.
  • Depending on the CSI used, Velero will use the addon designed for:
    • Generating a snapshot of the volume: this will result in a snapshot object
    • Mounting this snapshot to read the data and copy it to the bucket using a DataMover solution (here, Kopia): this will result in a datauploads object
    • Once the data is copied, the snapshot is released.

The restore logic is:

  • Velero and its dependencies run in a namespace; it has access to the entire cluster and access to the S3 bucket via a secret containing authentication information.
  • When the restore is triggered, Velero uses the configuration and the list of objects present within the bucket associated with the backup image used to recreate those objects in the cluster.
  • Once the original persistent volumes have been recreated, Velero creates a temporary PV containing the data copied from the snapshot taken during the backup phase.
  • Velero copies the data from this temporary volume to the target persistent volume, which will result in a datadownloads object

Still within this Kubernetes object context, Velero involves manipulating many new objects. Velero itself extends the default K8S API.

To give you an idea, here is a listing of the main new objects related to Velero, the prerequisites and necessary add-ons, involved in backup/restore operations.

Objects API Branch
backuprepositories velero.io/v1
backups velero.io/v1
backupstoragelocations velero.io/v1
datadownloads velero.io/v2alpha1
datauploads velero.io/v2alpha1
deletebackuprequests velero.io/v1
downloadrequests velero.io/v1
podvolumebackups velero.io/v1
podvolumerestores velero.io/v1
restores velero.io/v1
schedules velero.io/v1
serverstatusrequests velero.io/v1
volumesnapshotlocations velero.io/v1
volumesnapshotclasses snapshot.storage.k8s.io/v1
volumesnapshotcontents snapshot.storage.k8s.io/v1
volumesnapshots snapshot.storage.k8s.io/v1
backuprepositories backupdriver.cnsdp.vmware.com/v1alpha1
backuprepositoryclaims backupdriver.cnsdp.vmware.com/v1alpha1
clonefromsnapshots backupdriver.cnsdp.vmware.com/v1alpha1
deletesnapshots backupdriver.cnsdp.vmware.com/v1alpha1
snapshots backupdriver.cnsdp.vmware.com/v1alpha1

We can also illustrate the interaction between the main base objects and the components of a Velero architecture.

Velero architecture

Click the image to enlarge.

Deployment of the solution

Defining the destination bucket with Minio

The first step is to create an S3 bucket dedicated to backups using Minio. I won't go into detail on this part, as you can find a description of how to use Minio in my article dedicated to that.

In my case, I have a simple NFS share dedicated to Minio on my NAS. Minio exposes it through an S3-like interface.

We create a bucket, as well as a dedicated service account associated with an access policy. This policy must be configured correctly to allow Velero to read and write all data from the backup.

In the example, the bucket is called backup-k8s.

Minio bucket

Click the image to enlarge.

We use an account named svc-k8s-backup.

Minio service account

Click the image to enlarge.

The account is associated with a policy named pol-backup-k8s

Minio policy

Click the image to enlarge.

Here are the details of the policy:

  {
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetBucketLocation",
                "s3:ListBucket",
                "s3:ListBucketMultipartUploads"
            ],
            "Resource": [
                "arn:aws:s3:::backup-k8s"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:DeleteObject",
                "s3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3:::backup-k8s/*"
            ]
        }
    ]
}

The important element for the next step is to retrieve the authentication information associated with this account. To do this, we create an access key consisting of an ID and a password that we need to save and enter in a file named credential-velero.

Access key

Click the image to enlarge.

This file must have the following format:

  
[default]
aws_access_key_id = id_issu_de_minio
aws_secret_access_key = secret_associe_a_id
File containing the connection information for the bucket

Click the image to enlarge.

Of course, it is a critical file, so access must be protected. It will only be used for installing Velero and can then be deleted (but I recommend keeping it in a secure location in case you need it later).

Enabling snapshot consideration on the cluster

Another prerequisite is to enable snapshot support through our CSI driver. In my case, it's the vSphere driver. It's important to know that a CSI provides or supports a set of features specific to its vendor. Not all features are necessarily enabled during deployment.

In my cookbook, in the section about deploying the vSphere CSI, I use the default configuration, and this does not support volume snapshots.

So, we'll start by adding the missing configuration. This part may vary from one CSI to another; you need to verify that:

  • Velero supports your CSI
  • Your CSI is configured with the necessary prerequisites for it to be used by Velero

For the vSphere CSI, you'll need to retrieve a sh script provided by VMware that will handle installing the missing dependencies for using snapshots.

This script evolves as the CSI versions change, so you need to include it in your update procedures.

As of this article, version 3.3.1 of the CSI driver is available; the script can be retrieved from this URL.

You need to download this script onto the first control plane of your cluster, wget https://raw.githubusercontent.com/kubernetes-sigs/vsphere-csi-driver/refs/heads/master/manifests/vanilla/deploy-csi-snapshot-components.sh

Script retrieval

Click the image to enlarge.

Then, as root, make it executable and finally run it using your standard account (the same one you use for your kubectl operations). Of course, your vSphere CSI must be deployed beforehand.

Deployment of snapshot dependencies

Click the image to enlarge.

A series of actions will be performed by the script, and if everything goes well, it should exit without errors.

If you are using a different CSI, check its snapshot support and how to use them in a K8S cluster.

Now that the missing components are deployed, we need to proceed with some additional configuration steps.

A snapshot, regardless of the CSI, is an object in k8S. It is associated with another object called a VolumeSnapshotClass, similar to how a persistent volume (pv) can be attached to a storage class.

Therefore, you need to create this VolumeSnapshotClass object that will use the vSphere CSI and the new elements we have just installed.

The YAML file volumesnapshotclass-csi-vsphere.yaml associated with this object is as follows:

  
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotClass
metadata:
  name: volumesnapshotclass-csi-vsphere
driver: csi.vsphere.vmware.com
deletionPolicy: Delete

Then, deploy it using the command kubectl apply -f volumesnapshotclass-csi-vsphere.yaml.

This object is shared across namespaces (like StorageClass) and we can now verify that it is present on the cluster using the command kubectl get volumesnapshotclass.

Snapshot provider control

Click the image to enlarge.

My cluster has a StorageClass (sc) associated with the vSphere CSI. It is presented in my cookbook in this article.

I need to update its description to add support for snapshots using Velero. This is done simply by editing the YAML file that I used to deploy it and adding the label velero.io/csi-volumesnapshot-class: "true".

Modifying the storage class

Click the image to enlarge.

I reapply my YAML to update the sc with the command kubectl apply -f 01-sc-datastore.yaml.

Preparing the namespace

We are almost at the end of the prerequisites. We just need to create the namespace dedicated to Velero.

In my example, and to adhere to my naming convention, the namespace will be called inf-velero-lan.

We create it with the command kubectl create ns inf-velero-lan.

In this namespace, I will need to host a secret containing the access credentials for my vCenter, because the Velero addon dedicated to vSphere will need to generate volume snapshots and therefore have access to the vCenter.

This secret will be based on the same configuration file csi-vsphere.conf used during the deployment of the vSphere CSI, as presented in this section of my K8S cookbook.

The csi-vsphere.conf file has the following content:

  
  [Global]
  cluster-id = "id_du_cluster_k8s"
  
  
  [VirtualCenter "fqdn.votre.vcenter"]
  insecure-flag = "true"
  user = "compte_de_service_ayant_acces_au_vcenter"
  password = "password_di_compte"
  port = "443"
  datacenters = "datacenter_vmware"

(more details are available in the section dedicated to the vSphere CSI).

The values depend on the vSphere configuration.

We create our secret in the namespace dedicated to Velero with the command kubectl -n inf-velero-lan create secret generic velero-vsphere-config-secret --from-file=csi-vsphere.conf

There is one more object, a configMap, to create. A configMap allows you to store the configuration of a component or application and then expose that configuration within a pod. It's similar to a secret, but without being encoded in base64, so it contains content considered less critical in terms of confidentiality.

This configMap will be used by the pods that will be deployed during the installation of the vSphere addon for Velero.

The content of the associated YAML file velero-vsphere-plugin-config.yaml is as follows:

  
apiVersion: v1
kind: ConfigMap
metadata:
  name: velero-vsphere-plugin-config
  namespace: inf-velero-lan
data:
  cluster_flavor: VANILLA
  vsphere_secret_name: velero-vsphere-config-secret
  vsphere_secret_namespace: inf-velero-lan

It refers to the secret velero-vsphere-config-secret that we created previously.

We deploy it with the command kubectl apply -f velero-vsphere-plugin-config.yaml

Preparing the namespace for Velero and its add-ons

Click the image to enlarge.

We now have all the prerequisites necessary for deploying Velero.

Installation of Velero

There are different ways to install Velero. In this example, I suggest using the binary directly. It works similarly to kubectl. You can control Velero entirely from a simple executable on your machine. This executable uses the same configuration file as kubectl, which is located in your user profile. This means you need the necessary permissions on the cluster, specifically a high level of access close to that of a cluster administrator, because you will be deploying many components.

Simply download the binary from the official repository for your client machine. In my case, it's an executable file for Windows.

I recommend modifying your PATH so you can call it from anywhere.

To start the Velero installation, use the following command:

velero install --provider aws --plugins velero/velero-plugin-for-aws:v1.10.1 --bucket backup-k8s --secret-file ./credentials-velero --backup-location-config region=minio,s3ForcePathStyle="true",s3Url=http://svc-minio-default.prd-minio-lan.svc.cluster.local:9000,publicUrl=https://minioapi.coolcorp.priv --features=EnableCSI --snapshot-location-config region=minio --namespace inf-velero-lan --use-node-agent --cacert .\CA-COOLCORP.crt

It's quite long. Here's a detailed explanation of the parameters passed to the Velero binary.

  • --provider aws: This indicates that we will be using an AWS-based backup repository (S3).
  • --plugins velero/velero-plugin-for-aws:v1.10.1: This specifies the version of the plugin to use for the backup repository. Feel free to check the version using this URL.
  • --bucket backup-k8s: This is the name of the target S3 bucket, in my case, the one I created in Minio.
  • --secret-file ./credentials-velero: We use the file created earlier, which contains the credentials for accessing the bucket, to provide Velero with the necessary identifiers for reading and writing to the bucket.
  • --backup-location-config region=minio,s3ForcePathStyle="true",s3Url=http://svc-minio-default.prd-minio-lan.svc.cluster.local:9000,publicUrl=https://minioapi.coolcorp.priv: These are the different URLs associated with the bucket. It's important to note that we use two URLs: one internal to the K8S cluster (svc-minio-default.prd-minio-lan.svc.cluster.local:9000), which is linked to the service deployed during Minio installation, and a public URL (https://minioapi.coolcorp.priv) that is visible outside the cluster. Feel free to refer to my article on Minio for a better understanding of these two URLs.
  • --features=EnableCSI: This enables support for the CSI standard
  • --snapshot-location-config region=minio: This indicates the AWS region where the snapshot is stored. In this case, since we are "simulating" AWS with Minio, we simply enter "minio".
  • --namespace inf-velero-lan: This specifies the namespace where Velero is installed (by default, Velero installs in the 'velero' namespace).
  • --use-node-agent: This tells Velero that it can use agents deployed on the nodes, which is related to moving data from persistent volumes.
  • --cacert .\CA-COOLCORP.crt: This specifies the certificate of the CA associated with the public URL of Minio. In my case, I am using my own Windows PKI. Therefore, I am providing the public certificate from my PKI, which was used to generate the certificate specific to my Minio URL, managed by Traefik, as a parameter.
Installation of Velero

Click the image to enlarge.

Following the installation of Velero, we add the vSphere plugin.

To do this, we use the following command:

velero plugin add vsphereveleroplugin/velero-plugin-for-vsphere:v1.5.4 -n inf-velero-lan

You can acknowledge the warning message.

Installing the vSphere plugin for Velero

Click the image to enlarge.

As with the AWS plugin, check the version of the vSphere plugin via this URL.

From this point, you will need to wait several minutes for all components to initialize.

You can verify this with the following command:

kubectl get pod -n inf-velero-lan

Checking the pods

Click the image to enlarge.

You should eventually see output that is identical to the screenshot. It shows the pod specific to Velero, as well as the pods related to the vSphere plugin, which rely on the ConfigMap and Secret that we set up in the prerequisites.

Testing the solution

Backup test

To test the backup, I will use the sun namespace demo dev-demopvdatastore-lan that I had previously used to validate the installation of the vSphere CSI.

I will not go into detail about the different YAML files that make up the demonstration. It simply involves running an NGINX server within a pod, where a persistent volume is mapped to the path /usr/share/nginx/html of the container.

The pod is running, and you can verify this with the command: kubectl get pod -n dev-demopvdatastore-lan

Checking the témoinlab pod

Click the image to enlarge.

I am now in the context of the pod's container using the command: kubectl exec -it nom_du_pod -n dev-demopvdatastore-lan bash

By typing df -h, you can see that a 5GB disk is mapped to /usr/share/nginx/html. This disk corresponds to my persistent volume.

Checking the mount point

Click the image to enlarge.

I will write some data to it using the command touch, which creates a file named toto within the volume.

Setting up the témoin file

Click the image to enlarge.

The volume is indeed a persistent volume based on the vSphere CSI, and you can get information about it using the command: kubectl get pv

Checking the PV (Persistent Volume)

Click the image to enlarge.

You can start the backup of the entire demo namespace using the following command:

velero backup create backup-test-01 --include-namespaces dev-demopvdatastore-lan --namespace inf-velero-lan --snapshot-volumes --snapshot-move-data

Once again, the command is a bit long, but easy to understand.

  • velero backup create backup-test-01: This tells Velero to create a backup image named backup-test-01
  • --include-namespaces dev-demopvdatastore-lan: This indicates to Velero that the objects to be backed up are in the dev-demopvdatastore-lan namespace
  • --namespace inf-velero-lan: The namespace where Velero is located
  • --snapshot-volumes: This requests that volumes be snapshotted
  • --snapshot-move-data: This requests that the data from the snapshots be moved. This option is important because, without it, Velero will simply create a snapshot of the volumes, but these volumes will remain in their original location. The data contained within them will not be backed up outside of vSphere. Restoration will only work if the snapshot is still available, meaning that the initial infrastructure must still be operational. If we were facing a total disaster recovery scenario, we would not be able to restore the data. This option allows moving the contents to the S3 bucket. It is at this level that we use Kopia. I cannot provide more details about this usage, but essentially, Kopia is used by Velero to back up the snapshot content to the S3 bucket in combination with the vSphere addon
Starting the backup

Click the image to enlarge.

We can regularly monitor the progress of the backup using the command velero backup describe backup-test-01 -n inf-velero-lan --details.

Monitoring the backup

Click the image to enlarge.

From the vCenter perspective, we observe the snapshot operations.

Snapshot activity

Click the image to enlarge.

You can list the snapshots using the following command: kubectl get snapshot -n dev-demopvdatastore-lan (the snapshot is linked to the namespace of the PVC being backed up).

Listing snapshots

Click the image to enlarge.

The snapshot is attached to a temporary PV.

If we look at Minio, we will see objects appearing in the bucket.

File on the bucket

Click the image to enlarge.

We must be careful about the status of the backup. Sometimes Velero indicates that the operation is complete, but in reality, the data is still being moved, especially on the vSphere addon side, which continues to copy it.

To be certain that the operation has finished, we need to check the logs of the pods dedicated to moving the data from the vSphere plugin. The temporary PV associated with the snapshot should also disappear.

At the end of the operation, you can list the available backup images using the command velero get backup -n inf-velero-lan.

You should find our image there with the default retention period of 30 days (this parameter can, of course, be customized when starting the backup).

Checking the image

Click the image to enlarge.

Once everything is complete, we can delete the demo namespace and everything associated with it, including the PV.

I am executing the deletion commands.

kubectl delete -f D:\Doc\GIT\gitlab.com\kub.coolcorp.priv\namespaces\dev-demopvdatastore-lan
kubectl delete ns dev-demopvdatastore-lan
kubectl delete pv nom_du_pv

Finally, the dev-demopvdatastore-lan namespace, its objects, and the PV no longer exist in the cluster.

Deleting the témoin objects

Click the image to enlarge.

Restore test

We can now evaluate the restoration process.

If we list the backups using the command velero get backup -n inf-velero-lan, we can find our backup image backup-test-01.

We will keep this in mind to run the restore command:

velero restore create --from-backup backup-test-01 --namespace inf-velero-lan

Starting the restoration

Click the image to enlarge.

In this case, the instructions are easy to understand

We can follow the restoration process using the command velero restore describe backup-test-01-id-restauration(donné en sortie de lancement de restauration) -n inf-velero-lan --details

Basic diagram of the lab

Click the image to enlarge.

If we list the namespaces, we will quickly see our demo namespace again, which indicates that the restoration is in progress.

Reappearance of the namespace

Click the image to enlarge.

After a certain amount of time, the operation completes. In my case, I am not able to get a successful result from the information command. I probably have something to refine, but if I check the status of my NGINX pod, it is indeed back up.

Listing the pod

Click the image to enlarge.

We can enter its context and check the contents of the folder /usr/share/nginx/html.

The file 'toto' is present and shows that the data has been restored.

Checking the restored data

Click the image to enlarge.

Therefore, the operation was able to complete successfully, and I have my application fully restored with its persistent data.

Since I did not specify any options during the backup, the image will remain for 30 days before being purged. This retention period can be configured in Velero.

I invite you to read the product documentation to get more information about creating a backup policy, including job scheduling. You can define a backup that protects multiple namespaces, or create a policy per namespace, per specific object... it's up to each user to organize their backup plan according to their constraints.

Conclusion

At the time of writing this article, Velero is the preferred tool for backing up Kubernetes environments. Fully integrated into the modular and extensible logic of K8S, it adopts its object-based operation and... also its complexity.

The most complicated thing about Velero is not its usage, but ensuring that you have all the prerequisites with the associated dependencies configured and functioning to take into account all the specific features of the source cluster.

That's why it's important to understand how it works and the interactions between components.

Be very careful when interpreting backup success messages. In general, a backup has no value if you are not certain that you can restore it. This implies regularly testing your backups, and this advice is not specific to Kubernetes.

Feel free to combine your protection strategies. Having a Velero backup image doesn't prevent you from having an application dump as well.

Also, protect your backup images. Always prioritize an independent and isolated backup infrastructure separate from the infrastructure you are protecting.

In the example, Velero is installed on the cluster that contains the resources to be backed up. This isn't a problem if the images are written to a third-party component and secured, such as a NAS in my case.

It will always be possible to have a new instance of Velero re-examine the contents of the backup images.

A weakness of my mini architecture is that my NAS is on the same local network as my workstation, which means that an attack on my PC, which is naturally more exposed, could affect my backup images (the infamous cryptolocker...).

In the context of my mini lab, designed primarily for educational purposes, this isn't really a problem, but in a real production environment, it should be prohibited to have a backup network identical to the primary network.

Regarding Velero, feel free to implement it, but you need to accept the operational overhead that comes with it and the necessary version tracking (including all third-party dependencies). It's a powerful and effective tool, but it's not as user-friendly or easy to install as something like Veeam Backup.

I haven't really found a GUI that could help yet (if you have any suggestions, I'm open to them). SaaS integration solutions like CloudCasa are available under the condition of a compatible cluster (and an additional cost).

In general, never underestimate backup needs... and restoration needs... but remain consistent in your policy by offering something that is technically maintainable. Don't promise backups that you, or your future colleagues, will never be able to restore

Deploy Velero and remember to back up your etcd database.

You will already have a good structure for protecting applications hosted on your K8S cluster.