边车容器(sidecar):边车容器就是与主容器一起在一个pod中运行的容器,为业务容器赋能,共享一个网络空间,所以可以用127.0.0.1:3306连接主容器的数据库 。一、背景:一开始的初衷是,想写一个脚本来监控服务器的高占用率进程并通过邮件发送给我,然后突发奇想,可以使用这种方式来备份我的数据库,开始动手!
二、设计思路:通过编写Shell脚本,调用linux的mAIl工具,MySQLDump的方式来保存数据库的sql文件,通过Mail工具添加到附件 , 最后发送到我的邮箱 。
三、编写启动脚本首先我们来编写一个启动脚本 为了方便以后的个性化配置 , 我们将脚本中的变量都提取到一个Application.yml文件中 , 文件如下:
RUNTIME: 084900 HOST: 172.16.1.2USER: rootPORT: 3306PASSword: 123456DATABASE: deanTARGETMAIL: deanmr@qq.com
接下来我们来写一下shell脚本,逻辑也很简单,当前时间与启动时间相同时,则调用sendmail函数发送邮件
#!/bin/bashRUNTIME=$(cat ./application.yml | grep RUNTIME| awk '{print $2}')HOST=$(cat ./application.yml | grep HOST| awk '{print $2}')USER=$(cat ./application.yml | grep USER| awk '{print $2}')PORT=$(cat ./application.yml | grep PORT| awk '{print $2}')PASSWORD=$(cat ./application.yml | grep PASSWORD| awk '{print $2}')DATABASE=$(cat ./application.yml | grep DATABASE| awk '{print $2}')TARGETMAIL=$(cat ./application.yml | grep TARGETMAIL| awk '{print $2}')functionsendmail(){echo -e "mysqlbak_$CURRENT_TIME" |mail -s "mysqlbak_$CURRENT_TIME" -a $DATABASE.sql $TARGETMAILsleep 1}while truedoCURRENT_TIME=$(date +%H%M%S)if [ $CURRENT_TIME = $RUNTIME ];thenecho "starting bak mysql database"sendmailcontinueelseecho $CURRENT_TIMEsleep 1fidone
四、构建镜像因为我们最后要放到k8s平台上的,所以我们要构建一个镜像,在构建镜像之前 , 请先把application.yml demo.sh Dockerfile放在同一目录下 Dockerfile如下:PS:添加了mysql的客户端,邮件mail客户端
FROM centosRUN mkdir /app && yum install -y mysql.x86_64 sendmailmailx libreport-plugin-mailx WORKDIR /appCOPY demo.sh .COPY application.yml .CMD ["/bin/sh","demo.sh"]
使用docker build命令构建镜像,要记得加一下最后的点
docker build -t mysqlmail-bak:1.0.1 .
五、添加边车容器边车容器(sidecar):边车容器就是与主容器一起在一个pod中运行的容器,为业务容器赋能,共享一个网络空间 , 所以可以用127.0.0.1:3306连接主容器的数据库 。
5.1 创建配置文件为了方便调试,我把里面的shell脚本也挂载出来 。创建两个configmap,分别对应容器内的配置文件与shell脚本,后面如果不需要调试可以取消mysqlshell的挂载 。
apiVersion: v1kind: ConfigMapmetadata:name: mysqlmail-confnamespace: deandata:application.yml: |RUNTIME: 105800HOST: 127.0.0.1USER: rootPASSWORD: 123456DATABASE: deanTARGETMAIL: deanmr@qq.com---apiVersion: v1kind: ConfigMapmetadata:name: mysqlmail-shellnamespace: deandata:demo.sh: |#!/bin/bashRUNTIME=$(cat ./application.yml | grep RUNTIME| awk '{print $2}')HOST=$(cat ./application.yml | grep HOST| awk '{print $2}')USER=$(cat ./application.yml | grep USER| awk '{print $2}')PASSWORD=$(cat ./application.yml | grep PASSWORD| awk '{print $2}')DATABASE=$(cat ./application.yml | grep DATABASE| awk '{print $2}')TARGETMAIL=$(cat ./application.yml | grep TARGETMAIL| awk '{print $2}')functionsendmail(){mysqldump -h$HOST-u$USER -p$PASSWORD--complete-insert --skip-add-drop-table--column-statistics=0 --hex-blob $DATABASE> $DATABASE.sqlecho -e "mysqlbak_$CURRENT_TIME" |mail -s "mysqlbak_$CURRENT_TIME" -a $DATABASE.sql $TARGETMAILsleep 1}while truedoCURRENT_TIME=$(date +%H%M%S)if [ $CURRENT_TIME = $RUNTIME ];thenecho "starting bak mysql database"sendmailcontinueelseecho $CURRENT_TIMEsleep 1fidone
5.2 创建有状态服务部署文件【我们一起聊聊K8s定时备份MySQL并发送到指定邮箱】我们的deploy文件使用的是上篇文章中创建的mysql有状态服务的yaml
apiVersion: apps/v1kind: StatefulSetmetadata:name: mysqlnamespace: deanspec:serviceName: mysql-serviceselector:matchLabels:app: mysqlreplicas: 1template:metadata:labels:app: mysqlspec:containers:- name: mysqlmail-bakimagePullPolicy: IfNotPresentimage: mysqlmail-bak:1.0.1volumeMounts:- name: mysqlmail-confmountPath: /app/application.ymlsubPath: application.yml- name: mysqlmail-shellmountPath: /app/demo.shsubPath: demo.sh- name: mysql-podimagePullPolicy: IfNotPresentimage: mysql:5.7env:- name: MYSQL_ROOT_PASSWORDvalue: "123456"ports:- containerPort: 3306name: msyql-listinvolumeMounts:- name: mysql-datamountPath: /var/lib/mysqlsubPath: mysql-data- name: mysql-confmountPath: /etc/mysql/conf.d/my.cnfsubPath: my.cnfvolumes:- name: mysql-datahostPath:path: /data/mysql- name: mysql-confconfigMap:name: mysql-conf- name: mysqlmail-confconfigMap:name: mysqlmail-conf- name: mysqlmail-shellconfigMap:name: mysqlmail-shell---apiVersion: v1kind: Servicemetadata:name: mysql-servicenamespace: deanlabels:app: mysqlspec:ports:- targetPort: 3306port: 3306clusterIP: Noneselector:app: mysql
推荐阅读
- qq一起听歌怎么单独退出,手机QQ一起听歌怎么设置定时播放
- 《繁花》仅用2集告诉我们,当女配太惊艳时,女主可以去打酱油了
- 红了就变了一个人!这就是我们不敢粉任何明星的原因!
- 高铁怎么购票,高铁怎么帮人一起购票
- 如何在实战中使用泛型编程?
- 同样是40+女明星,把秦岚和金莎放一起,差别一下就出来了
- 太敬业了!小沈阳零下20度连唱4首歌,脱掉手套陪观众一起挨冻
- 如何把纸质文档变成pdf 如何将纸质文件变成pdf文档
- 一文了解Redis的持久化
- 一文聊聊如何快速监控 Oracle 数据库