Keepalived如何解决脑裂问题

作者:灯下黑
链接:https://www.zhihu.com/question/50997425/answer/136178424

可以采用第三方仲裁的方法。由于keepalived体系中主备两台机器所处的状态与对方有关。如果主备机器之间的通信出了网题,就会发生脑裂,此时keepalived体系中会出现双主的情况,产生资源竞争。一般可以引入仲裁来解决这个问题,即每个节点必须判断自身的状态。最简单的一种操作方法是,在主备的keepalived的配置文件中增加check配置,服务器周期性地ping一下网关,如果ping不通则认为自身有问题 。最容易的是借助keepalived提供的vrrp_script及track_script实现。如下所示。

初始的配置: global_defs {
  router_id LVS_DEVEL
}

vrrp_sync_group VG_1 {
    group {
        VI_1
    }
    notify_master /root/slave2master.py
    notify_backup /root/master2slave.py
}

vrrp_instance VI_1 {
        vrrp_unicast_bind 192.168.150.21
        vrrp_unicast_peer 192.168.150.20
        state BACKUP
        interface manbr
        virtual_router_id 51
        nopreempt
        priority 100
        advert_int 10
        authentication {
            auth_type PASS
            auth_pass XXXX
        }
    virtual_ipaddress {
        192.168.150.23 dev manbr
    }
}增加仲裁的配置:global_defs {
    router_id LVS_DEVEL
}
track_script {
    check_local
}
vrrp_script check_local {
    script "/root/check_gateway.sh" 
    interval 5
}
vrrp_sync_group VG_1 {
    group {
        VI_1
    }
    notify_master /root/slave2master.py
    notify_backup /root/master2slave.py
}

vrrp_instance VI_1 {
        vrrp_unicast_bind 192.168.150.21
        vrrp_unicast_peer 192.168.150.20
        state BACKUP
        interface manbr
        virtual_router_id 51
        nopreempt
        priority 100
        advert_int 10
        authentication {
            auth_type PASS
            auth_pass XXXX
        }
    virtual_ipaddress {
        192.168.150.23 dev manbr
    }

}check_gateway.sh 就是我们的仲裁逻辑,发现ping不通网关,则关闭keepalived service keepalived stop。该方法在服务器通信正常之后无法再将keepalived启动起来4. 我认为更值得推荐的是自己写脚本,写一个while循环,每轮ping网关,累计连续失败的次数,当连续失败达到一定次数则运行service keepalived stop关闭keepalived服务。如果发现又能够ping通网关,再重启keepalived服务。最后在脚本开头再加上脚本是否已经运行的判断逻辑,将该脚本加到crontab里面。

你可能感兴趣的:(LB&&HA,linux,服务器)