keepalived故障切换邮件通知
keepalived+nginx,实现nginx高可用,注意事项:1、VIP不需要在服务器网络配置文件中配置。2、nginx主不可用时,需要kill掉nginx主的keepalived服务,这样才可以实现VIP切换,因为主的keepalived优先级高。3、故障切换时发送邮件通知由nginx备的keepalived服务来实现。
其中nginx主上keepalived.conf配置为:
/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
#标识本节点的名称
router_id master
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
#每2秒检测一次nginx的运行状态
interval 2
#失败一次,将自己的优先级调整为-20
weight -20
}
vrrp_instance VI_1 {
#状态,主节点为MASTER
state MASTER
#绑定VIP的网络接口
interface ens33
#虚拟路由的ID号,两个节点设置必须一样
virtual_router_id 51
#节点优先级,值范围0~254,MASTER>BACKUP
priority 100
#组播信息发送时间间隔,两个节点必须设置一样,默认为1秒
advert_int 1
#设置验证信息,两个节点必须一致
authentication {
auth_type PASS
auth_pass 1111
}
#虚拟IP,两个节点设置必须一致,可以设置多个
virtual_ipaddress {
192.168.1.106
}
#nginx存活状态检测脚本
track_script {
chk_nginx
}
}
其中调用的/etc/keepalived/nginx_check.sh脚本内容为:
#!/bin/bash
A=`ps -C nginx -no-header |wc -l`
if [ $A -eq 1 ];then
pkill keepalived
fi
nginx备上keepalived.conf配置为:
! Configuration File for keepalived
global_defs {
#标识本节点的名称
router_id backup
}
vrrp_instance VI_1 {
#状态,备节点为BACKUP
state BACKUP
#绑定VIP的网络接口
interface ens33
#虚拟路由的ID号,两个节点设置必须一样
virtual_router_id 51
#节点优先级,值范围0~254,MASTER>BACKUP
priority 99
#组播信息发送时间间隔,两个节点必须设置一样,默认为1秒
advert_int 1
#设置验证信息,两个节点必须一致
authentication {
auth_type PASS
auth_pass 1111
}
#节点变为master时执行
notify_master "/usr/bin/python /root/send_mail_ssl.py backup 192.168.1.232 192.168.1.235"
#虚拟IP,两个节点设置必须一致,可以设置多个
virtual_ipaddress {
192.168.1.106
}
}
里边调用的send_mail.sh脚本为使用Perl编写的,需要安装环境:
yum -y install perl-CPAN
cpan Net::SMTP_auth
send_mail.sh脚本内容为:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib, sys, time, subprocess
from email.mime.text import MIMEText
from email.utils import formataddr
my_sender='xxx@qqvbc.com' # 发件人邮箱账号
my_pass = 'xxx@' # 发件人邮箱密码
my_user='xxx@qq.com' # 收件人邮箱账号,我这边发送给自己
def print_help():
note = '''python script.py role ip vip
'''
print(note)
exit(1)
time_stamp = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
p = subprocess.Popen('hostname', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
hostname = p.stdout.readline().split('\n')[0]
if len(sys.argv) != 4:
print_help()
elif sys.argv[1] == 'master':
message_content = '%s server: %s(%s) change to Master, vIP: %s' %(time_stamp, sys.argv[2], hostname, sys.argv[3])
subject = '%s change to Master -- keepalived notify' %(sys.argv[2])
elif sys.argv[1] == 'backup':
message_content = '%s server: %s(%s) change to Backup, vIP: %s' %(time_stamp, sys.argv[2], hostname, sys.argv[3])
subject = '%s change to Backup -- keepalived notify' %(sys.argv[2])
else:
print_help()
def mail():
ret=True
try:
msg=MIMEText(message_content,'plain','utf-8')
msg['From']=formataddr(["Server Auto Notify",my_sender]) # 括号里的对应发件人邮箱昵称、发件人邮箱账号
msg['To']=formataddr(["SA",my_user]) # 括号里的对应收件人邮箱昵称、收件人邮箱账号
msg['Subject']="服务器状态切换通知" # 邮件的主题,也可以说是标题
server=smtplib.SMTP_SSL("smtp.exmail.qq.com", 465) # 发件人邮箱中的SMTP服务器,端口是25
server.login(my_sender, my_pass) # 括号中对应的是发件人邮箱账号、邮箱密码
server.sendmail(my_sender,[my_user,],msg.as_string()) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
server.quit() # 关闭连接
except Exception: # 如果 try 中的语句没有执行,则会执行下面的 ret=False
ret=False
return ret
ret=mail()
if ret:
#print("邮件发送成功")
sys.exit(0)
else:
#print("邮件发送失败")
sys.exit(1)