Keepalived+haproxy
Client eth0:192.168.122.1/24
HAproxy master eth0192.168.122.50/24
HAproxy backup eth0192.168.122.60/24
HTML 1 eth0 192.168.122.10/24
HTML 2 eth0 192.168.122.20/24
HTML 1和2
# yum install httpd
分别创建测试页面index.html ,开启服务
HAproxy master 和backup
# tar xf haproxy-1.4.20.tar.gz
# cd haproxy-1.4.20
# make TARGET=linux26PREFIX=/usr/local/haproxy install
生成HAproxy配置文件
# cd /usr/local/haproxy/
# mkdir conf logs
# cd conf/
# vim haproxy.cfg
global
log127.0.0.1 local3 info
maxconn4096
uidnobody
gidnobody
daemon
nbproc1
defaults
log global
mode http
maxconn2048
retries 3
option redispatch
stats uri /haproxy
contimeout 5000
clitimeout 50000
srvtimeout 50000
frontendhttp-in
bind0.0.0.0:80
modehttp
logglobal
optionhttplog
optionhttpclose
aclhtml url_reg -i \.html$
use_backendhtml-server if html
default_backendhtml-server
backendhtml-server
modehttp
balanceroundrobin
optionhttpchk GET /index.html
cookieSERVERID insert indirect nocache
serverhtml-A 192.168.122.10:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
serverhtml-B 192.168.122.20:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5
# /usr/local/haproxy/sbin/haproxy -f/usr/local/haproxy/conf/haproxy.cfg
在HAproxymaster上安装配置Keepalived
# tar xf keepalived-1.2.1.tar.gz
# cd keepalived-1.2.1
# ./configure --prefix=/--mandir=/usr/local/share/man/
# make
# make install
# cd /etc/keepalived/
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs { #全局定义主要设置 keepalived 的通知机制和标识
notification_email{
root@localhost
}
notification_email_fromkeepalived@localhost
smtp_server127.0.0.1
smtp_connect_timeout30
router_idHAproxy1
}
vrrp_script chk_haproxy {
script"/etc/keepalived/chk_haproxy.sh"
interval2
}
vrrp_instance VI_1 { #VRRP(虚拟路由冗余协议)实例配置
stateMASTER #另一个 Director 标记为 BACKUP!!!
interfaceeth0 #实例绑定的网卡
virtual_router_id51 #VRID 虚拟路由标识
priority150 #优先级高为master,master 至少要高于backup 50 !!!
advert_int1 #检查间隔
authentication{
auth_typePASS
auth_pass1111
}
track_script{
chk_haproxy
}
virtual_ipaddress{
192.168.122.254/24
}
}
检测脚本
# vim /etc/keepalived/chk_haproxy.sh
#!/bin/bash
A=`ps -C haproxy --no-header | wc -l`
if [ $A -eq 0 ]
then /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
sleep3
if[ `ps -C haproxy --no-header | wc -l ` -eq0 ]
then /etc/init.d/keepalived stop
fi
fi
# scp keepalived.conf192.168.122.20:/etc/keepalived/
拷贝后,修改配置文件
stateBACKUP
priority100
# scp chk_haproxy.sh192.168.122.20:/etc/keepalived/
2台HAproxy上,启动Keepalived服务
# service keepalived start
测试用client 访问haproxy ip 刷新能分别看到html 1和2的网页然后把haproxy master 关机你会在HAproxy backup 日志里过一会看到脚本的执行tail -f /var/log/messages
然后继续用client 访问haproxy ip 仍然可以看到html 1和2 的网页服务仍然正常运行