Keepalived 配置文件核心逻辑
这个配置文件主要实现了以下功能:
- 定义全局通知、SMTP 等基础参数
- 创建一个名为
checkping的检测脚本,每 10 秒执行一次 - 配置一个 VRRP 实例 VI_1,使用 bond0 网卡,虚拟 IP 为 172.16.30.52,优先级 200,开启非抢占模式
- 绑定检测脚本,脚本检测失败时会将节点优先级降低 20
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 LVS_DEVEL
}
vrrp_script checkping
{
script "/app/shell/vrrp_strict_shell/checkping.sh"
interval 10
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface bond0
virtual_router_id 123
priority 200
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 5588111a
}
virtual_ipaddress {
172.16.30.52
}
track_script
{
checkping
}
}
将 Keepalived 配置文件保存到 /etc/keepalived/keepalived.conf,修改以下参数适配你的环境:
smtp_server:替换为你的 SMTP 服务器地址interface bond0:确认网卡名称正确(可通过ip addr查看)virtual_router_id 123:确保同一 VRRP 组内的节点该值相同priority 200:主节点优先级高于备节点(如备节点设为 190)
checkping.sh:
#!/bin/bash
# 检测目标IP 172.16.30.24 是否可达
# 成功返回 0(不调整优先级),失败返回 1(触发优先级降低)
activestat=$(/usr/bin/nmap -n -sn --max-retries 3 172.16.30.24 2>/dev/null | egrep -c "Host is up.*latency")
# 容错处理:如果变量为空,默认视为检测失败
if [[ "${activestat:-0}" -gt 0 ]]; then
# 检测成功,返回 0
exit 0
else
# 检测失败,返回 1(触发 weight -20 的优先级调整)
exit 1
fi
Categories:
系统运维