keepalived主动切换vip,邮件通知

5 篇文章 0 订阅
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" 
    #在脚本中配置邮件,或其他操作就可以了
 }
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值