標籤:host 檔案建立 request val ssm lob 環境 ant follow
本文介紹在kubernetes環境中部署mysql主從叢集,資料持久化採用nfs。
一、環境介紹
Mysql版本:5.7
Mysql master節點:
主機名稱:vm1
IP地址:192.168.115.5/24
Mysql slave節點:
主機名稱:vm2
IP地址:192.168.115.6/24
NFS節點:
主機名稱:vm2
IP地址:192.168.115.6/24
共用目錄:/home/mysql_master、/home/mysql_slave
二、準備mysql主從的鏡像環境
dockerfile、docker-entrypoint.sh 檔案如下
https://github.com/docker-library/mysql/tree/master/5.7
由於我們要配置mysql主從,所以需要對dockerfile、docker-entrypoint.sh 檔案做一點的修改,主要是在mysql主從配置部分。
準備master的鏡像
將Dockerfile, docker-entrypoint.sh複製一份用於build master鏡像檔案。
在Dockerfile中添加如下內容,將mysql master的server-id設定為1
RUN sed -i ‘/\[mysqld\]/a server-id=1\nlog-bin‘ /etc/mysql/mysql.conf.d/mysqld.cnf
在docker-entrypoint.sh中添加如下內容,建立一個複製使用者並賦許可權,重新整理系統許可權表
echo "CREATE USER ‘$MYSQL_REPLICATION_USER‘@‘%‘ IDENTIFIED BY ‘$MYSQL_REPLICATION_PASSWORD‘ ;" | "${mysql[@]}" echo "GRANT REPLICATION SLAVE ON *.* TO ‘$MYSQL_REPLICATION_USER‘@‘%‘ IDENTIFIED BY ‘$MYSQL_REPLICATION_PASSWORD‘ ;" | "${mysql[@]}" echo ‘FLUSH PRIVILEGES ;‘ | "${mysql[@]}"
準備slave的鏡像
將Dockerfile, docker-entrypoint.sh複製一份用於build slave鏡像檔案。
在Dockerfile中添加如下內容,將mysql slave的server-id設定為一個隨機數
RUN RAND="$(date +%s | rev | cut -c 1-2)$(echo ${RANDOM})" && sed -i ‘/\[mysqld\]/a server-id=‘$RAND‘\nlog-bin‘ /etc/mysql/mysql.conf.d/mysqld.cnf
在docker-entrypoint.sh中添加如下內容,配置串連master主機的host、user、password等參數,並啟動複製進程。
echo "STOP SLAVE;" | "${mysql[@]}" echo "CHANGE MASTER TO master_host=‘$MYSQL_MASTER_SERVICE_HOST‘, master_user=‘$MYSQL_REPLICATION_USER‘, master_password=‘$MYSQL_REPLICATION_PASSWORD‘ ;" | "${mysql[@]}" echo "START SLAVE;" | "${mysql[@]}"
三、開始使用修改好的dockerfile建立mysql master和slave鏡像
# cd /root/kubernetes/lnmp/mysql/Dockerfiles/Master# docker build -t registry.fjhb.cn/mysql-master:0.1 .# cd /root/kubernetes/lnmp/mysql/Dockerfiles/Slave# docker build -t registry.fjhb.cn/mysql-slave:0.1 .# docker push registry.fjhb.cn/mysql-master:0.1# docker push registry.fjhb.cn/mysql-slave:0.1
四、建立pv和pvc,用於mysql主從儲存持久化資料
在nfs伺服器上把目錄建立出來
# cd /home/# mkdir mysql_master mysql_slave
通過yaml檔案建立出兩組pv和pvc
# cd /root/kubernetes/lnmp/mysql/Storage# cat nfs-pv-master.yamlapiVersion: v1kind: PersistentVolumemetadata: name: pv-nfs-mysql-masterspec: capacity: storage: 5Gi accessModes: - ReadWriteOnce nfs: path: /home/mysql_master server: 192.168.115.6 persistentVolumeReclaimPolicy: Recycle # cat nfs-pvc-master.yamlkind: PersistentVolumeClaimapiVersion: v1metadata: name: pv-nfs-mysql-masterspec: accessModes: - ReadWriteOnce resources: requests: storage: 5Gi# cat nfs-pv-slave.yaml apiVersion: v1kind: PersistentVolumemetadata: name: pv-nfs-mysql-slavespec: capacity: storage: 6Gi accessModes: - ReadWriteOnce nfs: path: /home/mysql_slave server: 192.168.115.6 persistentVolumeReclaimPolicy: Recycle # cat nfs-pvc-slave.yaml kind: PersistentVolumeClaimapiVersion: v1metadata: name: pv-nfs-mysql-slavespec: accessModes: - ReadWriteOnce resources: requests: storage: 6Gi# kubectl create -f nfs-pv-master.yaml# kubectl create -f nfs-pvc-master.yaml# kubectl create -f nfs-pv-slave.yaml # kubectl create -f nfs-pvc-slave.yaml
五、根據yaml檔案建立mysql master ReplicationController和services
# cat mysql-master-rc.yaml apiVersion: v1kind: ReplicationControllermetadata: name: mysql-master labels: name: mysql-masterspec: replicas: 1 selector: name: mysql-master template: metadata: labels: name: mysql-master spec: containers: - name: mysql-master image: registry.fjhb.cn/mysql-master:0.1 volumeMounts: - mountPath: /var/lib/mysql name: mysql-master-data ports: - containerPort: 3306 env: - name: MYSQL_ROOT_PASSWORD value: "12345678" - name: MYSQL_REPLICATION_USER value: "repl" - name: MYSQL_REPLICAITON_PASSWORD value: "12345678" volumes: - name: mysql-master-data persistentVolumeClaim: claimName: pv-nfs-mysql-master# cat mysql-master-svc.yaml apiVersion: v1kind: Servicemetadata: name: mysql-master labels: name: mysql-masterspec: type: NodePort ports: - port: 3306 targetPort: 3306 name: http nodePort: 30066 selector: name: mysql-master# kubectl create -f mysql-master-rc.yaml # kubectl create -f mysql-master-svc.yaml
使用mysql用戶端串連master測試
六、根據yaml檔案建立mysql slave ReplicationController和services
# cat mysql-slave-rc.yaml apiVersion: v1kind: ReplicationControllermetadata: name: mysql-slave labels: name: mysql-slavespec: replicas: 1 selector: name: mysql-slave template: metadata: labels: name: mysql-slave spec: containers: - name: mysql-slave image: registry.fjhb.cn/mysql-slave:0.1 volumeMounts: - mountPath: /var/lib/mysql name: mysql-slave-data ports: - containerPort: 3306 env: - name: MYSQL_ROOT_PASSWORD value: "12345678" - name: MYSQL_REPLICATION_USER value: "repl" - name: MYSQL_REPLICAITON_PASSWORD value: "12345678" volumes: - name: mysql-slave-data persistentVolumeClaim: claimName: pv-nfs-mysql-slave# cat mysql-slave-svc.yaml apiVersion: v1kind: Servicemetadata: name: mysql-slave labels: name: mysql-slavespec: type: NodePort ports: - port: 3306 targetPort: 3306 name: http nodePort: 30067 selector: name: mysql-slave# kubectl create -f mysql-slave-rc.yaml # kubectl create -f mysql-slave-svc.yaml
七、測試與排錯
通過mysql命令列串連slave查看複製的狀態,發現狀態是connecting
使用slave 容器串連master,發現使用root帳號可以正常串連,而使用repl帳號無法正常串連,提示access deny
修改master上的repl使用者密碼
mysql> alter user [email protected]‘%‘ identified by "12345678";Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)
完成上述修改後在slave上進行驗證一下,repl帳號可以正常串連
在slave上進行change master操作,因為建立鏡像的時候並未配置GTID參數,所以這裡不能使用MASTER_AUTO_POSITION=1參數,如果要開啟,需要去修改Dockerfile,重建鏡像。
mysql> stop slave;mysql> set global sql_slave_skip_counter=1;CHANGE MASTER TO MASTER_HOST=‘mysql-master‘, MASTER_USER=‘repl‘, MASTER_PASSWORD=‘12345678‘;mysql> start slave;
參考文檔:
https://www.jianshu.com/p/509b65e9a4f5
在kubernetes叢集中部署mysql主從