# Backup Exoscale SKS workloads using Velero

In this technical guide, we'll explore the practical steps to fortify your Scalable Kubernetes Service (SKS) workloads on Exoscale using Velero, a robust backup and restore tool.

The guide includes a hands-on demo, illustrating how Velero simplifies the backup and recovery process for stateful applications like Nextcloud.

Whether you're a seasoned DevOps professional or diving into Kubernetes for the first time, this guide is tailored to enhance your understanding of backup strategies, ensuring the resilience of your SKS workloads.

### Step 1: Deploy SKS via Terraform

Follow the guide for [K8s Cluster Installation and Configuration on Exoscale SKS](https://devops.cisel.ch/k8s-cluster-installation-and-configuration-on-exoscale-sks).

### Step 2: Deploy Longhorn for Persistent Storage

Integrating Longhorn into the SKS cluster enhances data management, resilience, and backup capabilities, providing a reliable storage solution for stateful applications running in Kubernetes.

```bash
# Deploy longhorn on your SKS Cluster
kubectl apply -f https://raw.githubusercontent.com/longhorn/longhorn/master/deploy/longhorn.yaml
```

Check that all the pods are in the Running state

```bash
kubectl -n longhorn-system get pods
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699603068235/deb7dd04-48e6-46c8-8eea-ce9d3588a566.png align="center")

```bash
# Use port-forward to connect to the UI
kubectl port-forward deployment/longhorn-ui 7000:8000 -n longhorn-system
```

Access the Longhorn UI at [http://127.0.0.1:7000/#/](http://127.0.0.1:7000/#/).

### Step 3: Deploy Stateful Application (e.g., Nextcloud)

For the demo we're going to use the Nextcloud application, which is simple to deploy and allows us to work easily with persistent data. *And what's more, I love this soft!*

```bash
helm repo add nextcloud https://nextcloud.github.io/helm/
helm repo update
kubectl create ns nextcloud && helm -n nextcloud install nextcloud nextcloud/nextcloud --set service.type=LoadBalancer,persistence.enabled=true,nextcloud.password=whatanamaizingpassword
```

### Step 4: Install Velero CLI

To perform backup and restore operations directly from your console, you'll need to install and use the Velero CLI.

```bash
wget https://github.com/vmware-tanzu/velero/releases/download/v1.12.1/velero-v1.12.1-linux-amd64.tar.gz
tar -xvf velero-v1.12.1-linux-amd64.tar.gz
sudo cp velero-v1.12.1-linux-amd64/velero /usr/local/bin/
rm -rf velero-v1.12.1*
velero version
```

### Step 5: Install Velero using Helm Chart

Now we deploy Velero on the cluster using the official Helm chart.

The latest Velero documentation at this time is [https://velero.io/docs/v1.12/](https://velero.io/docs/v1.12/)

You can find more information about the Helm chart and all the available values on [https://artifacthub.io/packages/helm/vmware-tanzu/velero](https://artifacthub.io/packages/helm/vmware-tanzu/velero)

The settings below will allow us to push the data to the Exoscale s3 service named SOS (Simple Object Storage). The SOS bucket is named veleroexo and hosted in the ch-gva-2 region.

You will need to create an IAM API Keys with a role that allow access to Object Storage service.

Velero has to be initialized with the plugin velero-plugin-for-aws to interact with S3 services and velero-plugin-for-csi for storage operations.

```bash
# Add Velero Helm Chart repository
helm repo add velero https://vmware-tanzu.github.io/helm-charts/

# Create Velero credentials file from Exoscale IAM API Keys at /home/username/.aws/velero_exo_creds
[default]
aws_access_key_id=********************************
aws_secret_access_key=**********************************

# Install Velero with specific configurations
helm install velero velero/velero \
--namespace velero \
--create-namespace \
--set-file credentials.secretContents.cloud=/home/username/.aws/velero_exo_creds \
--set configuration.backupStorageLocation[0].name=exoscale \
--set configuration.backupStorageLocation[0].provider=aws \
--set configuration.backupStorageLocation[0].bucket=veleroexo \
--set configuration.backupStorageLocation[0].config.region=ch-gva-2 \
--set configuration.backupStorageLocation[0].config.publicUrl=https://sos-ch-gva-2.exo.io \
--set configuration.backupStorageLocation[0].config.s3ForcePathStyle=true \
--set configuration.backupStorageLocation[0].config.s3Url=https://sos-ch-gva-2.exo.io \
--set configuration.volumeSnapshotLocation[0].name=exoscale  \
--set configuration.volumeSnapshotLocation[0].provider=aws  \
--set configuration.volumeSnapshotLocation[0].config.region=sos-ch-gva-2.exo.io \
--set image.pullPolicy=IfNotPresent \
--set initContainers[0].name=velero-plugin-for-aws \
--set initContainers[0].image=velero/velero-plugin-for-aws:v1.7.1 \
--set initContainers[0].volumeMounts[0].mountPath=/target \
--set initContainers[0].volumeMounts[0].name=plugins \
--set configuration.features=EnableCSI \
--set initContainers[1].name=velero-plugin-for-csi \
--set initContainers[1].image=velero/velero-plugin-for-csi:v0.6.1 \
--set initContainers[1].volumeMounts[0].mountPath=/target \
--set initContainers[1].volumeMounts[0].name=plugins \
--set deployNodeAgent=true

# Label the longhorn volumesnapshotclass as the csi-volumesnapshot-class
kubectl label volumesnapshotclasses.snapshot.storage.k8s.io longhorn velero.io/csi-volumesnapshot-class=true

# Set exoscale as the default storage location
velero backup-location set exoscale --default
```

Check that everything is up and running

```bash
kubectl -n velero get all
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699602837733/12e61786-0a2e-482b-b4bb-db07335ad056.png align="center")

```bash
velero get backup-locations
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699605076010/979f31b0-e20c-412a-a5d4-d73c68dd73a6.png align="center")

### Step 6: Functional Demo

Let's add some data in Nextcloud before the backup and restore operations. Log to the Nextcloud UI with the admin account and the password provided during the installation.

```bash
#Enable port-forward to access the UI
kubectl port-forward deployment/nextcloud 8099:80 -n nextcloud
```

Access the Nextcloud UI at [http://127.0.0.1:8099/apps/files/?dir=/Documents](http://127.0.0.1:8099/apps/files/?dir=/Documents&fileid=7) and add some files.

Now we create a backup of all the elements of the nextcloud namespace.

```bash
# Create a backup of the Nextcloud namespace
velero backup create nc-backup-10112023-4 --include-namespaces nextcloud
```

The backup operation will be executed in the background. You can describe the backup operation or follow the backup logs. Velero will give you the command to get the information.

If you describe the backup operation you can see the status of the operation, or which namespace and resources are included.

```bash
# Describe backup details
velero backup describe nc-backup-10112023-4 --details
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699605951811/df85d70c-ee28-446b-b91a-8203c4cae700.png align="center")

Get the status of the backup using Velero CLI. You can also see the backup expiration date and the storage location.

```bash
# Get the list of the actual backups
velero get backup
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699602086447/f0cd4063-8702-48bd-a9ef-3e7df1c20c1d.png align="center")

Delete the entire Nextcloud namespace

```bash
# Delete the Nextcloud namespace for simulation
kubectl delete ns nextcloud
```

Validate that the nextcloud PV is deleted to be sure that the restore process is fully fonctionnal.

```bash
#No more nextcloud/nextcloud-nextcloud PV
kubectl get pv
```

Now we start the restore process using the Velero CLI

```bash
# Restore from the backup
velero restore create --from-backup nc-backup-10112023-4
```

The restore process will execute in background. Again Velero will give you the command to get information about the status of the operation.

```bash
# Describe restore details
velero restore describe nc-backup-10112023-4-20231110074848 --details
```

You get also get the status of the restore operation.

```bash
velero restore get
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699608620149/a35c4552-677f-490a-a01f-9924d3379834.png align="center")

Validate that Velero as recreated all the resources in the nextcloud namespace.

```bash
# Check the status of PVCs, PVs, and Pods in the Nextcloud namespace
kubectl -n nextcloud get pvc
kubectl -n nextcloud get pv
kubectl -n nextcloud get pods -w
```

Access the Nextcloud UI at [http://127.0.0.1:8099/apps/files/?dir=/Documents](http://127.0.0.1:8099/apps/files/?dir=/Documents&fileid=7) and validate that your files are present.

```bash
#Enable port-forward to access the UI
kubectl port-forward deployment/nextcloud 8099:80 -n nextcloud
```

All these operations can be carried out using the CRDs provided by Velero.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699608966431/e026eed1-cd2b-4343-84dc-77b7a48554d1.png align="center")

Now armed with these tools and insights, you're ready to navigate the dynamic seas of Kubernetes Backups with confidence. Happy deploying! 🚀🔒
