邊車容器(sidecar):邊車容器就是與主容器一起在一個pod中運行的容器,為業務容器賦能,共享一個網絡空間,所以可以用127.0.0.1:3306連接主容器的數據庫。
一、背景:
一開始的初衷是,想寫一個腳本來監控服務器的高占用率進程并通過郵件發送給我,然后突發奇想,可以使用這種方式來備份我的數據庫,開始動手!
二、設計思路:
通過編寫Shell腳本,調用linux的mAIl工具,MySQLDump的方式來保存數據庫的sql文件,通過Mail工具添加到附件,最后發送到我的郵箱。
三、編寫啟動腳本
首先我們來編寫一個啟動腳本 為了方便以后的個性化配置,我們將腳本中的變量都提取到一個Application.yml文件中,文件如下:
RUNTIME: 084900
HOST: 172.16.1.2
USER: root
PORT: 3306
PASSword: 123456
DATABASE: dean
TARGETMAIL: deanmr@qq.com
接下來我們來寫一下shell腳本,邏輯也很簡單,當前時間與啟動時間相同時,則調用sendmail函數發送郵件
#!/bin/bash
RUNTIME=$(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}')
function sendmail(){
echo -e "mysqlbak_$CURRENT_TIME" |mail -s "mysqlbak_$CURRENT_TIME" -a $DATABASE.sql $TARGETMAIL
sleep 1
}
while true
do
CURRENT_TIME=$(date +%H%M%S)
if [ $CURRENT_TIME = $RUNTIME ];then
echo "starting bak mysql database"
sendmail
continue
else
echo $CURRENT_TIME
sleep 1
fi
done
四、構建鏡像
因為我們最后要放到k8s平臺上的,所以我們要構建一個鏡像,在構建鏡像之前,請先把application.yml demo.sh Dockerfile放在同一目錄下 Dockerfile如下:PS:添加了mysql的客戶端,郵件mail客戶端
FROM centos
RUN mkdir /app && yum install -y mysql.x86_64 sendmail mailx libreport-plugin-mailx
WORKDIR /app
COPY 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: v1
kind: ConfigMap
metadata:
name: mysqlmail-conf
namespace: dean
data:
application.yml: |
RUNTIME: 105800
HOST: 127.0.0.1
USER: root
PASSWORD: 123456
DATABASE: dean
TARGETMAIL: deanmr@qq.com
---
apiVersion: v1
kind: ConfigMap
metadata:
name: mysqlmail-shell
namespace: dean
data:
demo.sh: |
#!/bin/bash
RUNTIME=$(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}')
function sendmail(){
mysqldump -h$HOST -u$USER -p$PASSWORD --complete-insert --skip-add-drop-table --column-statistics=0 --hex-blob $DATABASE > $DATABASE.sql
echo -e "mysqlbak_$CURRENT_TIME" |mail -s "mysqlbak_$CURRENT_TIME" -a $DATABASE.sql $TARGETMAIL
sleep 1
}
while true
do
CURRENT_TIME=$(date +%H%M%S)
if [ $CURRENT_TIME = $RUNTIME ];then
echo "starting bak mysql database"
sendmail
continue
else
echo $CURRENT_TIME
sleep 1
fi
done
5.2 創建有狀態服務部署文件
我們的deploy文件使用的是上篇文章中創建的mysql有狀態服務的yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
namespace: dean
spec:
serviceName: mysql-service
selector:
matchLabels:
app: mysql
replicas: 1
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysqlmail-bak
imagePullPolicy: IfNotPresent
image: mysqlmail-bak:1.0.1
volumeMounts:
- name: mysqlmail-conf
mountPath: /app/application.yml
subPath: application.yml
- name: mysqlmail-shell
mountPath: /app/demo.sh
subPath: demo.sh
- name: mysql-pod
imagePullPolicy: IfNotPresent
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
ports:
- containerPort: 3306
name: msyql-listin
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
subPath: mysql-data
- name: mysql-conf
mountPath: /etc/mysql/conf.d/my.cnf
subPath: my.cnf
volumes:
- name: mysql-data
hostPath:
path: /data/mysql
- name: mysql-conf
configMap:
name: mysql-conf
- name: mysqlmail-conf
configMap:
name: mysqlmail-conf
- name: mysqlmail-shell
configMap:
name: mysqlmail-shell
---
apiVersion: v1
kind: Service
metadata:
name: mysql-service
namespace: dean
labels:
app: mysql
spec:
ports:
- targetPort: 3306
port: 3306
clusterIP: None
selector:
app: mysql
六、測試
我們上面給他定的時間是RUNTIME: 105800,上海時區也就是18點58分,我們來看一下效果 查看日志, 注意:當一個pod包含多個容器時,要使用 -c 參數指定查看哪個容器
[root@VM-24-15-centos solo]# kubectl logs -n solo mysql-0 -c mysqlmail-bak | grep mysql -C 5
105755
105756
105757
105758
105759
starting bak mysql database
mysqldump: [Warning] Using a password on the command line interface can be insecure.
105801
105802
從日志可以看到,郵件已經發送成功了!我們來去郵箱看一下,發現也已經成功了,至此我們的實驗完美完成!