Backing Up MySQL Database using Openshift/Kubernetes Cron Job
Periodic Backups
When running an application in production, the data and app should be backed up periodically. One way to do that is by using git repository for the source code and periodically dumping the database. This article shows how to do such periodic jobs using CronJob functionality of Openshift/Kubernetes.
Technique
When running a cronjob, the filesystem that being used to run the pod will be ephemeral, that is, non-persistent. To work around this limit we should mount from external volume, which can be in the form of persistent volume claim or directly provided volume specification.
The Job specs
apiVersion: batch/v1kind: Job
metadata:
name: job04-test
spec:
template:
spec:
containers:
- name: job04c-test
image: centos/mysql-57-centos7
command: ["/bin/sh","-c", "/opt/rh/rh-mysql57/root/usr/bin/mysqldump --single-transaction -u $MYSQL_USER $MYSQL_DB -h $MYSQL_HOST | gzip > /tmp/dump`date '+%Y%m%d'`.sql.gz"]
volumeMounts:
- mountPath: /tmp
name: dbdumpsvol
subPath: job03
env :
- name: MYSQL_USER
value: "dbuser"
- name: MYSQL_DB
value: "dbname"
- name: MYSQL_PWD
value: "xxxx"
- name: MYSQL_HOST
value: "10.1.2.3"
restartPolicy: Never
volumes:
- name: dbdumpsvol
persistentVolumeClaim:
claimName: dbdumps
backoffLimit: 4
This job is using persistent volume claim, in which the persistent volume need to be prepared before. If you want to use direct specification, for instance referencing hostpath, the spec will be like this :
apiVersion: batch/v1
kind: Job
metadata:
name: job05-hostpath
spec:
template:
spec:
containers:
- name: job05
image: centos/mysql-57-centos7
command: ["/bin/sh","-c", "/opt/rh/rh-mysql57/root/usr/bin/mysqldump --single-transaction -u $MYSQL_USER $MYSQL_DB -h $MYSQL_HOST | gzip > /tmp/dump`date '+%Y%m%d'`.sql.gz"]
volumeMounts:
- mountPath: /tmp
name: dbdumpsvol
subPath: job05
env :
- name: MYSQL_USER
value: "dbuser"
- name: MYSQL_DB
value: "dbname"
- name: MYSQL_PWD
value: "xxxx"
- name: MYSQL_HOST
value: "10.1.2.3"
restartPolicy: Never
volumes:
- name: dbdumpsvol
hostPath:
path: /data/dbdumps
type: Directory
backoffLimit: 4
To run the job, save the job spec into a file (example: job05.yaml) first. If using kubernetes, this should do the job :
kubectl apply -f job05.yaml
or if using openshift command line interface :
oc create -f job05.yaml
or if using openshift web console:
- login to console, choose one existing project
- click add to project
- click import YAML/JSON
- paste the job spec
- click create
Converting To CronJob
A cronjob is just a job with a cron schedule, for instance this cronjob is running once every hour at the 27th minute :
apiVersion: batch/v2alpha1
kind: CronJob
metadata:
name: crondbdump1
spec:
schedule: "27 * * * *"
successfulJobsHistoryLimit: 7
jobTemplate:
spec:
template:
metadata:
labels:
parent: "crondumpifrs"
spec:
containers:
- name: crondbdump1
image: centos/mysql-57-centos7
command: ["/bin/sh","-c", "/opt/rh/rh-mysql57/root/usr/bin/mysqldump --single-transaction --routines -u $MYSQL_USER $MYSQL_DB -h $MYSQL_HOST | gzip > /tmp/dump`date '+%Y%m%d_%H%M%S'`.sql.gz"]
volumeMounts:
- mountPath: /tmp
name: dbdumpsvol
subPath: job03
env :
- name: MYSQL_USER
value: "dbuser"
- name: MYSQL_DB
value: "dbname"
- name: MYSQL_PWD
value: "xxxx"
- name: MYSQL_HOST
value: "10.1.2.3"
restartPolicy: OnFailure
volumes:
- name: dbdumpsvol
hostPath:
path: /data/dbdumps
type: Directory
To run the cronjob, use the same procedure (oc create -f and kubectl apply -f ). In Openshift, we are limited to command-line interface monitoring the cron jobs :
oc get cronjobs
NAME SCHEDULE SUSPEND ACTIVE LAST-SCHEDULE
crondumpifrs2 27 * * * * True 0 Mon, 16 Apr 2018 16:27:00 +0700
oc edit crondumpifrs
Comments