5 min read

Backup Restore Kubernetes with Velero & Swift

Backup Restore Kubernetes with Velero & Swift

Hello World! Pada Artikel kali ini saya mau sharing ringan terkait riset yang sudah saya coba terkait Backup Restore Kubernetes dengan Velero dan Swift.

Pendahuluan

Velero merupakan tool untuk backup restore, disaster recovery, dan juga migrasi Kubernetes cluster resource dan persistent volume. Velero dapat dijalankan diatas cloud provider ataupun on-premises.

Velero ini bagus sebagai backup resource di Kubernetes, bisa sebagai backup harian dan menghindari kerugian yang dialami misal terjadi human error.

Sedangkan OpenStack Swift adalah project open-source Openstack yang menyediakan layanan Object Store. Swift ideal untuk menyimpan data yang bersifat tidak terstruktur, seperti file gambar, video, dan data arsip.

Sebelumnya saya juga sempat membuat Artikel terkait Openstack Swift, jika tertarik bisa melihat pada Artikel berikut:

Using RadosGW as a drop-in replacement for Swift
Hello Folks! Artikel ini lebih seperti catatan untuk diri sendiri, tetapi ini mungkin membantu jika anda mencari cara untuk mengintegrasikan Openstack Swift dengan Ceph Object Storage yaitu Ceph Rados Gateway. Pendahuluan OpenStack Swift adalah project open-source Openstack yang menyediakan layan…

Tujuan dari Artikel ini adalah provide step-by-step terkait backup-restore aplikasi Wordpress yang running diatas RKE2 aio, menggunakan velero untuk Backup dan Restore serta Swift sebagai S3 seperti Object Storage

Diagram

Prasyarat

Saya tidak akan menjelaskan bagaimana cara deploy Cluster Openstack serta RKE2. Diasumsikan bahwa sudah ada Cluster Openstack dengan service Swift running serta Cluster RKE2 running.

Requirements:

  • Cluster Kubernetes / RKE2 / lainnya
  • Cluster Openstack  dengan service Swift enabled

Artikel ini akan berfokus kepada Backup Restore resource Kubernetes dengan Swift berperan sebagai BackupLocation. Selain itu saya juga akan memanfaatkan tool restic untuk backup data yang tersimpan pada PV/Persistent Volume.

Agar Swift dapat dijadikan sebagai BackupLocation oleh Velero diperlukan plugin tambahan agar velero dapat berinteraksi dengan Openstack. Untuk detail plugin yang digunakan bisa merefer pada github berikut:

GitHub - Lirt/velero-plugin-for-openstack: Openstack Cinder, Manila and Swift plugin for Velero backups
Openstack Cinder, Manila and Swift plugin for Velero backups - GitHub - Lirt/velero-plugin-for-openstack: Openstack Cinder, Manila and Swift plugin for Velero backups

Inisiasi Awal

Sebelum Install Velero, perlu dipersiapkan terlebih dulu Container (Bucket) pada Swift yang akan digunakan oleh Velero nantinya.

openstack container create velero

Setelah membuat container pada swift, diharuskan untuk membuat Temporary URL Key agar dapat download velero backups.

SWIFT_TMP_URL_KEY=$(dd if=/dev/urandom | tr -dc A-Za-z0-9 | head -c 40)
swift post -m "Temp-URL-Key:${SWIFT_TMP_URL_KEY}" velero

Autentikasi Velero dengan Openstack

Terdapat beberapa method yang bisa digunakan agar Plugin Velero dapat berinteraksi dengan Openstack, selengkapnya: OpenStack Authentication Configuration.

Method yang saya pakai ialah menggunakan Openstack Application Credential agar Velero dapat berkomunikasi dengan keystone.

openstack application credential create rj-rke2

Catat output yang dihasilkan.

Instalasi Velero

Instalasi Velero dilakukan diatas Cluster RKE2.

Download Velero Client.

wget https://github.com/vmware-tanzu/velero/releases/download/v1.11.1/velero-v1.11.1-linux-amd64.tar.gz
tar -xvf velero-v1.11.1-linux-amd64.tar.gz
cp velero-v1.11.1-linux-amd64/velero /usr/local/bin/

Create namespace untuk velero.

kubectl create ns velero

Buat Secret berisi variable yang dibutuhkan oleh Plugin Velero. Masukkan Value dari Application Credential yang sudah dibuat sebelumnya.

export OS_AUTH_TYPE=v3applicationcredential
export OS_AUTH_URL=https://public.demo.lab:5000/v3
export OS_IDENTITY_API_VERSION=3
export OS_REGION_NAME="RegionOne"
export OS_INTERFACE=public
export OS_APPLICATION_CREDENTIAL_ID=<application_credential_id>
export OS_APPLICATION_CREDENTIAL_SECRET=<application_credential_password>
export OS_VERIFY="false"
export TLS_SKIP_VERIFY="true"

kubectl -n velero create secret generic openstack-cloud-credentials \
--from-literal OS_AUTH_TYPE=$OS_AUTH_TYPE \
--from-literal OS_AUTH_URL=$OS_AUTH_URL \
--from-literal OS_IDENTITY_API_VERSION=$OS_IDENTITY_API_VERSION \
--from-literal OS_REGION_NAME=$OS_REGION_NAME \
--from-literal OS_INTERFACE=$OS_INTERFACE \
--from-literal OS_APPLICATION_CREDENTIAL_ID=$OS_APPLICATION_CREDENTIAL_ID \
--from-literal OS_APPLICATION_CREDENTIAL_SECRET=$OS_APPLICATION_CREDENTIAL_SECRET \
--from-literal OS_VERIFY=$OS_VERIFY \
--from-literal TLS_SKIP_VERIFY=$TLS_SKIP_VERIFY \
-o yaml

Tambahkan Repo Helm Velero.

helm repo add vmware-tanzu https://vmware-tanzu.github.io/helm-charts
helm repo update

Buat values.yaml, bisa disesuaikan saja sesuai environment masing-masing.

cat<<EOF > values.yml
credentials:
  extraSecretRef: "openstack-cloud-credentials"
dnsPolicy: ClusterFirstWithHostNet
nodeAgent:
  dnsPolicy: ClusterFirstWithHostNet
configuration:
  backupStorageLocation:
  - name: swift
    provider: community.openstack.org/openstack
    bucket: velero
    accessMode: ReadWrite
    default: true
    config:
      resticRepoPrefix: swift:velero:/restic/
      insecureSkipTLSVerify: "true"
  defaultVolumesToFsBackup: true
initContainers:
- name: velero-plugin-openstack
  image: lirt/velero-plugin-for-openstack:v0.6.0
  imagePullPolicy: IfNotPresent
  volumeMounts:
    - mountPath: /target
      name: plugins
snapshotsEnabled: false
backupsEnabled: true
deployNodeAgent: true
EOF

Install Velero.

helm install velero vmware-tanzu/velero --namespace velero --values values.yml

(Optional) Jika domain pada Cluster Openstack tidak bisa direach dari Cluster Kubernetes, bisa mencoba menambah parameter hostNetwork pada deployment velero.

KUBE_EDITOR="nano" kubectl edit deployment velero -n velero
---
      hostNetwork: true

Verifikasi Instalasi Velero.

kubectl get secret,pod -n velero
velero backup-location get

Instalasi Wordpress

Download Wordpress Manifest

curl -LO https://k8s.io/examples/application/wordpress/mysql-deployment.yaml
curl -LO https://k8s.io/examples/application/wordpress/wordpress-deployment.yaml

Buat Ingress Resource

cat<<EOF > ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: wordpress-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
  - host: wordpress.rjhaikal.my.id
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: wordpress
            port:
              number: 80

Buat Kustomization File

cat<<EOF > kustomization.yaml
secretGenerator:
- name: mysql-pass
  literals:
  - password=MySecurePassword123!
resources:
  - mysql-deployment.yaml
  - wordpress-deployment.yaml
  - ingress.yaml
namespace: swift-wordpress
EOF

Deploy Wordpress

kubectl apply -k ./

Akses Ingress, Buat Admin Password Wordpress dan Login.

Selanjutnya buat Data atau Posting sesuatu di Wordpress sebagai verifikasi Backup Restore dengan velero.

Backup Wordpress dengan Velero

Buat backup dari wordpress yang sudah dideploy sebelumnya.

velero backup create wp --include-namespaces swift-wordpress

Delete Wordpress

Hapus Wordpress deployment beserta PV untuk melihat apakah data kembali ketika di restore oleh Velero nantinya.

kubectl delete -k ./

Restore Wordpress dengan Velero

Restore Wordpress dengan Velero, verifikasi apakah data wordpress kembali ketika di restore

velero restore create wp-restore --from-backup wp

Verifikasi Data

Wordpress Data berhasil di restore

Tips

Sering-sering cek logs dari pod velero / node-agent jika terjadi issue saat backup / restore.

Referensi

Velero Docs - Overview
GitHub - Lirt/velero-plugin-for-openstack: Openstack Cinder, Manila and Swift plugin for Velero backups
Openstack Cinder, Manila and Swift plugin for Velero backups - GitHub - Lirt/velero-plugin-for-openstack: Openstack Cinder, Manila and Swift plugin for Velero backups