首页 > 其他分享 >keepalived-状态邮件通知和定向日志输出

keepalived-状态邮件通知和定向日志输出

时间:2024-08-29 08:53:47浏览次数:15  
标签:lvs keepalived ka2 ka1 notify 日志 root 邮件

keepalived-状态邮件通知和定向日志输出

  说明1:当keepalived实例角色切换时,根据自定义邮件脚本,推送本地邮件通知

  说明2:当keepalived实例角色切换时,根据自定义邮件脚本,推送互联网邮件通知

  说明3:默认keepalive状态日志写入/var/log/messages文件

拓扑:

 

环境说明:

  1、单虚拟路由器实例

     2、master:ka1   slave:ka2

示例1:配置定义角色切换,本地邮件通知,依赖postfix服务(默认开启)

1、ka1 keepalived配置:

[root@lvs-ka1 keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
   [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id ka1
   vrrp_mcast_group4 224.100.100.100 
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 66
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        172.16.0.100/32 dev ens33 label ens33:1
    }
   notify_master "/etc/keepalived/notify.sh master"      #当前节点成为主节点时触发的脚本
   notify_backup "/etc/keepalived/notify.sh backup"      #当前节点成为从节点时触发的脚本
   notify_fault "/etc/keepalived/notify.sh fault"        #当前节点转为“失败”状态时触发的脚本
}

 

2、ka2 keepalived配置:

[root@lvs-ka2 keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
   [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id ka1
   vrrp_mcast_group4 224.100.100.100 
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 66
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        172.16.0.100/32 dev ens33 label ens33:1
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}

 

3、通用脚本:

  keepalived实例角色切换通知脚本

[root@lvs-ka2 keepalived]# cat /etc/keepalived/notify.sh 
#!/bin/bash
#
contact='root@localhost'
notify() {
    mailsubject="$(hostname) to be $1, vip floating"
    mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
    echo "$mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
    notify master
    ;;
backup)
    notify backup
    ;;
fault)
    notify fault
    ;;
*)
    echo "Usage: $(basename $0) {master|backup|fault}"
    exit 1
    ;;
esac

4、验证:

开启ka2 keepalived服务:

  依赖系统postfix服务给root发通知邮件

[root@lvs-ka2 keepalived]# systemctl start keepalived
[root@lvs-ka2 keepalived]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:50:56:34:07:10 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.126/24 brd 10.0.0.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 172.16.0.100/32 scope global ens33:1
       valid_lft forever preferred_lft forever
    inet6 fe80::250:56ff:fe34:710/64 scope link 
       valid_lft forever preferred_lft forever
[root@lvs-ka2 keepalived]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 2 messages 2 new
>N  1 root                  Tue Jan 12 21:07  18/695   "lvs-ka2 to be backup, vip floating"     #刚启动服务,角色是BACKUP,
 N  2 root                  Tue Jan 12 21:08  18/695   "lvs-ka2 to be master, vip floating"     #启动keepalived服务,组播宣告后,抢占VIP,表示活动状态,提升为master

 

再开启ka1 keepalived服务:

  依赖系统postfix服务给root发通知邮件

[root@lvs-ka1 keepalived]# systemctl start keepalived
[root@lvs-ka1 keepalived]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N  1 root                  Tue Jan 12 21:31  18/695   "lvs-ka1 to be master, vip floating"    #ka1 MASTER角色抢占VIP通知
[root@lvs-ka2 keepalived]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 3 messages 3 new
>N  1 root                  Tue Jan 12 21:07  18/695   "lvs-ka2 to be backup, vip floating"
 N  2 root                  Tue Jan 12 21:08  18/695   "lvs-ka2 to be master, vip floating"
 N  3 root                  Tue Jan 12 21:31  18/695   "lvs-ka2 to be backup, vip floating"    #ka2 角色转为BACKUP模式通知

 

示例2:配置定义角色切换,互联网邮件通知

 1、ka1和ka2都添加以下mail配置:

[root@lvs-ka1 ~]# cat /etc/mail.rc 
set from=******@qq.com                           #定义邮件通知发件人
set smtp=smtp.qq.com                             #定义smtp服务器
set smtp-auth-user=******@qq.com                 #发件人用户认证
set smtp-auth-password=******                    #发件人密码认证(开启QQ邮箱第三方授权及对应授权密码)
set smtp-auth=login                              #验证方式
set ssl-verify=ignore                            #忽略ssl验证

  2、ka1和ka2都调整收件人配置:

[root@lvs-ka1 ~]# cat /etc/keepalived/notify.sh 
#!/bin/bash
#
contact='******@163.com'                         #定义收件人邮箱
notify() {
    mailsubject="$(hostname) to be $1, vip floating"
    mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
    echo "$mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
    notify master
    ;;
backup)
    notify backup
    ;;
fault)
    notify fault
    ;;
*)
    echo "Usage: $(basename $0) {master|backup|fault}"
    exit 1
    ;;
esac

 3、验证

  停止和开启ka1,查看邮件通知

 

 

 

示例3:指定keepalive日志输出

  以ka1节点为例配置

[root@lvs-ka1 keepalived]# vim /etc/sysconfig/keepalived 

# Options for keepalived. See `keepalived --help' output and keepalived(8) and
# keepalived.conf(5) man pages for a list of all options. Here are the most
# common ones :
#
# --vrrp               -P    Only run with VRRP subsystem.
# --check              -C    Only run with Health-checker subsystem.
# --dont-release-vrrp  -V    Dont remove VRRP VIPs & VROUTEs on daemon stop.
# --dont-release-ipvs  -I    Dont remove IPVS topology on daemon stop.
# --dump-conf          -d    Dump the configuration data.
# --log-detail         -D    Detailed log messages.
# --log-facility       -S    0-7 Set local syslog facility (default=LOG_DAEMON)    #支持rsyslog自定义日志分类 local0-local7
#
KEEPALIVED_OPTIONS="-D -S 6"                                                       #指定keepalived日志分类为local6

[root@lvs-ka1 keepalived]# grep local6 /etc/rsyslog.conf                           #rsyslog配置local6,*表示所有日志级别
local6.*                                                /var/log/keepalived.log

[root@lvs-ka1 keepalived]# systemctl restart rsyslog.service
[root@lvs-ka1 keepalived]# systemctl restart keepalived

[root@lvs-ka1 keepalived]# ll /var/log/keepalived.log                              #生成keepalived自己的专有日志文件
-rw------- 1 root root 54372 Jan 12 21:31 /var/log/keepalived.log

 

标签:lvs,keepalived,ka2,ka1,notify,日志,root,邮件
From: https://www.cnblogs.com/cnblogsfc/p/14269446.html

相关文章

  • keepalived-lvs-DR
    keepalived实现lvs高可用说明:lvs单实例拓扑: 环境说明:1、client网关指向Router,跨网段访问lvs-VIP,需要指定正确网关和路由,如果client和Route之间还有其他路由或防火墙设备,网关设置直连路由IP2、虚拟机模拟路由器,Router需要开启路由转发ip_forward3、实验环境模拟路由器......
  • keepalived简介
    一、keepalived概述官方网站:https://www.keepalived.org/keepalived:vrrp协议:virtualrouterredundancyprotocolvrrp是一种容错协议,它保证当lvs调度器MASTER出现故障时,由另一台lvs调度器BACKUP来代替出现故障的lvs调度器进行工作,从而保持提供服务的连续性和可靠性......
  • 【整理】 【Windows系列】Windows安全日志分析实战:关键事件+详解
    参考......
  • 【Shell脚本】根据web访问日志,封禁请求量异常的IP,如IP在半小时后恢复正常,则解除封禁
    #!/bin/bash#####################################################################################根据web访问日志,封禁请求量异常的IP,如IP在半小时后恢复正常,则解除封禁####################################################################################lo......
  • C#环境下支持多线程的异步日志记录器的设计与实现
    上篇博文提供了c++版异步日志类,本文提供同样功能的C#版的异步日志管理类。C#环境下支持多线程的异步日志记录器的设计与实现在现代应用程序开发中,日志记录是一项至关重要的任务,它帮助开发者追踪程序的运行情况,调试问题,并进行性能监控。特别是在高并发场景下,传统的同步日志......
  • 【Shell脚本】检测网卡流量,并按规定格式记录在日志中
    #!/bin/bash########################################################检测网卡流量,并按规定格式记录在日志中#规定一分钟记录一次#日志格式如下所示:#2019-08-1220:40#ens33input:1234bps#ens33output:1235bps######################################......
  • nginx: 两个解析日志的脚本
    一,解析日志得到访问量最高的100个ip地址:awk'{print$1}'www.access_log|sort|uniq-c|sort-n-k1-r|head-n100效果如图:二,解析日志得到访问量最高的10个url命令[root@blog27]#awk'{print$7}'20240827_access.log|sort|uniq-c|sort-rn|head-10返回......
  • mysql 开启和关闭日志记录
    开启和关闭日志记录(临时)#默认情况下mysql是不会记录最近执行sql语句的,需要手动开启才能记录。另外sql语句有两种方式记录,记录到table,记录到文件。另外开启日志记录多少会占用性能,适合开发测试环境使用。--临时设置,重启MySQL服务失效showvariableslike'general_log%';--......
  • Mysql 通过binlog日志恢复数据
    Binlog日志,即binarylog,是二进制日志文件,有两个作用,一个是增量备份,另一个是主从复制,即主节点维护一个binlog日志文件,从节点从binlog中同步数据,也可以通过binlog日志来恢复数据1,登录mysql查看binlog日志的状态,输入showvariableslike‘%log_bin%’;查看binlog为off关闭状态2,开......
  • python + logging 记录日志
    日志生成的位置为当前文件目录下的tmp文件夹,是以固定大小(10M)的方式去滚动日志,如想设置为按时间滚动日志,需要设置为TimedRotatingFileHandler(filename=_create_log_path(),when="midnight",interval=1,backupCount=7)去替换RotatingFileHandler,每天晚上12点生成一个新的日志......