LInux 搭建keepalived邮件服务,自动发送keepalived状态

一、安装软件

[root@master keepalived]# yum install -y mailx
[root@master keepalived]# yum install -y keepalived

二、开启邮件smtf服务 

进入QQ邮箱(其他邮箱同理)打开SMTP/POP3服务

保存授权码!!!

 三、服务器中编辑邮箱配置文件

[root@master keepalived]# vim /etc/mail.rc
vim /etc/mail.rc
set bsdcompat
set from=mailaddr@qq.com	#邮箱地址(××××××××××@qq.com)
set smtp=smtp.qq.com	#smtp服务器
set smtp-auth-user=mailaddr@qq.com		#邮箱账号
set smtp-auth-password=jawyphpthfkycbeg		#授权密码,注意是授权密码,不是在web页面上登邮箱的密码,授权密码可以在邮箱的pop3/smtp设置页面自行设置。
set smtp-auth=login
set ssl-verify=ignore

四、编写keepalived自动发信脚本

[root@master keepalived]# pwd
/etc/keepalived
[root@master keepalived]# cat sendmail.sh
#!/bin/bash
to_email='mailaddr@qq.com'
ipaddress=`ip -4 a show dev ens33 | awk '/brd/{print $2}'`
notify() {
    mailsubject="${ipaddress}to be $1, vip转移"
    mailbody="$(date +'%F %T'): vrrp 飘移, $(hostname) 切换到 $1"
    echo "$mailbody" | mail -s "$mailsubject" $to_email
}
case $1 in
master)
    notify master
    ;;
backup)
    notify backup
    ;;
fault)
    notify fault
    ;;
*)
    echo "Usage: $(basename $0) {master|backup|fault}"
    exit 1
    ;;
esac

五、编辑keepalived配置文件

[root@master keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id master
}
vrrp_script check_run {
   script /etc/keepalived/keepalived_check_mysql.sh
   interval 5
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 66
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.36.192.6/24
    }
    track_script {
        check_run
    }
    notify_master "/etc/keepalived/sendmail.sh master"
    notify_backup "/etc/keepalived/sendmail.sh backup"
    notify_fault "/etc/keepalived/sendmail.sh fault"
}

六、查看邮件,检验是否成功 

主keepalived服务器中断开主的keepalived服务,vip漂移到备keepalived服务器

[root@master keepalived]# systemctl stop keepalived

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值