一、环境介绍
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.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-nfs-mysql-master
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
nfs:
path: /home/mysql_master
server: 192.168.115.6
persistentVolumeReclaimPolicy: Recycle
# cat nfs-pvc-master.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pv-nfs-mysql-master
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
# cat nfs-pv-slave.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-nfs-mysql-slave
spec:
capacity:
storage: 6Gi
accessModes:
- ReadWriteOnce
nfs:
path: /home/mysql_slave
server: 192.168.115.6
persistentVolumeReclaimPolicy: Recycle
# cat nfs-pvc-slave.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pv-nfs-mysql-slave
spec:
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