haproxy+keepalive

两个节点安装keepalived

yum install keepalived haproxy -y

修改keepalived配置文件

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_http_port {
    script "/var/mxscripts/check_haproxy.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state MASTER
    interface ens160
    virtual_router_id 51
    priority 120
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
    chk_http_port
}
    virtual_ipaddress {
    10.0.10.99
}
}

check_haproxy.sh脚本

if [ $(systemctl status haproxy|grep active|grep running|wc -l) -eq 0 ];then
    systemctl start haproxy
fi
sleep 2
if [ $(systemctl status haproxy|grep active|grep running|wc -l) -eq 0 ];then
    systemctl stop keepalived
fi

从节点master改成backup
优先级调第一点
其余一致

修改内核选项,

否则备份节点无VIP则haproxy绑定失败无法启动

net.ipv4.ip_nonlocal_bind = 1

两个节点启动服务

systemctl start keepalived
systemctl enable keepalived

你可能感兴趣的:(haproxy+keepalive)