KA 配两台 MASTER,BACKUP节点  

 

安装Keepalived

1、下载安装依赖包

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

2、下载、安装Keepalived

wget https://www.keepalived.org/software/keepalived-2.0.18.tar.gz
tar zxvf keepalived-2.0.18
cd keepalived-2.0.18
 ./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 /keepalived-2.0.18/keepalived/etc/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
}

vrrp_script chk_haproxy {
    script "/etc/keepalived/chk.sh"                      //检查haproxy的脚本
    interval 2                                           //每两秒检查一次
}

vrrp_instance VI_1 {
    state MASTER                                        //定义为MASTER节点
    nopreempt                                           //开启不抢占
    interface eth0
    virtual_router_id 51
    priority 100                                        //开启了不抢占,所以此处优先级必须高于另一台
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass abcd
    }
    virtual_ipaddress {
        172.17.0.21                                 //配置VIP  当前DOCKER IP:172.17.0.20
    }
    track_script {
        chk_haproxy //调用检查脚本
    }
notify_backup "/etc/init.d/haproxy restart" //KA 启动Haproxy notify_fault "/etc/init.d/haproxy stop" }



5.chk.sh文件:

vi /etc/keepalived/chk.sh


#!/bin/bash
#
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then
       /etc/init.d/keepalived stop
fi


chmod +x /etc/keepalived/chk.sh


service keepalived start