Keepalived 监听服务切换与运维指南

发布于:2025-07-21 ⋅ 阅读:(17) ⋅ 点赞:(0)

Keepalived 监听服务切换与运维指南

一、监听服务切换方式

Keepalived 提供多种健康检查机制实现服务状态监测与自动切换,以下是常用方式及配置示例:

1.1 HTTP_GET/SSL_GET

基于 HTTP/HTTPS 协议的应用层检查,支持状态码验证和内容摘要校验。

状态码检测配置
real_server 192.168.1.100 80 {
    weight 1
    HTTP_GET {
        url {
            path /health.html
            status_code 200  # 期望返回状态码
        }
        connect_timeout 3    # 连接超时时间
        nb_get_retry 3       # 重试次数
        delay_before_retry 2 # 重试间隔
    }
}
内容摘要检测配置

使用 genhash 工具生成页面摘要:

genhash -s 192.168.1.100 -p 80 -u /index.html
# 输出示例:MD5SUM = b7bd8391367e4cf9e4e85263ce313ae8

配置文件引用:

real_server 192.168.1.100 80 {
    weight 1
    HTTP_GET {
        url {
            path /index.html
            digest b7bd8391367e4cf9e4e85263ce313ae8  # 生成的摘要值
        }
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 2
    }
}

1.2 TCP_CHECK

基于 TCP 协议的端口存活检查,适用于非 HTTP 服务(如数据库、SSH)。

real_server 192.168.1.101 3306 {
    weight 2
    inhibit_on_failure  # 故障时权重设为0
    TCP_CHECK {
        connect_port 3306      # 检查端口
        connect_timeout 5      # 超时时间
        nb_get_retry 3         # 重试次数
        delay_before_retry 3   # 重试间隔
    }
}

1.3 MISC_CHECK

自定义脚本检查,支持复杂业务逻辑判断(如多端口联动、日志关键字检测)。

脚本示例(HTTP服务检查)
#!/bin/bash
# /etc/keepalived/check_http.sh
CHECK_URL=$1
if curl -I -s "$CHECK_URL" | grep -q "200 OK"; then
    exit 0  # 成功
else
    exit 1  # 失败
fi
配置引用
real_server 192.168.1.102 80 {
    weight 1
    MISC_CHECK {
        misc_path "/etc/keepalived/check_http.sh http://192.168.1.102/index.html"
        misc_timeout 5  # 脚本超时时间
        # misc_dynamic  # 动态调整权重(可选)
    }
}

1.4 检查方式对比

方式 优点 缺点 适用场景
HTTP_GET 支持应用层健康度检测 配置复杂 Web服务
TCP_CHECK 轻量、通用 无法检测应用内部状态 数据库、SSH等TCP服务
MISC_CHECK 支持任意复杂逻辑 需要维护脚本 自定义业务逻辑

二、监听脚本示例

2.1 HTTP/HTTPS服务检查

#!/bin/bash
# 检查URL返回状态码
check_http() {
    local url=$1
    if curl -s -o /dev/null -w "%{http_code}" "$url" | grep -q "200"; then
        exit 0
    else
        exit 1
    fi
}
check_http "http://192.168.1.100/health"

2.2 TCP端口检查

#!/bin/bash
# 检查TCP端口连通性
check_tcp() {
    local host=$1
    local port=$2
    if nc -z -w 2 "$host" "$port"; then
        exit 0
    else
        exit 1
    fi
}
check_tcp "192.168.1.101" 3306

2.3 UDP端口检查

#!/bin/bash
# 检查UDP端口(需安装nc)
check_udp() {
    local host=$1
    local port=$2
    if nc -u -z -w 1 "$host" "$port" 2>/dev/null | grep -q "open"; then
        exit 0
    else
        exit 1
    fi
}
check_udp "192.168.1.102" 53

2.4 进程状态检查

#!/bin/bash
# 检查进程是否存活
check_process() {
    local proc_name=$1
    if pgrep -x "$proc_name" >/dev/null; then
        exit 0
    else
        exit 1
    fi
}
check_process "nginx"

2.5 状态切换通知脚本

#!/bin/bash
# /etc/keepalived/notify.sh
contact="admin@example.com"
notify() {
    local subject="$(hostname) 切换为 $1 状态"
    local body="$(date +'%F %T'): VIP 漂移,$(hostname) 成为 $1 节点"
    echo "$body" | mail -s "$subject" "$contact"
}

case $1 in
    master) notify master ;;
    backup) notify backup ;;
    fault) notify fault ;;
    *) echo "Usage: $0 {master|backup|fault}" ;;
esac

配置引用

vrrp_instance VI_1 {
    # ... 其他配置 ...
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}

三、常用运维命令

3.1 服务管理

# 启动
systemctl start keepalived
# 停止
systemctl stop keepalived
# 重启
systemctl restart keepalived
# 开机自启
systemctl enable keepalived

3.2 配置检查

# 验证配置文件语法
keepalived -t -f /etc/keepalived/keepalived.conf
# 指定配置文件启动
keepalived -f /etc/keepalived/custom.conf

3.3 状态查看

# 查看服务状态
systemctl status keepalived
# 查看VRRP实例状态
ip addr show | grep "inet.*secondary"  # 查看VIP绑定
# 查看LVS规则
ipvsadm -Ln
# 查看日志
journalctl -u keepalived -f

3.4 故障排查

# 抓包分析VRRP报文
tcpdump -i eth0 vrrp -nn -vv
# 查看健康检查日志
grep "healthcheck" /var/log/keepalived.log
# 手动触发故障转移
systemctl stop keepalived  # 在主节点执行

四、注意事项

  1. 脚本权限:自定义脚本需设置执行权限 chmod +x /path/to/script.sh
  2. 防火墙配置:允许VRRP协议(多播地址224.0.0.18,协议号112)
  3. 非抢占模式:配置 nopreempt 避免主节点恢复后抢占VIP
  4. 日志配置:在 /etc/sysconfig/keepalived 中设置 KEEPALIVED_OPTIONS="-D -S 6" 输出详细日志
  5. SELinux:临时关闭 setenforce 0,或配置策略允许脚本执行

网站公告

今日签到

点亮在社区的每一天
去签到