keepalived踩坑记录

环境

操作系统: CentOS7.9

keepalived: 1.35

master配置

bash 复制代码
cat > /etc/keepalived/keepalived.conf<<'EOF'
global_defs {
    router_id Nginx1
}
vrrp_script chk_nginx {
    script "/etc/keepalived/check_nginx.sh"
    interval 3
    weight -3
}
vrrp_instance VI_1 {
    state MASTER
    interface enp0s8 
    virtual_router_id 101
    priority 120
    advert_int 2
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 12333
    }
    virtual_ipaddress {
        192.168.240.6
    }
    track_script {
      chk_nginx 
    }
}

EOF
bash 复制代码
cat > /etc/keepalived/check_nginx.sh<<'EOF'
B=$(pgrep nginx | wc -l)
if [[ $B -eq 0 ]]; then
    /usr/bin/systemctl stop keepalived
    exit 1
fi
exit 0

EOF

backup配置

bash 复制代码
cat > /etc/keepalived/keepalived.conf<<'EOF'
global_defs {
    router_id Nginx2
}
vrrp_script chk_nginx {
    script "/etc/keepalived/check_nginx.sh"
    interval 3
    weight -3
}
vrrp_instance VI_1 {
    state BACKUP
    interface enp0s8 
    virtual_router_id 101
    priority 119
    advert_int 2
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 12333
    }
    virtual_ipaddress {
        192.168.240.6
    }
    track_script {
      chk_nginx
    }
}

EOF
bash 复制代码
cat > /etc/keepalived/check_nginx.sh<<'EOF'
B=$(pgrep nginx | wc -l)
if [[ $B -eq 0 ]]; then
    /usr/bin/systemctl stop keepalived
    exit 1
fi
exit 0

EOF

说明

坑点

  1. 检测脚本/etc/keepalived/check_nginx.sh第一行一定不要写#!/bin/bash
  2. 检测脚本/etc/keepalived/check_nginx.sh的退出状态码不起作用,所以只能/usr/bin/systemctl stop keepalived

排错参考文档

【Linux】详解shell中source、sh、bash、./执行脚本的区别

相关推荐
丶Darling.几秒前
进程间通信博客总结目录
linux·网络编程·进程同步·系统编程·c/c++·阻塞和非阻塞
清水加冰18 分钟前
【Linux进程】进程间的通信
linux·进程
YRr YRr36 分钟前
详细指南:在Ubuntu 20.04上安装和配置Orbbec SDK及USB设备权限
linux·运维·ubuntu
托尼沙滩裤43 分钟前
【MAC】深入浅出 Homebrew 下 Nginx 的安装与配置指南
运维·nginx·macos
yuanbenshidiaos1 小时前
linux----文件访问(c语言)
linux·服务器·算法
ghostwritten1 小时前
Linux 下的 GPT 和 MBR 分区表详解
linux·运维·gpt
ElePower95271 小时前
linux常用命令(touch、cat、less、head、tail)
linux
初心_20241 小时前
10. 虚拟机VMware Workstation Pro下共享Ubuntu和Win11文件夹
linux·运维·服务器
时空无限2 小时前
raid 状态查看 storcli64
linux
大熊程序猿2 小时前
apisix docker 安装
运维·docker·容器