配置Keepalived双主热备

规则:以一个虚拟ip分组归为同一个路由;VI_1为一组,VI_2为一组

1.1 主节点配置

global_defs {
   # 路由id:当前安装keepalived的节点主机标识符,保证全局唯一
   router_id keep_171
}

vrrp_instance VI_1 {
    # 表示状态是MASTER主机还是备用机BACKUP
    state MASTER
    # 该实例绑定的网卡
    # 查看网卡名称命令:ip addr
    interface ens33
    # 保证主备节点一致即可
    virtual_router_id 51
    # 权重,master权重一般高于backup,
    # 如果有多个,那就是选举,谁的权重高,谁就当选
    priority 100
    # 主备之间同步检查时间间隔,单位秒
    advert_int 1
    # 认证权限密码,防止非法节点进入
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    # 虚拟出来的ip,可以有多个(vip)
    virtual_ipaddress {
        192.168.1.161
    }
}

vrrp_instance VI_2 {
    # 备用机设置为BACKUP
    state BACKUP
    interface ens33
    virtual_router_id 52
    # 权重低于MASTER
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.162
    }
}

1.2 备用节点配置

global_defs {
   router_id keep_172
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        # 注意:主备两台的vip都是一样的,绑定到同一个vip
        192.168.1.161
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface ens33
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.162
    }
}

1.3 启动Keepalived

# 启动keepalived
systemctl start keepalived
# 停止keepalived
systemctl stop keepalived
# 重启keepalived
systemctl restart keepalived
# 开启启动keepalived
systemctl enable keepalived
# 取消开机启动keepalived
systemctl disable keepalived

Keepalived配置Nginx自动重启

2.1 增加Nginx重启检测脚本

vim /etc/keepalived/check_nginx_alive_or_not.sh
#!/bin/bash

A=`ps -C nginx --no-header |wc -l`
# 判断nginx是否宕机,如果宕机了,尝试重启
if [ $A -eq 0 ];then
    /usr/local/nginx/sbin/nginx
    # 等待一小会再次检查nginx,如果没有启动成功,
    # 则停止keepalived,使其启动备用机
    sleep 3
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        killall keepalived
    fi
fi
  • 增加运行权限
chmod +x /etc/keepalived/check_nginx_alive_or_not.sh

2.2 配置keepalived监听nginx脚本

vrrp_script check_nginx_alive {
    script "/etc/keepalived/check_nginx_alive_or_not.sh"
    interval 2 # 每隔两秒运行上一行脚本
    weight 10 # 如果脚本运行成功,则升级权重+10
    # weight -10 # 如果脚本运行失败,则升级权重-10
}

2.3 在vrrp_instance中新增监控的脚本

track_script {
    check_nginx_alive   # 追踪 nginx 脚本
}

2.4 重启Keepalived使得配置文件生效

systemctl restart keepalived

2.5 全配置如下

! Configuration File for keepalived

global_defs {
   router_id Keep_151
}

vrrp_script check_nginx_alive {
    script "/etc/keepalived/check_nginx_alive_or_not.sh"
    interval 2 # 每隔两秒运行上一行脚本
    weight 10 # 如果脚本运行成功,则升级权重+10
    # weight -10 # 如果脚本运行失败,则升级权重-10
}

vrrp_instance VI_1 {
    state MASTER
    interface enp0s3
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        check_nginx_alive   # 追踪 nginx 脚本
    }
    virtual_ipaddress {
        192.168.31.155
    }
}

vrrp_instance VI_2 {
    state BACKUP
    interface enp0s3
    virtual_router_id 52      
    priority 80 
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.31.156
    }
}

Q.E.D.

知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

那一年,我也变成了光!!