依据Kubernetes搭建MySQL主从集群

甘当君,忠于自己,活得如自己。

清单:
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配置

  1. 文件/etc/my.cnf中[mysqld]下添加配置

log-bin=mysql-bin
server-id=1
  1. 重启MySQL
    # service mysqld restart
  2. 创办同账号

# mysql -u root -p
# create user 'sync'@'%' identified by 'MySQL@lipuan.2018';
  1. 与同步账号远程权限
    # grant replication slave on *.* to 'sync'@'%' identified by 'MySQL@lipuan.2018';

slave配置

  1. 文件/etc/my.cnf中[mysqld]下添加配置

log-bin=mysql-bin
server-id=2
  1. 重启MySQL
    # service mysqld restart
  2. 布置主机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镜像

  1. 下载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
  1. Dockerfile中添加,位置如下图所示

RUN sed -i '/\[mysqld\]/a server-id=1\nlog-bin' /etc/mysql/mysql.conf.d/mysqld.cnf

Dockerfile-master

  1. 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的镜像

  1. 下载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
  1. 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

  1. 构建master镜像

# cd /home/lipuan/mysqlDockerFile/
# docker build -t paulliu/mysql-master:0.1 .
  1. 构建slave镜像

# cd /home/lipuan/mysqlDockerFile/
# docker build -t paulliu/mysql-slave:0.1 .
  1. 及传镜像

# docker login
# docke push paulliu/mysql-master:0.1
# docke push paulliu/mysql-slave:0.1

老三、MySQL集群部署

  • # cd /home/lipuan/DockerBuild从此以后情要无指出均以这目录下实行
  • 这个部分操作,所有yaml文件缩进空格必须从严,否则无法构建成功

master

  1. 新建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"
  1. 新建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
  1. 部署MySQL master服务

# kubectl create -f mysql-master-rc.yaml
# kubectl create -f mysql-master-service.yaml
  1. 查看运行状况
    # kubectl get pods,service,rc

slave

  1. 新建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"
  1. 新建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
  1. 部署MySQL slave服务

# kubectl create -f mysql-slave-rc.yaml
# kubectl create -f mysql-slave-service.yaml
  1. 查运行状态
    # kubectl get pods,service,rc

描绘在文末

写及这里因Kubernetes的MySQL主从集群就搭建成功了,如果发生哪个朋友对里边步骤不是特别懂得,欢迎互相学习。
正文是作者首不善写技术博客,如产生错漏等问题,还为各位大大指出批评!

创新日志

  1. 2018.01.13
    在准备镜像部分添加了对于docker-entrypoint.sh文件授予可实施权,若无此步骤可能造成镜像无法运行;
网站地图xml地图