前言* 随着负载均衡器的成熟,人们熟知的软件负载均衡如LVS、HAProxy,各方面性能不亚于硬件负载均衡,HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。
HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。
我们平时用的负载均衡LVS是基于四层,新型的大型互联网公司也在采用HAProxy,了解了haproxy大并发、七层应用等特点,那今天我们来研究一下HAProxy+Keepalived基于七层负载高可用负载均衡的配置。
一、系统环境:
- 系统版本:CentOS6.0 x86_64
- HAProxy版本:1.4.21
- Keepalived版本:1.2.1
- Nginx版本:1.2.2
- MASTER_IP:192.168.0.130
- BACKUP_IP:192.168.0.131
- VIP:192.168.0.133
- WEB_1:192.168.0.134
- WEB_2:192.168.0.135
二、HAProxy安装:
1)首先安装192.168.0.130上安装:
下载最新稳定版本:
- cd /usr/src ;wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.21.tar.gz
- tar xzf haproxy-1.4.21.tar.gz &&cd haproxy-1.4.21 &&make TARGET=linux26 PREFIX=/usr/local/haproxy &&make install PREFIX=/usr/local/haproxy
- 安装完毕,在/usr/local/haproxy/目录下会看到三个目录:doc、sbin、share
2) 接下来配置HAProxy。
cd /usr/local/haproxy ;mkdir -p etc/
然后在/usr/local/haproxy/etc/下新建haproxy.cfg文件,这里拷贝haproxy配置文件:
- global
- log 127.0.0.1 local0
- maxconn 65535
- chroot /usr/local/haproxy
- uid 99
- gid 99
- daemon
- nbproc 8
- pidfile /usr/local/haproxy/haproxy.pid
- defaults
- log 127.0.0.1 local3
- mode http
- option httplog
- option httpclose
- option dontlognull
- option forwardfor
- option redispatch
- retries 2
- maxconn 65535
- balance source
- stats uri /web-status
- contimeout 5000
- clitimeout 50000
- srvtimeout 50000
- listen chinaapp.sinaapp.com
- bind *:80
- mode http
- option httplog
- log global
- option httpchk HEAD /index.html HTTP/1.0
- server web1 192.168.0.134:80 weight 5 check inter 2000 rise 2 fall 3
- server web2 192.168.0.135:80 weight 5 check inter 2000 rise 2 fall 3
然后启动haproxy,如下执行启动命令:
- /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
- [WARNING] 217/202150 (2857) : Proxy 'chinaapp.sinaapp.com': in multi-process mode, stats will be limited to process assigned to the current request.
- 会提示如上信息,nbproc进程如果设置为1则不会提示,如果想去掉这个提示可以修改编译文件即可。 在源码配置src/cfgparse.c找到如下行
- if (nbproc > 1) { if (curproxy->uri_auth) { - Warning("Proxy '%s': in multi-process mode, stats will be limited to process assigned to the current request.\n",
调整nbproc > 1数值即可。
+ Warning("Proxy '%s': in multi-process mode, stats will be limited to the process assigned to the current request.\n",
三、安装Keepalived:
- cd /usr/src ;
- wget http://www.keepalived.org/software/keepalived-1.2.1.tar.gz
- tar xzf keepalived-1.2.1.tar.gz
- cd keepalived-1.2.1 &&
- ./configure –with-kernel-dir=/usr/src/kernels/2.6.32-71.el6.x86_64/ &&make &&make install
- DIR=/usr/local/ ;cp $DIR/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ && cp $DIR/etc/sysconfig/keepalived /etc/sysconfig/ && mkdir -p /etc/keepalived && cp $DIR/sbin/keepalived /usr/sbin/
四、配置keepalived:
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- wgkgood@139.com
- }
- notification_email_from wgkgood@139.com
- smtp_server 127.0.0.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- }
- vrrp_script chk_haproxy {
- script "/data/sh/check_haproxy.sh"
- interval 2
- weight 2
- }
- # VIP1
- vrrp_instance VI_1 {
- state BACKUP
- interface eth0
- lvs_sync_daemon_inteface eth0
- virtual_router_id 151
- priority 100
- advert_int 5
- nopreempt
- authentication {
- auth_typePASS
- auth_pass 2222
- }
- virtual_ipaddress {
- 192.168.0.133
- }
- track_script {
- chk_haproxy
- }
- }
五、创建haproxy脚本:
设置可执行权限chmod +x check_haproxy.sh,内容如下:
- #!/bin/bash
- #auto check haprox process
- #2012-8-12 wugk
- killall -0 haproxy
- if
- [[ $? -ne 0 ]];then
- /etc/init.d/keepalived stop
- fi
BACKUP同样如上配置haproxy、keepalived,配置完修改优先级为90即可,然后启动。
六、测试haproxy+keepalived:
如图当手动kill 掉131的haproxy进程后,130的keepalived后台日志显示如下,并且访问VIP 192.168.0.133 正常访问:
访问haproxy status如下图:
本文参考煮酒兄http://andrewyu.blog.51cto.com/1604432/669491 HAProxy+Keepalived高可用负载均衡配置