環境
os:centos6.6
keepalived+haproxy
hapromaster 192.168.1.13
haproslave 192.168.1.12
vip 192.168.1.2
1、keepalived安裝
yum install keepalived*
2、配置haproxy
主
cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
wolf@qq.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.1.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
script "/root/ha.sh"
weight -20
interval 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 110
advert_int 1
track_script {
chk_haproxy
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.2
}
notify_master /root/ha_master.sh
}
從
! Configuration File for keepalived
global_defs {
notification_email {
yujinga@wolf.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server smtp.wolf.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.2
}
notify_master /root/ha_master.sh
}
相關腳本
[root@hapromaster ~]# cat ha.sh
#!/bin/bash
a=`ps -ef | grep haproxy| grep -v grep |wc -l`
if [ $a == 0 ]
then
/etc/init.d/keepalived stop
echo "haproxy is down,stoping keepalive..."
exit
fi
[root@hapromaster ~]# cat ha_master.sh
#!/bin/bash
sleep 3
/etc/init.d/haproxy reload && echo "haproxy is reload........"
3、安裝haproxy
yum install haproxy*
vi /etc/haproxy/haproxy.conf
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
log 127.0.0.1 local0 info #[err warning info debug]
maxconn 65535
daemon
nbproc 2
user nobody
group nobody
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
log global
mode http
option dontlognull
option httpclose
option abortonclose
retries 3
option redispatch
maxconn 65535
timeout connect 900000
timeout client 1800000
timeout server 1800000
option httplog
option forwardfor
stats enable
stats uri /admin?stats
stats auth admin:1qazxsw2
stats refresh 10s
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend web_wumart
mode http
maxconn 65535
bind :80
acl is_laolang hdr_beg(host) -i laolang.wolf.com
acl is_gateway hdr_beg(host) -i gateway.wolf.com
acl is_wolf hdr_beg(host) -i wolf.wolf.com
use_backend laolang_server if is_laolang
use_backend gateway_server if is_gateway
use_backend wolf_server if is_wolf
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend laolang_server
mode http
balance roundrobin
stats hide-version
option httpclose
option forwardfor
server web1 192.168.1.8:80 check inter 1500 rise 3 fall 3 weight 3
server web2 192.168.1.10:80 check inter 1500 rise 3 fall 3 weight 3
backend gateway_server
mode http
balance roundrobin
stats hide-version
option httpclose
option forwardfor
server web1 192.168.2.5:80 check inter 1500 rise 3 fall 3 weight 3
server web2 192.168.2.6:80 check inter 1500 rise 3 fall 3 weight 3
backend wolf_server
mode http
balance roundrobin
stats hide-version
option httpclose
option forwardfor
server web1 192.168.1.9:80 check inter 1500 rise 3 fall 3 weight 3
server web2 192.168.1.11:80 check inter 1500 rise 3 fall 3 weight 3
4、啟動停止
service keepalived start
service keepalived restart
service keepalived stop
service haproxy start
service haproxy stop
service haproxy restart
5、測試切換
停止主機haproxy
ip查看地址
6、監控
http://192.168.1.12/admin?stats
http://192.168.1.13/admin?stats