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

news/2024/11/15 13:52:20/文章来源:https://www.cnblogs.com/cnblogsfc/p/14269446.html

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 keepalivedglobal_defs {notification_email {root@lvs.localdomain}notification_email_from keepaliced@lvs.localdomainsmtp_server 127.0.0.1smtp_connect_timeout 30router_id ka1vrrp_mcast_group4 224.100.100.100 
}vrrp_instance VI_1 {state MASTERinterface ens33virtual_router_id 66priority 100advert_int 1authentication {auth_type PASSauth_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 keepalivedglobal_defs {notification_email {root@lvs.localdomain}notification_email_from keepaliced@lvs.localdomainsmtp_server 127.0.0.1smtp_connect_timeout 30router_id ka1vrrp_mcast_group4 224.100.100.100 
}vrrp_instance VI_1 {state BACKUPinterface ens33virtual_router_id 66priority 90advert_int 1authentication {auth_type PASSauth_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 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft foreverinet6 ::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 1000link/ether 00:50:56:34:07:10 brd ff:ff:ff:ff:ff:ffinet 10.0.0.126/24 brd 10.0.0.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft foreverinet 172.16.0.100/32 scope global ens33:1
       valid_lft forever preferred_lft foreverinet6 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

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/788963.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

keepalived-lvs-DR

keepalived实现lvs高可用说明:lvs单实例 拓扑:环境说明: 1、client网关指向Router,跨网段访问lvs-VIP,需要指定正确网关和路由,如果client和Route之间还有其他路由或防火墙设备,网关设置直连路由IP 2、虚拟机模拟路由器,Router需要开启路由转发ip_forward 3、实验环境模…

LVS之NAT模式

LVS之NAT模式实现原理图: 拓扑图: 环境说明: 1、基于DNAT实现lvs,LVS需要开启ip_forward 2、lvs和RS之间可以跨路由 3、本实验基于虚拟机实验,client与VIP直连,不用配置网关;实际环境client和lvs之间有路由,client需要配置直连路由的为网关 4、client端无法ping通RS,…

MySQL联表查询

按功能分内连接等值连接 非等值连接 自连接外连接左外连接 右外连接 全外连接交叉连接1. 内连接(INNER JOIN) 获取所有的员工名和其对应的部门名 SELECT e.last_name, d.department_name FROM employees e INNER JOIN departments dON e.department_id = d.department_id;查询…

网络防火墙之SNAT

网络防火墙 NAT NAT: network address translationPREROUTING,INPUT,OUTPUT,POSTROUTING请求报文:修改源/目标IP,由定义如何修改响应报文:修改源/目标IP,根据跟踪机制自动实现 SNAT:source NAT POSTROUTING, INPUT让本地网络中的主机通过某一特定地址访问外部网络,实现…

网络防火墙之自定义chain

网络防火墙 自定义链 链管理:-N:new, 自定义一条新的规则链-X:delete,删除自定义的空的规则链-P:Policy,设置默认策略;对filter表中的链而言,其默认策略有:ACCEPT:接受DROP:丢弃-E:重命名自定义链;引用计数不为0的自定义链不能够被重命名,也不能被删除iptables/n…

网络防火墙之DNAT

网络防火墙 NAT NAT: network address translationPREROUTING,INPUT,OUTPUT,POSTROUTING请求报文:修改源/目标IP,由定义如何修改响应报文:修改源/目标IP,根据跟踪机制自动实现DNAT:destination NAT PREROUTING , OUTPUT把本地网络中的主机上的某服务开放给外部网络访问…

Windows 10 on ARM, version 22H2 (updated Aug 2024) ARM64 AArch64 中文版、英文版下载

Windows 10 on ARM, version 22H2 (updated Aug 2024) ARM64 AArch64 中文版、英文版下载Windows 10 on ARM, version 22H2 (updated Aug 2024) ARM64 AArch64 中文版、英文版下载 基于 ARM 的 Windows 10 请访问原文链接:https://sysin.org/blog/windows-10-arm/,查看最新版…

Linux监控性能调优分析-perf(上)

1 简介 Linux perf一个轻量级命令行工具,用于剖析和监控Linux系统的CPU性能。该工具虽然简单,却能提供有助于分析CPU的丰富信息。该命令包含许多用于收集、跟踪和分析CPU事件数据的子命令。1.1 安装perf perf程序并没有预装在Linux系统中 # Ubuntu/Debian $ sudo apt install…

基于Ubuntu部署企业级kubernetes集群---k8s集群容部署

1.下载用于Kubernetes软件包仓库的公共签名秘钥#如果 `/etc/apt/keyrings` 目录不存在,则应在 curl 命令之前创建它。 sudo mkdir -p -m 755 /etc/apt/keyrings curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrin…

什么是算法?一切皆算法

如果有人问我什么算法?我就一句话:算法就是对一类问题的最优求解路径。如果有人问我什么算法?我就一句话:算法就是对一类问题的最优求解路径。 1、计算机领域的算法概念 算法一直都是计算机领域非常重要的概念,具备极高的地位,各个公司对算法的考察也非常热衷。 我们只有…

Adobe After Effects 2024 v24.6 (macOS, Windows) - 后期特效

Adobe After Effects 2024 v24.6 (macOS, Windows) - 后期特效Adobe After Effects 2024 v24.6 (macOS, Windows) - 后期特效 Acrobat、After Effects、Animate、Audition、Bridge、Character Animator、Dimension、Dreamweaver、Illustrator、InCopy、InDesign、Lightroom C…

在SimpleRAG中使用SiliconCloud快速测试Function Calling

Funcion Calling介绍 函数调用允许您将模型如gpt-4o与外部工具和系统连接起来。这对于许多事情都很有用,比如为AI助手赋能,或者在你的应用程序与模型之间建立深度集成。 如果您了解或者使用过Semantic Kernel可能会发现除了OpenAI支持Function Calling的模型之外,自动函数调…