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
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 的網頁服務仍然正常運作
本文轉自潘闊 51CTO部落格,原文連結:http://blog.51cto.com/pankuo/1389324,如需轉載請自行聯系原作者