Backup & Restore Kubernetes Persistent Data Using Velero
Bavithran M
Senior Cloud & DevOps Engineer | AWS & Azure Certified | Kubernetes & Automation Advocate | Training | Mentoring | Uplifting Many IT Professionals
One of the biggest challenges in managing Kubernetes workloads is ensuring data persistence and disaster recovery. Imagine running a critical database or stateful application in Kubernetes, and suddenly an update goes wrong, a pod is deleted, or a cluster migration is needed.
How do you back up and restore your persistent data in such scenarios?
Enter Velero—an open-source tool that helps you back up and restore Kubernetes clusters, including Persistent Volumes (PVs), Kubernetes objects, and applications across clusters.
This guide will take you through:
? What is Velero, and why should you use it?
? How to back up Kubernetes workloads and persistent volumes
? How to restore data in case of failure or cluster migration
? Hands-on scenarios for real-world disaster recovery
?? Why Use Velero for Kubernetes Backup & Restore?
Velero is not just a snapshot tool; it provides:
? Full cluster backups including persistent data and configuration.
? Disaster recovery across different Kubernetes clusters.
? Scheduled backups to ensure automatic recovery points.
? Support for multiple cloud providers (AWS, Azure, GCP) and on-prem environments.
Velero is an essential tool when running stateful applications like databases (MySQL, PostgreSQL, MongoDB), message queues (Kafka, RabbitMQ), and persistent applications (Elasticsearch, Prometheus, etc.).
?? Scenario 1: Backing Up & Restoring a MySQL Database in Kubernetes Using Velero
?? Context
You are running a MySQL database inside Kubernetes using a Persistent Volume (PV), and you want to:
? Schedule backups of the database and Persistent Volume (PV).
? Restore data in case of accidental deletion.
Step 1: Install Velero on Your Kubernetes Cluster
First, we need to install Velero using the official Helm chart.
For AWS (Using S3 for Backups)
helm repo add vmware-tanzu https://vmware-tanzu.github.io/helm-charts
helm install velero vmware-tanzu/velero --namespace velero --create-namespace \
--set configuration.provider=aws \
--set configuration.backupStorageLocation.name=default \
--set configuration.backupStorageLocation.bucket=<YOUR-S3-BUCKET> \
--set credentials.useSecret=true \
--set-file credentials.secretContents.cloud=./credentials-velero
Replace <YOUR-S3-BUCKET> with your actual S3 bucket name.
Verify Velero Installation
kubectl get pods -n velero
Expected Output:
NAME READY STATUS RESTARTS AGE
velero-xyz-1234 1/1 Running 0 10s
? Velero is now installed and ready to back up Kubernetes data!
Step 2: Deploy a MySQL Database with Persistent Storage
Now, let's create a MySQL deployment with a Persistent Volume.
Create a Persistent Volume Claim for MySQL
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
Deploy MySQL Using StatefulSet
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
serviceName: mysql
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: "password"
volumeMounts:
- mountPath: "/var/lib/mysql"
name: mysql-storage
volumes:
- name: mysql-storage
persistentVolumeClaim:
claimName: mysql-pvc
Apply the MySQL deployment:
kubectl apply -f mysql-pvc.yaml
kubectl apply -f mysql-statefulset.yaml
? MySQL is now running with a Persistent Volume for data storage.
Step 3: Take a Backup of MySQL Using Velero
Run the following command to create a backup of the MySQL deployment, including its PV.
velero backup create mysql-backup --include-namespaces=default --wait
Check the backup status:
velero backup get
Expected Output:
NAME STATUS CREATED EXPIRES
mysql-backup Completed 2025-02-24 12:30:00 UTC 30d
? A full backup of MySQL and its persistent data is now stored in the cloud!
Step 4: Simulate Data Loss by Deleting the MySQL Deployment
kubectl delete statefulset mysql
kubectl delete pvc mysql-pvc
Check if MySQL is deleted:
kubectl get pods
Expected Output:
No resources found.
? Our MySQL instance and its storage are now gone.
Step 5: Restore MySQL Using Velero
Now, let’s restore the MySQL database from the backup.
velero restore create --from-backup mysql-backup --wait
Check the restore status:
velero restore get
Expected Output:
NAME STATUS STARTED COMPLETED
mysql-restore Completed 2025-02-24 12:45:00 UTC 2025-02-24 12:46:30 UTC
? MySQL and its persistent storage are now fully restored!
?? Scenario 2: Scheduled Backups for Kubernetes Workloads
Instead of manually triggering backups, we can schedule them using Velero.
Create a Backup Schedule
velero schedule create daily-backup --schedule "0 2 * * *" --include-namespaces=default
This will create a backup every day at 2 AM UTC.
Check Scheduled Backups
velero schedule get
Expected Output:
NAME SCHEDULE STATUS
daily-backup 0 2 * * * Enabled
? Velero now automatically backs up your workloads every day!
?? Scenario 3: Migrating Workloads Between Kubernetes Clusters
Velero also helps move workloads between clusters, useful for:
? Disaster recovery across different Kubernetes environments
? Migrating applications from test to production clusters
Step 1: Take a Backup in the Old Cluster
velero backup create cluster-migration --include-cluster-resources --wait
Step 2: Transfer Backup to New Cluster
Copy the backup files to the new cluster's object storage (S3, Azure, GCP).
Step 3: Restore in the New Cluster
velero restore create --from-backup cluster-migration --wait
? Your entire Kubernetes workload is now migrated to the new cluster!
?? Key Takeaways
?? Let’s Discuss!
Have you used Velero for backup and restore in Kubernetes? Do you prefer cloud-based backups or on-prem solutions? Let’s discuss in the comments!
Follow Bavithran M for more DevOps, Kubernetes, and cloud-native insights.
Found this useful? Share it with your network!
DevOps Engineer @ Phoenix Contact | Terraform Certified | DevSecOps | Kubernetes | MLOps | Leveraging AI
1 周Interesting topic Bavithran M