本文介绍在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

c77400b68402eef788f66a7db7a09f30.png

在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[@]}"

8c7e8e8bc0e343cc8e5ff572602119b9.png

准备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

980e2e4b2a0f5c7850431950408dea1d.png

在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[@]}"

2f5a9b18d29241ae2e6796776fbf79d6.png

三、开始使用修改好的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

5f178d54131fee4f06731371dfbd7f7a.png

四、创建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

3e9ca3abc832967fc06df5a765d63a7c.png

五、根据yaml文件创建mysql master ReplicationController和services

# cat 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: 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: v1

kind: Service

metadata:

name: mysql-master

labels:

name: mysql-master

spec:

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

63cb832b52d62890af7639e2f6a1da3d.png

8d0f5ad9ae39ed72908cee1c7b638ae4.png

使用mysql客户端连接master测试

def6c230ac0624b7638627c37d092eae.png

六、根据yaml文件创建mysql slave ReplicationController和services

# cat 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: 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: v1

kind: Service

metadata:

name: mysql-slave

labels:

name: mysql-slave

spec:

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

c3e47d62012cd72456c38aa8a3e7ab2d.png

使用slave 容器连接master,发现使用root账号可以正常连接,而使用repl账号无法正常连接,提示access deny

2e43477bdee02693f7dbd83b36151c40.png

修改master上的repl用户密码

mysql> alter user repl@'%' identified by "12345678";

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

完成上述修改后在slave上进行验证一下,repl账号可以正常连接

f8d10b6810bea36ad51120ed19a32661.png

在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;

b66fcc07d2c329c889e65857309750f6.png

78f7d2ecc4b971d3aea66cb5c7a876c7.png

0b1331709591d260c1c78e86d0c51c18.png

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐