侧边栏壁纸
博主头像
平凡的运维之路博主等级

行动起来,活在当下

  • 累计撰写 49 篇文章
  • 累计创建 25 个标签
  • 累计收到 3 条评论

目 录CONTENT

文章目录

Keepalived平滑漂移vip地址

平凡的运维之路
2024-07-12 / 0 评论 / 2 点赞 / 41 阅读 / 5134 字

Keepalived平滑漂移vip地址

使用平滑切换,检查脚本进程进程是否存在,则对应return返回1或者0,1时,则keepalived为主节点是降级,切换vip地址,不停keepalived服务。

配置文件说明

  • 备1服务器Keepalived配置文件
! Configuration File for keepalived

global_defs {
   router_id nginx
   vrrp_cpu_affinity 1
   vrrp_priority -20
   checker_priority -20
   vrrp_rt_priority 99
   vrrp_garp_master_refresh 15
   vrrp_no_swap 
   checker_no swap
   vrrp_skip_check_adv_addre
}
vrrp_script chk_jincheng {
    script "/etc/keepalived/scripts/chk.sh"
    interval 5
    fall 2
    rise 1
    user appnginx  #直营chk.sh脚本使用appnginx用户执行
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 136
    nopreempt
    advert_int 1
    unicast_src_ip 
    unicast_peer { 192.168.127.8
        192.168.127.9
    }
    authentication {
        auth_type PASS
        auth_pass nginx_2024
    }
    virtual_ipaddress {
        192.168.127.10
    }
    track_script {
        chk_jincheng
    }
    notify_master /etc/keepalived/scripts/to_master.sh #当状态切换主时,执行脚本。
    notify_fault  /etc/keepalived/scripts/to_fault.sh #当状态为错误时,执行脚本。
}
  • 备2服务器Keepalived配置文件
! Configuration File for keepalived

global_defs {
   router_id nginx
   vrrp_cpu_affinity 1
   vrrp_priority -20
   checker_priority -20
   vrrp_rt_priority 99
   vrrp_garp_master_refresh 15
   vrrp_no_swap 
   checker_no swap
   vrrp_skip_check_adv_addre
}
vrrp_script chk_jincheng {
    script "/etc/keepalived/scripts/chk.sh"
    interval 5
    fall 2
    rise 1
    user appnginx  #直营chk.sh脚本使用appnginx用户执行
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 136
    nopreempt
    advert_int 1
    unicast_src_ip 
    unicast_peer { 192.168.127.9
        192.168.127.8
    }
    authentication {
        auth_type PASS
        auth_pass nginx_2024
    }
    virtual_ipaddress {
        192.168.127.10
    }
    track_script {
        chk_jincheng
    }
    notify_master /etc/keepalived/scripts/to_master.sh #当状态切换主时,执行脚本。
    notify_fault  /etc/keepalived/scripts/to_fault.sh #当状态为错误时,执行脚本。
}

检查脚本说明

  • chk.sh检测脚本
[root@web_nginx_1 scripts]# more chk.sh 
#!/bin/bash

#check nginx
pid_num=$(ps -ef|grep 'api_server|grep -v grep| wc -l)
if [ $pid_num -eq 0 ];then
    exit 1;
else
    exit 0;
fi
  • to_master.sh切换主节点,有服务,需要重启,才能监听vip地址。
#!/bin/bash
mv /opt/keepalived/etc/scripts/chk.sh  /opt/keepalived/etc/scripts/chk.sh_bak
cd /data/appapi/Platform
source /data/appapi/.bash_profile
./start.sh stop api
./start.sh start api
#exit
#EOF
mv /opt/keepalived/etc/scripts/chk.sh_bak /opt/keepalived/etc/scripts/chk.sh
2

评论区