keepalived主动切换vip,邮件通知

本文介绍如何利用VRRP与Keepalived实现虚拟IP的主动切换及状态通知功能。通过设置脚本检查节点状态并调整优先级来控制VIP在不同节点间的切换,并在状态变化时发送通知。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.主动切换vip

通过vrrp_script判断,调节权重进行切换

! Configuration File for keepalived  

global_defs {  
   notification_email {  
         root@localhost
   }  
   notification_email_from kanotify@muuzz.com
   smtp_connect_timeout 3  
   smtp_server 127.0.0.1  
   router_id LVS_DEVEL  
}  


vrrp_script chk_mantaince_down {
   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"  #[ -f /etc/keepalived/down ]检查是否有down这个文件,如果真返回1,1代表失败,如果假返回0,0代表成功
   # commadn1 && command2  只有在 && 左边的命令返回真(命令返回值 $? == 0),&& 右边的命令才会被执行
   # commadn1 || command2  只有在 || 左边的命令返回假(命令返回值 $? == 1),|| 右边的命令才会被执行。
   interval 1
   weight -2
   #只要上面exit 1,weight就-2,下面的101 -2 = 99 比BACKUP的100少了,优先级低了,就变成备用状态了,再次检测如果down没了,权重又成101了,就又变成MASTER了。这样就可以手动干预vip在节点间切换
}

vrrp_instance VI_1 {  
    interface eth0  
    state MASTER  # BACKUP for slave routers
    priority 101  # 100 for BACKUP
    virtual_router_id 51 
    garp_master_delay 1 

    authentication {  
        auth_type PASS  
        auth_pass password  
    }  
    track_interface {  
       eth0    
    }  
    virtual_ipaddress {  
        192.168.255.100/24
    }  
    track_script {  
        chk_mantaince_down
    }  
 }
2.在状态切换时进行通知

notify scripts and alerts are optional
filenames of scripts to run on transitions
can be unquoted (if just filename)
or quoted (if has parameters)
to MASTER transition
notify_master /path/to_master.sh
to BACKUP transition
notify_backup /path/to_backup.sh
FAULT transition
notify_fault “/path/fault.sh VG_1”

1.使用:

notify_master
notify_backup
notify_fault
可以使用在vrrp_sync_group和vrrp_instance中,较多使用在vrrp_instance中
如果脚本带有参数,需要用”“扩起:notify_fault “/path/fault.sh VG_1”
当发生状态切换时,会根据切换的状态分配调用MASTER BACKUP FAULT指定的脚本

2.使用:

notify进行通知,notify可以使用在vrrp_sync_group和vrrp_instance中
此时脚本需要接受三个参数:
$1 指明组 vrrp_sync_group 或 实例 vrrp_instance
$2 组或instance名
$3 “MASTER”|”BACKUP”|”FAULT”

for ANY state transition.
“notify” script is called AFTER the
notify_* script(s) and is executed
with 3 arguments provided by keepalived
(ie don’t include parameters in the notify line).
arguments
$1 = “GROUP”|”INSTANCE”
$2 = name of group or instance
$3 = target state of transition
(“MASTER”|”BACKUP”|”FAULT”)
notify /path/notify.sh

还是notify_master, notify_backup, notify_fault 这种好用

下面是一个notify.sh脚本的简单示例:

#!/bin/bash
# Author: MageEdu <linuxedu@foxmail.com>
# description: An example of notify script
# 

vip=172.16.100.1
contact='root@localhost'

notify() {
    mailsubject="`hostname` to be $1: $vip floating"
    mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"
    echo $mailbody | mail -s "$mailsubject" $contact
}

case "$1" in
    master)
        notify master
        /etc/rc.d/init.d/haproxy start
        exit 0
    ;;
    backup)
        notify backup
        /etc/rc.d/init.d/haproxy stop
        exit 0
    ;;
    fault)
        notify fault
        /etc/rc.d/init.d/haproxy stop
        exit 0
    ;;
    *)
        echo 'Usage: `basename $0` {master|backup|fault}'
        exit 1
    ;;
esac

MASTER:
!/bin/bash
vip=192.168.255.100
concat=’root@localhost’
thisis=`ifconfig eno16777736|awk ‘/inet /{print $2}’`

notify() {
mailbody=”vrrp transaction,$vip floated to $thisip”
subject=”$thisip become $vip master”
echo $mailbody | mail -s $subject $concat
}

notify

于是配置文件变为

! Configuration File for keepalived  

global_defs {  
   notification_email {  
         root@localhost
   }  
   notification_email_from kanotify@muuzz.com
   smtp_connect_timeout 3  
   smtp_server 127.0.0.1  
   router_id LVS_DEVEL  
}  


vrrp_script chk_mantaince_down {
   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"  #[ -f /etc/keepalived/down ]检查是否有down这个文件,如果真返回1,1代表失败,如果假返回0,0代表成功
   # commadn1 && command2  只有在 && 左边的命令返回真(命令返回值 $? == 0),&& 右边的命令才会被执行
   # commadn1 || command2  只有在 || 左边的命令返回假(命令返回值 $? == 1),|| 右边的命令才会被执行。
   interval 1
   weight -2
   #只要上面exit 1,weight就-2,下面的101 -2 = 99 比BACKUP的100少了,优先级低了,就变成备用状态了,再次检测如果down没了,权重又成101了,就又变成MASTER了。这样就可以手动干预vip在节点间切换
}

vrrp_instance VI_1 {  
    interface eth0  
    state MASTER  # BACKUP for slave routers
    priority 101  # 100 for BACKUP
    virtual_router_id 51 
    garp_master_delay 1 

    authentication {  
        auth_type PASS  
        auth_pass password  
    }  
    track_interface {  
       eth0    
    }  
    virtual_ipaddress {  
        192.168.255.100/24
    }  
    track_script {  
        chk_mantaince_down
    }  
    notify_master "/etc/keepalived/notify.sh master"  
    notify_backup "/etc/keepalived/notify.sh backup"  
    notify_fault "/etc/keepalived/notify.sh fault" 
    #在脚本中配置邮件,或其他操作就可以了
 }
### Keepalived 配置邮件通知的实现方式 #### 1. 安装必要的工具 为了使 Keepalived邮件通知功能正常工作,需要先安装 `mail` 命令以及相关的邮件发送工具。可以通过以下命令完成安装: ```bash yum install -y mailx postfix ``` 确保 Postfix 或其他 SMTP 服务已正确配置并能够发送电子邮件。 --- #### 2. 编写邮件通知脚本 创建一个用于发送邮件通知脚本 `/etc/keepalived/mail_notify.sh` 并赋予其执行权限: ```bash vim /etc/keepalived/mail_notify.sh ``` 脚本内容如下: ```bash #!/bin/bash contact='youreamil@qq.com' # 替换为实际收件邮箱 hostname=$(hostname) case "$1" in master) subject="${hostname} 已切换为主节点" body="$(date '+%F %T'): ${hostname} 切换至主节点状态,请检查 Keepalived 运行情况。" ;; backup) subject="${hostname} 已切换为备节点" body="$(date '+%F %T'): ${hostname} 切换至备节点状态,请注意监控。" ;; fault) subject="${hostname} 出现故障" body="$(date '+%F %T'): ${hostname} 处于故障状态,请立即排查问题。" ;; esac echo "${body}" | mail -s "${subject}" ${contact} ``` 保存后,设置脚本的执行权限: ```bash chmod +x /etc/keepalived/mail_notify.sh ``` --- #### 3. 修改 Keepalived 配置文件 编辑 Keepalived 的配置文件 `/etc/keepalived/keepalived.conf`,添加邮件通知的相关参数和触发脚本路径: ```bash global_defs { router_id LVS_DEVEL notification_email { root@localhost # 收件人列表(可根据需求修改) } notification_email_from admin03@localhost # 发送方邮箱地址 smtp_server 127.0.0.1 # SMTP 服务器地址 smtp_connect_timeout 30 # SMTP 连接超时时间 } vrrp_instance VI_1 { state MASTER # 当前实例角色(MASTER/BACKUP) interface eth0 # 绑定网络接口 virtual_router_id 51 # 虚拟路由器 ID priority 100 # 优先级(备用机应低于此值) authentication { auth_type PASS # 认证类型 auth_pass 123456 # 密码 } virtual_ipaddress { 192.168.1.203 # 虚拟 IP 地址 } notify_master "/etc/keepalived/mail_notify.sh master" # 成为主节点时触发 notify_backup "/etc/keepalived/mail_notify.sh backup" # 变为备节点时触发 notify_fault "/etc/keepalived/mail_notify.sh fault" # 故障时触发 } ``` 以上配置中,通过 `notify_*` 参数指定了不同状态下触发的脚本及其参数[^2]。 --- #### 4. 测试邮件发送功能 在正式启用之前,建议测试邮件发送功能是否正常。可以手动运行以下命令模拟不同的场景: ```bash /etc/keepalived/mail_notify.sh master /etc/keepalived/mail_notify.sh backup /etc/keepalived/mail_notify.sh fault ``` 如果收到对应的测试邮件,则说明配置无误。 --- #### 5. 启动与重启 Keepalived 服务 最后,启动或重新加载 Keepalived 服务以应用新的配置: ```bash systemctl restart keepalived systemctl enable keepalived ``` 此时,当 Keepalived 的状态发生变化时,会自动触发相应的邮件通知脚本并向指定邮箱发送消息[^4]。 --- ### 注意事项 - 确保目标邮箱支持接收来自本地 SMTP 服务的邮件。 - 如果使用外部 SMTP 服务器(如 Gmail),需调整 `smtp_server` 和认证机制。 - 在生产环境中,推荐对日志进行定期审查以便及时发现潜在问题。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值