1. 安装Keepalived
安装使用yum即可,如果为离线环境,需要将Centos ISO作为一个私有局域网镜像,配置一下yum源即可。
yum install keepalived
2. 修改主节点Keepalived配置项
cd /etc/keepalived
vim keepalived.conf
! Configuration File for keepalived
global_defs {
# notification_email {
# acassen@firewall.loc
# failover@firewall.loc
# sysadmin@firewall.loc
# }
# notification_email_from Alexandre.Cassen@firewall.loc
# smtp_server 192.168.200.1
# smtp_connect_timeout 30
router_id lb001 # 全局唯一的routerid
script_user root
# vrrp_skip_check_adv_addr
# vrrp_strict # 不使用vrrp严格模式
# vrrp_garp_interval 0
# vrrp_gna_interval 0
}
# 定义检测kong 网关的脚本
vrrp_script chk_nginx {
script "/etc/keepalived/kong_check.sh" ## 检测 Kong 状态的脚本路径
interval 1 # 执行频率
weight 0 # shell脚本执行返回状态为0 成功,weight大于0,将修改优先级加weight,返回状态非零 weight 小于 0 ,优先级减去weight
}
vrrp_instance kong_gateway {
state MASTER # 主节点为MASTER
interface eth0 # 绑定 本地eth0 网卡
virtual_router_id 59 # 同一组MASTER,BACKUP此标识必须一样,局域网内有多组keepalived时候,注意此参数必须跟其他组不一样
priority 100 # 指定MASTER优先级,要比BACKUP节点大
advert_int 1 # MASTER 发送VRRP包周期
authentication {
auth_type PASS
auth_pass kong # 局域网多个组keepalive的时候,最好修改一下,不同组,密码不一样
}
track_script {
chk_nginx ## 执行 Nginx 监控的服务
}
virtual_ipaddress {
10.26.11.200 # 虚拟IP地址,需要同本机地址同网段
}
}
MASTER,BACKUP 能互通,并同网段
3. BACKUP节点配置
! Configuration File for keepalived
global_defs {
# notification_email {
# acassen@firewall.loc
# failover@firewall.loc
# sysadmin@firewall.loc
# }
# notification_email_from Alexandre.Cassen@firewall.loc
# smtp_server 192.168.200.1
# smtp_connect_timeout 30
router_id lb002 # 全局唯一的routerid
script_user root
# vrrp_skip_check_adv_addr
# vrrp_strict # 不使用vrrp严格模式
# vrrp_garp_interval 0
# vrrp_gna_interval 0
}
# 定义检测kong 网关的脚本
vrrp_script chk_nginx {
script "/etc/keepalived/kong_check.sh" ## 检测 Kong 状态的脚本路径
interval 1 # 执行频率
weight 0 # shell脚本执行返回状态为0 成功,weight大于0,将修改优先级加weight,返回状态非零 weight 小于 0 ,优先级减去weight
}
vrrp_instance kong_gateway {
state BACKUP # 主节点为BACKUP
interface eth0 # 绑定 本地eth0 网卡
virtual_router_id 59 # 同一组MASTER,BACKUP此标识必须一样,局域网内有多组keepalived时候,注意此参数必须跟其他组不一样
priority 90 # 指定MASTER优先级,要比BACKUP节点大
advert_int 1 # MASTER 发送VRRP包周期
authentication {
auth_type PASS
auth_pass kong # 局域网多个组keepalive的时候,最好修改一下,不同组,密码不一样
}
track_script {
chk_nginx ## 执行 Nginx 监控的服务
}
virtual_ipaddress {
10.26.11.200 # 虚拟IP地址,需要同本机地址同网段
}
}
默认配置为抢占模式,如果MASTER机器出现故障,又恢复后,MASTER优先级 比BACKUP大,会抢夺VIP地址绑定。如果不设置抢占模式需要将MASTER和BACKUP的节点的state属性都设置为backup。然后在vrrp_instance节点增加nopreempt属性。
4. 检测kong网关脚本
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
kong start
sleep 2
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
systemctl stop keepalived
fi
fi
5. 启动Keepalived
systemctl start keealived
6. 参考资料
https://www.keepalived.org/manpage.html
https://blog.csdn.net/mofiu/article/details/76644012
VIP设置为公网IP
网友评论