系统版本: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安装:
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
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", + Warning("Proxy ‘%s‘: in multi-process mode, stats will be limited to the process assigned to the current request.\n",调整nbproc > 1数值即可。
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脚本:
#!/bin/bash #auto check haprox process #2012-8-12 wugk killall -0 haproxy if [[ $? -ne 0 ]];then /etc/init.d/keepalived stop fiBACKUP同样如上配置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高可用负载均衡配置
本文出自 “京峰教育-运维架构师培训” 博客,请务必保留此出处http://jfedu.blog.51cto.com/8172058/1686835
原文地址:http://jfedu.blog.51cto.com/8172058/1686835