使用HAProxy实现MySQL多slave的读负载均衡
HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。而mysql一主多从是比较通用的架构,我们可以利用haproxy在tcp层对数据库的读请求进行代理,从而实现多个从库读的负载均衡。
目前公司的业务都是一主多从,想要把查询分到多个从库上,都是通过开发维护多个从库的配置来实现,这样比较麻烦,通过haproxy开发只需要一个配置项,开发不用理会后端有多少从库,而且可以把多个从库都利用起来,同时后台db有故障haproxy可以自动摘除,不需要像原先那样需要开发改数据库配置。
环境说明:
haproxy: 10.32.1.64
salve1: 10.32.1.67
slave2: 10.32.1.73
一,安装haproxy
大部分的linux系统都自带有haproxy安装包,我们以centos 6系统为例,直接安装系统自己的包。
1
2
3
|
# yum install -y haproxy # haproxy -v HA-Proxy version 1.5.4 2014 /09/02
|
二,准备haproxy配置文件
1
2
|
cd /etc/haproxy
vim 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
|
global log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy .pid
maxconn 4000
user haproxy
group haproxy
daemon
defaults mode tcp
log global
option httplog
option dontlognull
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
## 定义一个监控页面,监听在1080端口,并启用了验证机制 listen stats mode http
bind 0.0.0.0:1080
stats enable
stats hide-version
stats uri /haproxyadmin ?stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats admin if TRUE
frontend mysql bind *:3306
mode tcp
#log global
option tcplog
default_backend mysqlservers
backend mysqlservers balance leastconn
server dbsrv1 10.32.1.73:3306 check port 3306 rise 1 fall 2 maxconn 300
server dbsrv2 10.32.1.67:3306 check port 3306 rise 1 fall 2 maxconn 300
|
可以通过下面的命令检测配置文件是否正确:
1
|
# haproxy -f /etc/haproxy/haproxy.cfg -c |
出现Configuration file is valid表示没问题,如果配置文件有误的话检测的时候会有提示,根据提示进行修改就可以了。
添加haproxy系统帐号:
# useradd haproxy
三:开启haproxy日志
为haproxy设置单独的日志目录
# mkdir /var/log/haproxy
# chown haproxy:haproxy /var/log/haproxy
修改rsyslog.cfg文件
# vim /etc/rsyslog.cnf
$ModLoad imudp
$UDPServerRun 514
将这两行前的#去掉。
在local7.* /var/log/boot.log之后添加如下内容
# Save haproxy log
local2.* /var/log/haproxy/haproxy.log
修改rsylog文件
# vim /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS="" 改为 SYSLOGD_OPTIONS="-r -m 2 -c 2"
重启日志服务让配置生效
#/etc/init.d/rsyslog restart
四,启动haproxy
1
2
3
|
# /etc/init.d/haproxy start # ps -ef | grep haproxy haproxy 12648 1 0 02:34 ? 00:00:04 /usr/sbin/haproxy -D -f /etc/haproxy/haproxy .cfg -p /var/run/haproxy .pid
|
1
2
3
4
|
# netstat -tunlp | grep 12648 tcp 0 0 0.0.0.0:1080 0.0.0.0:* LISTEN 12648 /haproxy tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 12648 /haproxy udp 0 0 0.0.0.0:60182 0.0.0.0:* 12648 /haproxy
|
五,测试负载均衡
在slave1和slave2上分别给haproxy的ip授权:
1
2
|
> flush privileges; |
为了方便测试,在slave1的test库下面建一个t1表,要slave2的test库下面建test1表。在test库下执行show tables命令结果不一样则表示负载均衡有效。
salve1: 10.32.1.67
1
2
3
4
5
6
7
8
|
>use test ;
Database changed >show tables; +----------------+ | Tables_in_test | +----------------+ | t1 | +----------------+ |
slave2: 10.32.1.73
1
2
3
4
5
6
7
8
|
>use test ;
Database changed >show tables; +----------------+ | Tables_in_test | +----------------+ | test1 | +----------------+ |
测试结果:
多次查询返回的结果是t1和test1交替出现。说明查询分别分发到了slave1和slave2上,从而实现了负载均衡。
六,访问haproxy监控页面
因为在配置文件中配置了监控页面,我们可以通过http://10.32.1.64:1080/haproxyadmin?stats来查看后端db的状态。
七,查看日志
1
2
3
4
5
6
7
8
9
10
11
|
# tail -f /var/log/haproxy/haproxy.log
Feb 17 17:06:58 localhost haproxy[12648]: 10.32.1.64:50626 [17 /Feb/2017 :17:06:58.959] mysql mysqlservers /dbsrv1 1 /0/30 395 -- 0 /0/0/0/0 0 /0
Feb 17 17:07:00 localhost haproxy[12648]: 10.32.1.64:50629 [17 /Feb/2017 :17:07:00.050] mysql mysqlservers /dbsrv2 1 /0/5 392 -- 0 /0/0/0/0 0 /0
Feb 17 17:07:01 localhost haproxy[12648]: 10.32.1.64:50632 [17 /Feb/2017 :17:07:01.252] mysql mysqlservers /dbsrv1 1 /0/12 395 -- 0 /0/0/0/0 0 /0
Feb 17 17:07:02 localhost haproxy[12648]: 10.32.1.64:50635 [17 /Feb/2017 :17:07:02.037] mysql mysqlservers /dbsrv2 1 /0/9 392 -- 0 /0/0/0/0 0 /0
Feb 17 18:41:17 localhost haproxy[12648]: 10.32.1.227:8398 [17 /Feb/2017 :18:41:17.489] stats stats/<STATS> 3 /0/0/0/4 200 17398 - - LR-- 3 /3/0/0/0 0 /0 "GET /haproxyadmin?stats HTTP/1.1"
Feb 17 18:41:17 localhost haproxy[12648]: 10.32.1.227:8398 [17 /Feb/2017 :18:41:17.494] stats stats/<NOSRV> 89 /-1/-1/-1/89 503 212 - - LR-- 2 /2/0/0/0 0 /0 "GET /favicon.ico HTTP/1.1"
Feb 17 18:41:19 localhost haproxy[12648]: 10.32.1.227:8399 [17 /Feb/2017 :18:41:17.489] stats stats/<STATS> 1568 /0/0/0/1569 200 17399 - - LR-- 3 /3/0/0/0 0 /0 "GET /haproxyadmin?stats HTTP/1.1"
Feb 17 18:41:19 localhost haproxy[12648]: 10.32.1.227:8399 [17 /Feb/2017 :18:41:19.058] stats stats/<NOSRV> 68 /-1/-1/-1/68 503 212 - - LR-- 2 /2/0/0/0 0 /0 "GET /favicon.ico HTTP/1.1"
Feb 17 18:41:19 localhost haproxy[12648]: 10.32.1.227:8400 [17 /Feb/2017 :18:41:17.489] stats stats/<STATS> 2331 /0/0/1/2333 200 17402 - - LR-- 3 /3/0/0/0 0 /0 "GET /haproxyadmin?stats HTTP/1.1"
Feb 17 18:41:19 localhost haproxy[12648]: 10.32.1.227:8400 [17 /Feb/2017 :18:41:19.823] stats stats/<NOSRV> 60 /-1/-1/-1/60 503 212 - - LR-- 2 /2/0/0/0 0 /0 "GET /favicon.ico HTTP/1.1"
|
可以看到每一次请求都有记录在日志文件中。
八,failover测试
把slave1的mysql服务停掉模拟故障,我们先来看日志:
1
|
Server mysqlservers /dbsrv2 is DOWN, reason: Layer4 connection problem, info: "Connection refused" , check duration: 4ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
|
haproxy已经检测到了slave1不可用,现在我们再来通过haproxy转发查询,可以看到haproxy把所有的请求都转发到slave2了,监控界面也已经将slave标记为down了。
在将slave1的mysql服务启起来之后,slave1双可以正常提供服务了:
注意:
1,如果日志里面出现proxy proxy-mysql has no server available!这个的报错时,可以先看一下后端的mysql服务是否正常,如果正常的话有可能是防火墙的规则有问题或者selinux没有关闭导致的。
2,生产环境如果出现从库宕机要判断主从复制是否有误,复制延迟大不大,确认没问题再提供服务,不然要将有问题的从库从配置文件中摘除。
3,上面配置文件haproxy对后端mysql的检测只是检测端口是否在,也可以通过option mysql-check user haproxy来检测mysql是否可以正常连接,因为有遇到过mysqld进程还在,但是连不了的情况。此时需要对检测用户haproxy进行授权:
USE mysql;
INSERT INTO user (Host,User) values ('10.32.1.64>','haproxy');
FLUSH PRIVILEGES;
参考:
http://www.ttlsa.com/linux/haproxy-study-tutorial/
本文转自 emma_cql 51CTO博客,原文链接:http://blog.51cto.com/chenql/1898713