# haproxy 作用
HAProxy 厉害的点,不需要写 mysql 的健康检测,软件本身可以检测出 mysql 服务停止,然后切换到正常的服务器上,而且当 mysql 服务器重启之后,HAProxy 不需要重启,也能识别并且切换上去。
# 基本环境
mycat1:172.18.12.5
mycat2:172.18.12.6
# haproxy.cfg 配置文件
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
| global daemon #nbproc 1 #pidfile /var/run/haproxy.pid #工作目录 chroot /usr/local/etc/haproxy
defaults log 127.0.0.1 local0 err #[err warning info debug] mode http #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK retries 3 #两次连接失败就认为是服务器不可用,也可以通过后面设置 option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器 option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接 option dontlognull #日志中不记录负载均衡的心跳检测记录 maxconn 4096 #默认的最大连接数 timeout connect 5000ms #连接超时 timeout client 30000ms #客户端超时 timeout server 30000ms #服务器超时 #timeout check 2000 #=心跳检测超时
######## 监控界面配置 ################# listen admin_status #监控界面访问信息 bind 0.0.0.0:8888 mode http #URI相对地址 stats uri /dbs #统计报告格式 stats realm Global\ statistics #登录账户信息 stats auth admin:123456 ########frontend配置##############
######## mysql负载均衡配置 ############### listen proxy-mysql bind 0.0.0.0:3306 mode tcp #负载均衡算法 #static-rr 权重, leastconn 最少连接, source 请求IP, 轮询 roundrobin balance roundrobin #日志格式 #option tcplog #在 mysql 创建一个没有权限的haproxy用户,密码为空。 haproxy用户 #create user 'haproxy'@'%' identified by ''; FLUSH PRIVILEGES; #option mysql-check user haproxy #这里是容器中的IP地址,由于配置的是轮询roundrobin,weight 权重其实没有生效 server MYSQL_1 172.18.12.5:8066 check weight 1 maxconn 2000 server MYSQL_2 172.18.12.6:8066 check weight 1 maxconn 2000 #server MYSQL_3 192.168.130.102:3306 check weight 1 maxconn 2000 #使用keepalive检测死链 #option tcpka #########################################
|
# web 界面访问 haproxy 监控界面
配置:
1 2 3 4 5 6 7 8
| bind 0.0.0.0:8888 mode http #URI相对地址 stats uri /dbs #统计报告格式 stats realm Global\ statistics #登录账户信息 stats auth admin:123456
|
启动 haproxy 后,可通过 http://ip:8888/dbs 查看 mycat 状态
效果图如下
还需要通过keepalived保证haproxy的高可用,keepalived搭建失败