Docker搭建MySQL负载均衡

Docker 拉取MySQL集群

https://www.cnblogs.com/zuoyoua/p/17212377.html

前言: 为什么要搭建负载均衡

# 在搭好集群的情况下,负载均衡可以消除服务器之间的负载不平衡,可以优化访问请求在服务器组之间的分配,提高系统的反应速度和总体性能
# 负载均衡能够监控服务器的运行状态,提高整个服务器组的可靠性。提高系统的扩展能力,简化管理。

1.安装Haproxy

# docker pull 拉取 Haproxy1.8.8版本

[root@localhost ~]# docker pull haproxy:1.8.8
1.8.8: Pulling from library/haproxy
f2aa67a397c4: Pull complete 
fbe89b1fc408: Pull complete 
3860fe2d1a0f: Pull complete 
bd6e6f0896cb: Pull complete 
Digest: sha256:559c1e881f4d0c8434caade0c3814dce4ec57b5c478783ed387f8eaf00967fbc
Status: Downloaded newer image for haproxy:1.8.8
docker.io/library/haproxy:1.8.8
[root@localhost ~]# 

2.编辑Haproxy配置文件

# pull拉取下来的Haproxy镜像不包含配置文件
# 我的本机没有soft文件夹 所以在这里 -p连续创建一下 
# 查看自己的目录如果有soft文件夹 在这里可以不用 -p
[root@localhost ~]# mkdir -p /home/soft/haproxy
[root@localhost ~]# 

# 进入 vi 编辑器模式  按i键进行编辑
[root@localhost ~]# vi /home/soft/haproxy/haproxy.cfg
# 复制配置文件
global
	chroot /usr/local/etc/haproxy
	log 127.0.0.1 local5 info
	daemon

defaults
	log	global
	mode	http
	option	httplog
	option	dontlognull
	timeout connect 5000
	timeout client  50000
    timeout server  50000

listen  admin_stats
	bind  0.0.0.0:8888
    mode        http
    stats uri   /dbs
    stats realm     Global\ statistics
    stats auth  admin:abc123456
    
listen  proxy-mysql
	bind  0.0.0.0:3306  
	mode  tcp
    balance  roundrobin
    option  tcplog
    option  mysql-check user haproxy
    server  MySQL_1 172.18.0.2:3306 check weight 1 maxconn 2000  
    server  MySQL_2 172.18.0.3:3306 check weight 1 maxconn 2000  
	server  MySQL_3 172.18.0.4:3306 check weight 1 maxconn 2000 
	server  MySQL_4 172.18.0.5:3306 check weight 1 maxconn 2000
    option  tcpka  
# ESC键退出编辑模式  按一下「 :」冒号键 输入wq进行保存退出

3.进入容器,创建MySQL用户

# 进入你的MySQL 用命令创建用户  create user 'haproxy'@'%'  identified by '';  输入完退出MySQL exit 退出  然后退出容器 CTRL+P+Q 
[root@localhost ~]# docker exec -it node1 /bin/bash
root@29d17de3f772:/# mysql -uroot -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 5.7.20-18-57-log Percona XtraDB Cluster (GPL), Release rel18, Revision 4a4da7e, WSREP version 29.24, wsrep_29.24

Copyright (c) 2009-2017 Percona LLC and/or its affiliates
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

# mysql> create user 'haproxy'@'%'  identified by '';
Query OK, 0 rows affected (0.04 sec)

# mysql> exit
Bye
root@29d17de3f772:/# read escape sequence
[root@localhost ~]# 

4.创建Haproxy容器

# 运行 Haproxy 容器 
# 输入docker run -it -d -p 4001:8888 -p 4002:3306 -v /home/soft/haproxy:/usr/local/etc/haproxy --name haproxy1 --privileged --net=net1 haproxy:1.8.8 


[root@localhost ~]# docker run -it -d -p 4001:8888 -p 4002:3306 -v /home/soft/haproxy:/usr/local/etc/haproxy --name haproxy1 --privileged --net=net1 haproxy:1.8.8
ff5a611c360f49f3ad3bfbf6b5e65706091eb69dc5a8564a497879a7b762e08c
[root@localhost ~]# 

# 创建容器完成后 进入容器 启动Haproxy中间件
# 输入:haproxy -f /usr/local/etc/haproxy/haproxy.cfg
[root@localhost ~]# docker exec -it haproxy1 bash
root@ff5a611c360f:/# haproxy -f /usr/local/etc/haproxy/haproxy.cfg
root@ff5a611c360f:/#  
# 然后退出容器 CTRL+P+Q

5.访问Haproxy监控界面

# 在浏览器访问 http://宿主机ip:4001/dbs , 输入haproxy.cfg中配置的账号密码
例:http://192.168.120.133:4001/dbs
# 输入账号密码 刚才在Haproxy中配置的账号密码
用户名:admin
密码:abc123456

登录后,就可以查看数据集群的运行情况

posted @ 2023-03-16 12:58  左右啊  阅读(122)  评论(0编辑  收藏  举报