Jumat, 20 September 2024

KUBERNETES-velero backup test app

 

Velero Backups in Kubernetes

https://medium.com/cloudnloud/velero-backups-in-kubernetes-39af7e92d992

·

Velero is an open-source tool designed specifically for backing up and restoring data within Kubernetes clusters. Kubernetes is a powerful container orchestration platform that manages the deployment, scaling, and management of containerized applications. However, while Kubernetes provides mechanisms for deploying and managing applications, it doesn’t inherently address the need for data protection, backup, and recovery.

This is where Velero comes into play. Velero is an external tool that extends Kubernetes’ capabilities by providing a way to capture and preserve not only the configuration and metadata of your cluster’s resources (such as deployments, services, config maps, etc.) but also the associated persistent volumes and their data. This is crucial for maintaining data resilience and ensuring business continuity in case of failures, disasters, accidental deletions, or even during cluster migrations.

Reference : https://velero.io/

1. Prerequisites:
— You need a Kubernetes cluster up and running.
— Have the `kubectl` command-line tool installed and configured to interact with your cluster.
— Choose a storage backend for Velero’s backups. This could be an object storage service like Amazon S3, Google Cloud Storage, or a Minio instance.

2. Install Velero CLI:
Download and install the Velero CLI tool. This tool helps you interact with Velero and manage backup and restore operations. You can download the CLI from the Velero GitHub releases page.
https://github.com/vmware-tanzu/velero/releases/tag/v1.11.1

  • Download the file and extract.
  • Copy the velero files to /usr/local/bin.
  • Check out the Velero version.
root@k8s-master:~# velero version
Client:
Version: v1.11.0
Git commit: 0da2baa908c88ec3c45da15001f6a4b0bda64ae2
<error getting server version: namespaces "velero" not found>

3. Install Velero Server Components:
Velero’s server components need to be deployed to your Kubernetes cluster. You can use a YAML manifest to deploy these components.

velero install --use-node-agent   --provider aws   --plugins velero/velero-plugin-for-aws:v1.6.1   --bucket k8s   --secret-file ./minio.credentials   --use-volume-snapshots=false   --backup-location-config region=minio,s3ForcePathStyle="true",s3Url=http://192.168.145.134:9000

Velero status

root@k8s-master:~# velero version
Client:
Version: v1.11.0
Git commit: 0da2baa908c88ec3c45da15001f6a4b0bda64ae2
Server:
Version: v1.11.0

Check the bakup location

root@k8s-master:~# velero backup-location get
NAME PROVIDER BUCKET/PREFIX PHASE LAST VALIDATED ACCESS MODE DEFAULT
default aws k8s Available 2023-08-20 08:57:41 -0700 PDT ReadWrite true
root@k8s-master:~#

4. Perform a Test Backup and Restore:
Before using Velero in a production environment, it’s a good practice to perform a test backup and restore operation to ensure everything is set up correctly. This helps you become familiar with the tool’s functionality.

First, create the sample website (change your values according to your environment).

root@k8s-master:~# cat my-app.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
path: "/k8s/webapp"
server: "192.168.145.133"
readOnly: false
---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-pvc
spec:
accessModes:
- ReadWriteMany
storageClassName: ""
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
type: NodePort
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx # has to match .spec.template.metadata.labels
serviceName: "nginx"
replicas: 1 # by default is 1
template:
metadata:
labels:
app: nginx # has to match .spec.selector.matchLabels
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: k8s.gcr.io/nginx-slim:0.8
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumes:
- name: www
persistentVolumeClaim:
claimName: nfs-pvc
---

Pod status

root@k8s-master:~# kubectl get pod
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 82m

Configuring the backup

Create the annotation

root@k8s-master:~# kubectl annotate pod web-0 backup.velero.io/backup-volumes=www
pod/web-0 annotate

Creating the velero backup

root@k8s-master:~# velero backup create webapp --selector app=nginx --wait
Backup request "webapp" submitted successfully.
Waiting for backup to complete. You may safely press ctrl-c to stop waiting - your backup will continue in the background.
...........
Backup completed with status: Completed. You may check for more information using the commands `velero backup describe webapp` and `velero backup logs webapp`.

Describe the backup

 velero backup describe webapp

After completing the backup, delete the web content and delete the pod and pvc.

root@k8s-master:~# velero restore create --from-backup webapp
Restore request "webapp-20230820090429" submitted successfully.
Run `velero restore describe webapp-20230820090429` or `velero restore logs webapp-20230820090429` for more details.

Schedule a Backup

The schedule operation allows you to create a backup of your data at a specified time, defined by a Cron expression.

velero schedule create NAME --schedule="* * * * *" [flags]
velero schedule create name --schedule="* 18 * * *" --include-namespaces <Namesapce>

How to check the Schedule

velero schedule get

Cron schedules use the following format.

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │ 7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * *

Tidak ada komentar:

Posting Komentar