0%

keepalived+haproxy实现双机热备

安装haproxy

参考Haproxy安装使用

安装keepalived

yum install keepalived -y

修改keepalived配置文件

默认配置文件目录:/etc/keepalived/keepalived.conf

global_defs {
   router_id consul1    #虚拟路由名称
}
 
vrrp_script chk_haproxy {
    script "killall -0 haproxy"     #使用killall -0检查haproxy实例是否存在,性能高于ps命令
    interval 2    #脚本运行周期
    timeout 2    #每次检查的加权权重值
    fall 3
}
 
vrrp_instance haproxy {
    state MASTER     #本机实例状态,MASTER/BACKUP,备机配置文件中请写BACKUP
    interface ens33    #本机网卡名称,使用ifconfig命令查看
    virtual_router_id 51    #虚拟路由编号,主备机保持一致
    priority 100    #本机初始权重,备机请填写小于主机的值(例如100)
    advert_int 1        #争抢虚地址的周期,秒
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.41.150    #虚地址IP,主备机保持一致
    }
    track_script {
        chk_haproxy        #对应的健康检查配置
    }
}

启动keepalived服务

主备机配置完成后,均启动keepalived服务

systemctl  start keepalived.service

检查主备机网卡上是否有VIP

执行命令:ip addr sh ens33

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:e8:3f:ba brd ff:ff:ff:ff:ff:ff
    inet 192.168.41.129/24 brd 192.168.41.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.41.150/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::999f:e60:1951:9d4d/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::d5e3:a2fa:a42:31d3/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

测试

  • 停止haproxy服务,查看VIP是否转移
systemctl  stop haproxy.service
  • 停止keepalived服务,查看VIP是否转移
systemctl  stop keepalived.service