authentication {
auth_type PASS
auth_pass 376879148
#验证类型和密码,有PASS和AH两种,一般用PASS,据说AH有问题,认证密码主备服务器之间一定要一致,否则出错
}
virtual_ipaddress {
172.21.4.44
#设置虚拟IP,可以有多个地址,每个地址占一行,不需掩码。注意:这个 ip 必须与我们在 lvs 客户端设定的vip相一致
}
track_script {
chk_nginx
#引用上面的vrrp_script定义的脚本名称
}
notify_master "/etc/keepalived/changemail.py master"
notify_backup "/etc/keepalived/changemail.py backup"
notify_fault "/etc/keepalived/changemail.py fault"
#指定当切换到主、备及故障状态时,执行的脚本
}
###################################################################################
五、HA状态切换时的邮件提醒
[root@node-02 ~]# Python -V
Python 2.7.5
[root@node-01 ~]# vi /etc/keepalived/changemail.py
########################changemail.py利用Python2.7程序发邮件###########################
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
import socket
import time
from email.MIMEText import MIMEText
from email.Utils import formatdate
from email.Header import Header
import sys
#发送邮件的相关信息,根据实际情况填写
smtpHost = 'smtp.exmail.qq.com'
smtpPort = '25'
sslPort = '465'
fromMail = 'youki@appi.com'
toMail = 'youki@appi.com'
username = 'youki@appi.com'
password = 'xxxxxxx'
#解决中文问题
reload(sys)
sys.setdefaultencoding('utf8')
#邮件标题和内容
subject = socket.gethostname() + " HA status has changed"
body = (time.strftime("%Y-%m-%d %H:%M:%S")) + " vrrp transition, " + socket.gethostname() + " changed to be " + sys.argv[1]
#初始化邮件
encoding = 'utf-8'
mail = MIMEText(body.encode(encoding),'plain',encoding)
mail['Subject'] = Header(subject,encoding)
mail['From'] = fromMail
mail['To'] = toMail
mail['Date'] = formatdate()
try:
#连接smtp服务器,明文/SSL/TLS三种方式,根据你使用的SMTP支持情况选择一种
#普通方式,通信过程不加密
#smtp = smtplib.SMTP(smtpHost,smtpPort)
#smtp.ehlo()
#smtp.login(username,password)
#tls加密方式,通信过程加密,邮件数据安全,使用正常的smtp端口
#smtp = smtplib.SMTP(smtpHost,smtpPort)
#smtp.ehlo()
#smtp.starttls()
#smtp.ehlo()
#smtp.login(username,password)
#纯粹的ssl加密方式,通信过程加密,邮件数据安全
smtp = smtplib.SMTP_SSL(smtpHost,sslPort)
smtp.ehlo()
smtp.login(username,password)
#发送邮件
smtp.sendmail(fromMail,toMail,mail.as_string())
smtp.close()
print 'OK'
except Exception:
print 'Error: unable to send email'
[root@node-01 ~]#
#######################################################################################
1、上面的脚本制作完成后,记得赋权,否则无法执行。
[root@node-01 ~]# chmod +x /etc/keepalived/changemail.py
[root@node-01 ~]#scp /etc/keepalived/keepalived.conf 172.21.4.52:/etc/keepalived
把主机上的配置文件copy到Backup服务器上,现在在两台主机上可以利用 ip addr show命令来查看VIP的获取情况
2、keeplived日志位置指定:
在主从keeplived节点上编译/etc/sysconfig/keepalived文件
[root@node-01 ~]# vi /etc/sysconfig/keepalived
将最后一行KEEPALIVED_OPTIONS="-D" 修改为:KEEPALIVED_OPTIONS="-D -d -S 0"
3、修改主从节点日志配置文件/etc/rsyslog.conf
[root@node-01 ~]# vi /etc/rsyslog.conf
加入如下配置:
#keepalived -S 0
local0.*/var/log/keepalived.log
4、重启日志服务
[root@node-01 ~]# systemctl restart rsyslog.service
5、检查/var/log/keepalived.log文件是否存在