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、./执行脚本的区别

相关推荐
淼淼爱喝水2 分钟前
openEuler 系统下 Ansible 一键安装教程(保姆级)
运维·ansible·openeuler
XXOOXRT5 分钟前
零基础掌握Linux常用命令
linux·运维·服务器
迷海24 分钟前
Linux g++编译与GDB调试完整流程(文末附图)
linux·gdb调试工具·g++编译器
softbangong26 分钟前
815-批量Excel文件合并工具,批量excel文件、工作表合并软件
linux·windows·excel·文件合并·excel合并·数据整理
123过去34 分钟前
responder使用教程
linux·网络·测试工具·安全·哈希算法
不知名。。。。。。。。39 分钟前
数据链路层
linux·网络
桌面运维家42 分钟前
KVM虚拟机:快照增量备份与Linux系统快速恢复
linux·运维·服务器
charlie1145141911 小时前
嵌入式C++教程实战之Linux下的单片机编程:从零搭建 STM32 开发工具链(2) —— HAL 库获取、启动文件坑位与目录搭建
linux·开发语言·c++·stm32·单片机·学习·嵌入式
Sarapines Programmer1 小时前
【Docker】Windows 安装 Docker 简明指南
运维·docker·容器
cnnews1 小时前
手机通过Termux安装unbuntu,开启SSH
linux·运维·ubuntu·ssh