一、系统环境:
系统版本:CentOS5.5 x86_64
master_ip:172.20.27.40
backup_ip:172.20.27.50
vip:172.20.27.200
web_1: 172.20.27.90
web_2:172.20.27.100
二、haproxy安装:
1.首先172.20.27.40安装上安装:
1.1安装
tar zxvf haproxy-1.3.20.tar.gz
cd haproxy-1.3.20
make TARGET=linux26 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
1.2.配置
cd /usr/local/haproxy
mkdir etc
cd etc
vi haproxy.cfg
global
log 127.0.0.1 local0
maxconn 65535
chroot /usr/local/haproxy
uid 99
gid 99
daemon
nbproc 12
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
stats auth admin:[email protected]
stats refresh 60s
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen 172.20.27.200
bind *:80
mode http
option httplog
log global
option httpchk HEAD /t.htm HTTP/1.0
server web1 172.20.27.90:80 weight 5 check inter 2000 rise 2 fall 3
server web2 172.20.27.100:80 weight 5 check inter 2000 rise 2 fall 3
然后启动haproxy,如下执行启动命令:
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
2.首先172.20.27.50安装上安装:
2.1.安装
tar zxvf haproxy-1.3.20.tar.gz
cd haproxy-1.3.20
make TARGET=linux26 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
2.2.配置
cd /usr/local/haproxy
mkdir etc
cd etc
vi haproxy.cfg
global
log 127.0.0.1 local0
maxconn 65535
chroot /usr/local/haproxy
uid 99
gid 99
daemon
nbproc 12
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
stats auth admin:[email protected]
stats refresh 60s
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen 172.20.27.200
bind *:80
mode http
option httplog
log global
option httpchk HEAD /t.htm HTTP/1.0
server web1 172.20.27.90:80 weight 5 check inter 2000 rise 2 fall 3
server web2 172.20.27.100:80 weight 5 check inter 2000 rise 2 fall 3
然后启动haproxy,如下执行启动命令:
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
三、keepalived安装:
1.首先172.20.27.40安装上安装:
1.1安装
tar zxvf keepalived-1.1.17.tar.gz
cd keepalived-1.1.17
./configure
make
make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig
mkdir /etc/keepalived
cp /usr/local/sbin/keepalived /usr/sbin/
cd /etc/keepalived
1.2.配置
vi keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
script "/usr/local/haproxy/shell/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 {
172.20.27.200
}
track_script {
chk_haproxy
}
}
mkdir -p /usr/local/haproxy/shell
cd /usr/local/haproxy/shell
#建立监控haproxy脚本
vi check_haproxy.sh
#!/bin/bash
#auto check haprox process
#2014-6-30
killall -0 haproxy
if [[ $? -ne 0 ]];then
/etc/init.d/keepalived stop
fi
chmod u+x check_haproxy.sh
service keepalived restart
2.首先172.20.27.50安装上安装:
2.1安装
tar zxvf keepalived-1.1.17.tar.gz
cd keepalived-1.1.17
./configure
make
make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig
mkdir /etc/keepalived
cp /usr/local/sbin/keepalived /usr/sbin/
cd /etc/keepalived
2.2配置
vi keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
script "/usr/local/haproxy/shell/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 90
advert_int 5
nopreempt
authentication {
auth_typePASS
auth_pass 2222
}
virtual_ipaddress {
172.20.27.200
}
track_script {
chk_haproxy
}
}
mkdir -p /usr/local/haproxy/shell
cd /usr/local/haproxy/shell
#建立监控haproxy脚本
vi check_haproxy.sh
#!/bin/bash
#auto check haprox process
#2014-6-30
killall -0 haproxy
if [[ $? -ne 0 ]];then
/etc/init.d/keepalived stop
fi
chmod u+x check_haproxy.sh
service keepalived restart
四、测试
把vip 172.20.27.200绑定到你的域名上面进行测试
1.一直ping 172.20.27.200这个IP,然后把master_ip这台服务器模拟宕机状态。这时候ping会延迟3秒后,自动切换到backup_ip。(说明测试成功)