当前位置: > Linux集群 > 负载均衡SLB >

CentOS6.4 配置HAProxy+Keepalived

时间:2015-12-20 16:03来源:linux.it.net.cn 作者:IT

安装HAProxy请参考

 

安装Keepalived

1、下载安装依赖包

yum install -y wget make gcc openssl-devel popt-devel

2、下载、安装Keepalived

wget http://www.keepalived.org/software/keepalived-1.2.8.tar.gz
tar zxvf keepalived-1.2.8.tar.gz
cd keepalived-1.2.8
./configure --prefix=/usr/local/keepalived
make
make install

3、安装成功后做成服务模式。

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

4、配置文件

mkdir -p /etc/keepalived/
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
chmod +x /etc/init.d/keepalived
vi /etc/keepalived/keepalived.conf
 
global_defs {
    router_id LVS_DEVEL
}
#监测haproxy进程状态,每2秒执行一次
vrrp_script chk_haproxy {
    script "/etc/keepalived/chk_haproxy.sh"
    interval 2
    weight 2
}
vrrp_instance VI_1 {
    state MASTER          #从服务器为BACKUP
    interface eth0
    virtual_router_id 51
    priority 100             #从服务器要低于100
    advert_int 1
    mcast_src_ip 10.10.3.171    #MASTER服务器IP,从服务器写从服务器的IP
    authentication {
        auth_type PASS
        auth_pass 111111
    }
    track_script {
        chk_haproxy    #监测haproxy进程状态
    }
    virtual_ipaddress {
        10.10.3.170    #虚拟IP
    }
}
 

chk_haproxy.sh内容

vi /etc/keepalived/chk_haproxy.sh
 
status=$(ps aux|grep haproxy | grep -v grep | grep -v bash | wc -l)
if [ "${status}" = "0" ]; then
    /etc/init.d/haproxy start

    status2=$(ps aux|grep haproxy | grep -v grep | grep -v bash |wc -l)

    if [ "${status2}" = "0"  ]; then
            /etc/init.d/keepalived stop
    fi
fi
 

启动和关闭服务

service keepalived start
service keepalived stop

高可用测试

 
1、将Master上的keepalived停止

查看Master上的日志

tail -f /var/log/messages

Sep 23 01:24:34 181 Keepalived[3125]: Stopping Keepalived v1.2.8 (09/23,2013)
Sep 23 01:24:34 181 Keepalived_vrrp[3127]: VRRP_Instance(VI_1) sending 0 priority
Sep 23 01:24:34 181 Keepalived_vrrp[3127]: VRRP_Instance(VI_1) removing protocol VIPs.

发现Master释放了VIP

再查看Backup上的日志

Sep 23 01:20:26 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) Transition to MASTER STATE
Sep 23 01:20:27 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) Entering MASTER STATE
Sep 23 01:20:27 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) setting protocol VIPs.
Sep 23 01:20:27 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.10.3.180
Sep 23 01:20:27 182 Keepalived_healthcheckers[2886]: Netlink reflector reports IP 10.10.3.180 added

发现Backup绑定了VIP

2、将Master上的keepalived启动

查看Master上的日志

tail -f /var/log/messages

Sep 23 01:27:56 181 Keepalived_vrrp[3450]: VRRP_Instance(VI_1) Entering MASTER STATE
Sep 23 01:27:56 181 Keepalived_vrrp[3450]: VRRP_Instance(VI_1) setting protocol VIPs.
Sep 23 01:27:56 181 Keepalived_vrrp[3450]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.10.3.180
Sep 23 01:27:56 181 Keepalived_healthcheckers[3449]: Netlink reflector reports IP 10.10.3.180 added

发现Master绑定了VIP

再查看Backup上的日志

Sep 23 01:21:15 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) Received higher prio advert
Sep 23 01:21:15 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) Entering BACKUP STATE
Sep 23 01:21:15 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) removing protocol VIPs.
Sep 23 01:21:15 182 Keepalived_healthcheckers[2886]: Netlink reflector reports IP 10.10.3.180 removed

发现Backup释放了VIP
 
(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容