How to Backup MySQL databases in Kubernetes
How to Backup MySQL databases in Kubernetes
As we are adopting more and more Databases in kubernetes scheme of things, we need to be more vigilant to the Database Management. As part of our Database Management Job we need to keep the backup of the Databases, so we are safe in future if the need arises.
As i need to manage a Kubernetes cluster in IBM Cloud and have MySQL deployements in this cluster. I choose the kubernetes native Cronjob Function to schedule my MySQL DB Backup.
For Traditional MySQL DB Backup/Restore Procedure, Please click Here.
For more details on schduling repitative Job through Kubernetes Cronjob, Please click here.
Lets Start :
We'll schdule the cronjob in the Kubernestes Cluster. You need to clone the mysqldump github repository. Then create the job by using following commands.
Create secret, configmap and job(s)
kubectl create -f mysqldump/mysqldump.configmap.yaml
kubectl create -f mysqldump/mysqldump.secret.yaml
kubectl create -f mysqldump/mysqldump.scheduledjob.all.yaml
kubectl create -f mysqldump/mysqldump.secret.yaml
kubectl create -f mysqldump/mysqldump.scheduledjob.all.yaml
The github repository has three 3 yaml files.
- Configmap file
- Secret file
- Scheduler file
Configmap File:
In this file you need to give the DB Host details.
apiVersion: v1 | |
data: | |
dbhost: DB host details | |
db1: db name | |
all_databases: "true" | |
kind: ConfigMap | |
metadata: | |
name: mysqldump |
In this file you need to give the DB Credentials. Keep in mind that you need to give the "base64" encoded User Name and password.
apiVersion: v1 | |
data: | |
dbuser: user name in base64 encode | |
dbpass: password in base64 encode | |
kind: Secret | |
metadata: | |
name: mysqldump | |
type: Opaque |
In this file you need to give following details:
Job Schedule : When you want to run the job
Dump Path : Path where your Dump will be stored
apiVersion: batch/v1beta1 | |
kind: CronJob | |
metadata: | |
name: mysqldump | |
spec: | |
schedule: "*/2 * * * *" | |
jobTemplate: | |
spec: | |
template: | |
spec: | |
containers: | |
- name: mysqldump | |
image: manastri/mysqldump | |
env: | |
- name: ALL_DATABASES | |
valueFrom: | |
configMapKeyRef: | |
name: mysqldump | |
key: all_databases | |
- name: DB_HOST | |
valueFrom: | |
configMapKeyRef: | |
name: mysqldump | |
key: dbhost | |
- name: DB_USER | |
valueFrom: | |
secretKeyRef: | |
name: mysqldump | |
key: dbuser | |
- name: DB_PASS | |
valueFrom: | |
secretKeyRef: | |
name: mysqldump | |
key: dbpass | |
imagePullPolicy: Always | |
volumeMounts: | |
- mountPath: /mysqldump | |
name: mysqldump | |
volumes: | |
- name: mysqldump | |
hostPath: | |
path: /home/core/mysqldump | |
restartPolicy: OnFailure |
kubectl get jobs --watch
Interesting Articles on Kubernetes:Kubernetes : Kubernetes Node Management, Maintenance, Delete
How to add a New Worker Node to a existing kubernetes Cluster
MinIO Client Installation and Quickstart
PLEG is not healthy: Kubernetes Worker Node is in "NotReady" state
Backup MySQL databases in Kubernetes
How to Run Automated Tasks in Kubernetes with a cronjob
How to Completely remove Kubernetes
References: https://www.camil.org/backup-and-restore-mysql-databases-using-kubernetes-cron-jobs/
No comments