愿你,忠于自己,活得像自己。
清单:
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镜像部分
若已安装但忘记密码请直接看步骤13
检查是否已经安装MySQL
rpm -qa | grep mysql
下载MySQL源安装包
# wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
安装MySQL源
# yum localinstall mysql57-community-release-el7-8.noarch.rpm
检查MySQL源是否安装成功
# yum repolist enabled | grep "mysql.*-community.*"
yum资源列表
安装MySQL
# yum install mysql-community-server
启动MySQL服务
# systemctl start mysqld
设置开机启动(如不需要可跳过)
# systemctl enable mysqld
# systemctl daemon-reload
获取MySQL默认密码
# grep 'temporary password' /var/log/mysqld.log
MySQL默认密码
修改密码(需包含大小写英文、数字、特殊字符且大于8位)
# mysql -u root -p
# set password for 'root'@'localhost'=password('MySQL@lipuan.2018');
配置默认编码utf8,文件/etc/my.cnf中[mysqld]下加入
character_set_server=utf8
init_connect='SET NAMES utf8'
关键目录
配置文件: /etc/my.cnf
日志文件: /var/log//var/log/mysqld.log
服务启动脚本: /usr/lib/systemd/system/mysqld.service
socket文件: /var/run/mysqld/mysqld.pid
忘记密码操作,文件/etc/my.cnf中[mysqld]下加入
skip-grant-tables
重启MySQL
# service mysqld restart
转至步骤9修改密码,成功后删除文件/etc/my.cnf中的skip-grant-tables
二、构建Docker镜像
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
准备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
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
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主从集群就搭建成功了,如果有哪位朋友对于其中步骤不是很清楚,欢迎互相学习。
本文是笔者首次写技术博客,如有错漏等问题,还望各位大大指出批评!