原 【DB宝42】MySQL高可用架构MHA+ProxySQL实现读写分离和负载均衡
Tags: 原创MySQL高可用监控主从复制MHA负载均衡读写分离ProxySQL
一、MHA+ProxySQL架构
之前发过一篇MHA的文章,介绍了MHA相关的知识和功能测试,连接为:【DB宝19】在Docker中使用MySQL高可用之MHA 。今天这一篇给大家分享一下“MHA+中间件ProxySQL”来实现读写分离+负载均衡的相关知识。
我们都知道,MHA(Master High Availability Manager and tools for MySQL)目前在MySQL高可用方面是一个相对成熟的解决方案,是一套作为MySQL高可用性环境下故障切换和主从提升的高可用软件。它的架构是要求一个MySQL复制集群必须最少有3台数据库服务器,一主二从,即一台充当Master,一台充当备用Master,另一台充当从库。但是,如果不连接任何外部的数据库中间件,那么就会导致所有的业务压力流向主库,从而造成主库压力过大,而2个从库除了本身的IO和SQL线程外,无任何业务压力,会严重造成资源的浪费。因此,我们可以把MHA和ProxySQL结合使用来实现读写分离和负载均衡。所有的业务通过中间件ProxySQL后,会被分配到不同的MySQL机器上。从而,前端的写操作会流向主库,而读操作会被负载均衡的转发到2个从库上。
MHA+ProxySQL架构如下图所示:
二、快速搭建MHA环境
2.1 下载MHA镜像
- 小麦苗的Docker Hub的地址:https://hub.docker.com/u/lhrbest
1 2 3 4 5 6 7 8 9 10 11 | -- 下载镜像 docker pull registry.cn-hangzhou.aliyuncs.com/lhrbest/mha-lhr-master1-ip131 docker pull registry.cn-hangzhou.aliyuncs.com/lhrbest/mha-lhr-slave1-ip132 docker pull registry.cn-hangzhou.aliyuncs.com/lhrbest/mha-lhr-slave2-ip133 docker pull registry.cn-hangzhou.aliyuncs.com/lhrbest/mha-lhr-monitor-ip134 -- 重命名镜像 docker tag registry.cn-hangzhou.aliyuncs.com/lhrbest/mha-lhr-master1-ip131 lhrbest/mha-lhr-master1-ip131 docker tag registry.cn-hangzhou.aliyuncs.com/lhrbest/mha-lhr-slave1-ip132 lhrbest/mha-lhr-slave1-ip132 docker tag registry.cn-hangzhou.aliyuncs.com/lhrbest/mha-lhr-slave2-ip133 lhrbest/mha-lhr-slave2-ip133 docker tag registry.cn-hangzhou.aliyuncs.com/lhrbest/mha-lhr-monitor-ip134 lhrbest/mha-lhr-monitor-ip134 |
一共4个镜像,3个MHA Node,一个MHA Manager,压缩包大概3G,下载完成后:
1 2 3 4 5 | [root@lhrdocker ~]# docker images | grep mha registry.cn-hangzhou.aliyuncs.com/lhrbest/mha-lhr-monitor-ip134 latest 7d29597dc997 14 hours ago 1.53GB registry.cn-hangzhou.aliyuncs.com/lhrbest/mha-lhr-slave2-ip133 latest d3717794e93a 40 hours ago 4.56GB registry.cn-hangzhou.aliyuncs.com/lhrbest/mha-lhr-slave1-ip132 latest f62ee813e487 40 hours ago 4.56GB registry.cn-hangzhou.aliyuncs.com/lhrbest/mha-lhr-master1-ip131 latest ae7be48d83dc 40 hours ago 4.56GB |
2.2 编辑yml文件,创建MHA相关容器
编辑yml文件,使用docker-compose来创建MHA相关容器,注意docker-compose.yml文件的格式,对空格、缩进、对齐都有严格要求:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | # 创建存放yml文件的路径 mkdir -p /root/mha # 编辑文件/root/mha/docker-compose.yml cat > /root/mha/docker-compose.yml <<"EOF" version: '3.8' services: MHA-LHR-Master1-ip131: container_name: "MHA-LHR-Master1-ip131" restart: "always" hostname: MHA-LHR-Master1-ip131 privileged: true image: lhrbest/mha-lhr-master1-ip131 ports: - "33131:3306" - "2201:22" networks: mhalhr: ipv4_address: 192.168.68.131 MHA-LHR-Slave1-ip132: container_name: "MHA-LHR-Slave1-ip132" restart: "always" hostname: MHA-LHR-Slave1-ip132 privileged: true image: lhrbest/mha-lhr-slave1-ip132 ports: - "33132:3306" - "2202:22" networks: mhalhr: ipv4_address: 192.168.68.132 MHA-LHR-Slave2-ip133: container_name: "MHA-LHR-Slave2-ip133" restart: "always" hostname: MHA-LHR-Slave2-ip133 privileged: true image: lhrbest/mha-lhr-slave2-ip133 ports: - "33133:3306" - "2203:22" networks: mhalhr: ipv4_address: 192.168.68.133 MHA-LHR-Monitor-ip134: container_name: "MHA-LHR-Monitor-ip134" restart: "always" hostname: MHA-LHR-Monitor-ip134 privileged: true image: lhrbest/mha-lhr-monitor-ip134 ports: - "33134:3306" - "2204:22" networks: mhalhr: ipv4_address: 192.168.68.134 networks: mhalhr: name: mhalhr ipam: config: - subnet: "192.168.68.0/16" EOF |