愿你,忠于自己,活得像自己。
清单:
Name | Version |
---|---|
CentOS | 7 |
Kubernetes | 1.9.0 |
Docker | 17.09.1-ce |
MySQL | 5.7 |
前言
令我始料不及的出差活动中,开始接触Kubernetes并被要求搭建基于此的MySQL主从集群,由于笔者在Linux、Kubernetes等方面都是小白,故此展开了填坑活动,写本文目的只是为了让其他程序猿少踩坑,下面简单介绍下MySQL主从集群。
我们用MySQL集群的分布式部署来实现MySQL主从模式,在MySQL集群中,Master节点主要负责数据的分发和slave节点的管理。因此Master节点主要负责数据的写入和分发(包括insert、update、delete)。而slave节点主要负责数据的读取(包括select)。基本的master和slave的分工作业,如下图所示。
MySQL集群架构
MySQL集群在主从模式下实现一台服务充当主服务器,其他一台或者多台服务器充当从服务器。下面我们开始搭建。
一、安装MySQL
- 经深入研究后发现,docker镜像不用自行安装MySQL,如需安装MySQL,请转我的另一篇文章:https://www.jianshu.com/p/a6c5ec995660
二、构建Docker镜像
- 构建镜像需要以下操作,先简单说明后面步骤将此部分内容写入Dockerfile中
master配置
- 文件/etc/my.cnf中[mysqld]下添加配置
log-bin=mysql-bin
server-id=1
- 重启MySQL
# service mysqld restart
- 创建同步账号
# mysql -u root -p
# create user 'sync'@'%' identified by 'MySQL@lipuan.2018';
- 赋予同步账号远程权限
# grant replication slave on *.* to 'sync'@'%' identified by 'MySQL@lipuan.2018';
slave配置
- 文件/etc/my.cnf中[mysqld]下添加配置
log-bin=mysql-bin
server-id=2
- 重启MySQL
# service mysqld restart
- 配置主机IP
# mysql -u root -p
# change master to master_host='x.x.x.x',master_user='sync',master_password='MySQL@lipuan.2018';
注意: server-id为主机标识,不能重复。x.x.x.x'为master服务器IP
- 下面我们将以上的配置写入Dockerfile中
准备master镜像
- 下载MySQL官方Docker镜像文件,并解压
# wget https://codeload.github.com/docker-library/mysql/zip/master
# unzip master
# mkdir /home/lipuan/mysqlDockerFile
# cp -Rf mysql-master/5.7/Dockerfile /home/lipuan/mysqlDockerFile
# cp -Rf mysql-master/5.7/docker-entrypoint.sh /home/lipuan/mysqlDockerFile
# rm -rf mysql-master
# chmod +x docker-entrypoint.sh
- Dockerfile中添加,位置如下图所示
RUN sed -i '/\[mysqld\]/a server-id=1\nlog-bin' /etc/mysql/mysql.conf.d/mysqld.cnf
Dockerfile-master
- 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[@]}"
entrypoint-master
准备slave的镜像
- 下载MySQL官方Docker镜像文件
# wget https://codeload.github.com/docker-library/mysql/zip/master
# unzip master
# mkdir /home/lipuan/mysqlDockerFile
# cd ~
# cp -Rf mysql-master/5.7/Dockerfile /home/lipuan/mysqlDockerFile
# cp -Rf mysql-master/5.7/docker-entrypoint.sh /home/lipuan/mysqlDockerFile
# rm -rf mysql-master
# chmod +x docker-entrypoint.sh
- Dockerfile中添加,位置如下图所示
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
此处server-id用的是随机数
Dockerfile-slave
2.3 docker-entrypoint.sh中添加,位置如下图所示
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[@]}"
slave配置中,master_host一项用的是$MYSQL_MASTER_SERVICE_HOST,这个环境变量(enviromnent variable)是由k8s生成的。
entrypoint-slave.png
- 构建master镜像
# cd /home/lipuan/mysqlDockerFile/
# docker build -t paulliu/mysql-master:0.1 .
- 构建slave镜像
# cd /home/lipuan/mysqlDockerFile/
# docker build -t paulliu/mysql-slave:0.1 .
- 上传镜像
# docker login
# docke push paulliu/mysql-master:0.1
# docke push paulliu/mysql-slave:0.1
三、MySQL集群部署
-
# cd /home/lipuan/DockerBuild
此后内容若无指出均在此目录下执行 - 此部分操作,所有yaml文件缩进空格必须严格,否则无法构建成功
master
- 新建mysql-master-rc.yaml文件
# touch mysql-master-rc.yaml
添加内容
apiVersion: v1
kind: ReplicationController
metadata:
name: mysql-master
labels:
name: mysql-master
spec:
replicas: 1
selector:
name: mysql-master
template:
metadata:
labels:
name: mysql-master
spec:
containers:
- name: master
image: paulliu/mysql-master:0.1
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "MySQL@lipuan.2018"
- name: MYSQL_REPLICATION_USER
value: "sync"
- name: MYSQL_PASSWORD
value: "MySQL@lipuan.2018"
- 新建mysql-master-service.yaml文件
# touch mysql-master-service.yaml
添加内容
apiVersion: v1
kind: Service
metadata:
name: mysql-master
labels:
name: mysql-master
spec:
ports:
- port: 3306
targetPort: 3306
selector:
name: mysql-master
- 部署MySQL master服务
# kubectl create -f mysql-master-rc.yaml
# kubectl create -f mysql-master-service.yaml
- 查看运行情况
# kubectl get pods,service,rc
slave
- 新建mysql-slave-rc.yaml文件
# touch mysql-slave-rc.yaml
添加内容
apiVersion: v1
kind: ReplicationController
metadata:
name: mysql-slave
labels:
name: mysql-slave
spec:
replicas: 1
selector:
name: mysql-slave
template:
metadata:
labels:
name: mysql-slave
spec:
containers:
- name: master
image: paulliu/mysql-slave:0.1
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "MySQL@lipuan.2018"
- name: MYSQL_REPLICATION_USER
value: "sync"
- name: MYSQL_PASSWORD
value: "MySQL@lipuan.2018"
- 新建mysql-slave-service.yaml文件
# touch mysql-slave-service.yaml
添加内容
apiVersion: v1
kind: Service
metadata:
name: mysql-slave
labels:
name: mysql-slave
spec:
ports:
- port: 3306
targetPort: 3306
selector:
name: mysql-slave
- 部署MySQL slave服务
# kubectl create -f mysql-slave-rc.yaml
# kubectl create -f mysql-slave-service.yaml
- 查看运行情况
# kubectl get pods,service,rc
写在文末
写到这里基于Kubernetes的MySQL主从集群就搭建成功了,如果有哪位朋友对于其中步骤不是很清楚,欢迎互相学习。
本文是笔者首次写技术博客,如有错漏等问题,还望各位大大指出批评!
更新日志
- 2018.01.13 在准备镜像部分添加了对于docker-entrypoint.sh文件授予可执行权限,若无此步骤可能导致镜像无法运行;
网友评论
mysql-master-service.yaml
根据这个来设置的话,3306无法暴露到公网ip地址,无法做hostport,如果做负载均衡,会临时创建一个port,可以用ip+port连接,但是想用宿主机的固定ip+固定port访问好像不行,请指点迷津
304724132@qq.com