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