您的位置:首页 > 科技 > IT业 > k8s环境使用cronjob对mysql8进行备份

k8s环境使用cronjob对mysql8进行备份

2024/12/23 5:28:27 来源:https://blog.csdn.net/tingting0119/article/details/141060103  浏览:    关键词:k8s环境使用cronjob对mysql8进行备份

一、configmap

数据库备份脚本写入k8s环境的configmap文件,并生成config。

# cat mysql-back-configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:name: mysql8backnamespace: crontabs
data:mysql8back.sh: |#!/bin/bashDUMPDIR=/tmp#backupbackupDate=`date +%Y%m%d`DB_LIST=/tmp/mydb.lstmysql -h mysql8-primary.mysql8.svc.cluster.local -u root -pXXXXXX -N -e"show databases" > $DB_LIST 2>/dev/nullif [ -s ]thencat $DB_LIST |while read LINEdomysqldump -h mysql8-primary.mysql8.svc.cluster.local  -u root -pXXXXXX --opt --single-transaction --set-gtid-purged=OFF $LINE > ${DUMPDIR}/$LINE-$backupDate.sqltar -czf ${DUMPDIR}/$LINE-$backupDate.tar.gz ${DUMPDIR}/$LINE-$backupDate.sqlrm -rf ${DUMPDIR}/$LINE-$backupDate.sqldoneelseecho "No databases list get !"fi#remove tarremoveTarDate=`date -d "5 days ago" +%Y%m%d`rm -rf ${DUMPDIR}/*-$removeTarDate.tar.gz# kubectl create ns crontabs
# kubectl apply -f mysql-back-configmap.yaml

二、镜像部署

存储类得自己提前创建

# cat backup.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: mysql8baknamespace: crontabs
spec:storageClassName: "nfs-hdd"accessModes:- ReadWriteManyresources:requests:storage: 20Gi
---
apiVersion: batch/v1
kind: CronJob
metadata:name: mysql8backupnamespace: crontabs
spec:schedule: "0 0 * * *"jobTemplate:spec:template:spec:containers:- name: mysql8backupimage: dhub.kubesre.xyz/databack/mysql-backup:latestcommand: ["/bin/bash", "/opt/mysql8back.sh"]#args:#  - /opt/mysql8back.shlifecycle:postStart:exec:command: ["bash", "chmod +x /opt/mysql8back.sh"]preStop:exec:command: ["bash", "/opt/mysql8back.sh"]volumeMounts:- name: logsmountPath: /tmp- name: mysql8backupmountPath: /optvolumes:- name: logspersistentVolumeClaim:claimName: mysql8bak- name: mysql8backupconfigMap:name: mysql8backitems:- key: "mysql8back.sh"path: "mysql8back.sh"restartPolicy: OnFailure# kubectl apply -f backup.yaml

三、简单使用

# kubectl get cronjob -n crontabs  -o wide# kubectl get pod -n crontabs  -o wide# kubectl describe cronjob mysql8backup -n crontabs# kubectl logs pod/mysql8backup-28719610-knzd8 -n crontabs

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com