HAproxy+keepalived


server1,server2:


vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

vrrp_script check_haproxy {
script "/opt/check_haproxy.sh"
interval 2
weight 2
}



global_defs {
   notification_email {
        root@localhost
   }
   notification_email_from keepalived@server1.lalala.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER        #备机BAKEUP
    interface eth0    
    virtual_router_id 188
    priority 100        #备机50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       172.25.88.100
    }
        track_script {
        check_haproxy
        }
}


vim check_haproxy.sh

#!/bin/bash
/etc/init.d/haproxy status &> /dev/null || /etc/init.d/haproxy restart &> /dev/null
if [ $? -ne 0 ];then
/etc/init.d/keepalived stop &> /dev/null
fi

这个脚本的意思是haproxy 挂了或者启动不起来了,备机自动接管!!!


vim /etc/haproxy/haproxy.cfg


listen westos *:80
    balance     roundrobin
    server  web1 172.25.88.3:80 check
    server  web1 172.25.88.4:80 check

更多关于HAproxy配置,可以查看这个链接:


HAproxy安装,配置,重定向及读写分离


检验

server1     /etc/init.d/keepalived stop

server2 就接管拉~


HAproxy+keepalived_HAproxy+keepalived


HAproxy+keepalived_HAproxy+keepalived_02




网页访问不受影响....


HAproxy+keepalived_HAproxy+keepalived_03