haproxy+keepalived负载均衡之主备切换(centos)

2020-01-30 13:14:15于丽

from email.Header import Header
import sys
import smtplib

#-------------------------------
# file: mailnotify.py
# desc: send notify email
#
#-------------------------------
strFrom = 'toplover@sina.com'
strTo = 'toplover@126.com'
smtp_server = 'smtp.sina.com'
smtp_user = 'toplover'
smtp_pass = 'xxx***xxx'

if sys.argv[1] != "master" and sys.argv[1] != "backup" and sys.argv[1] != "fault":
sys.exit()
else:
notify_type = sys.argv[1]

mail_title = "[crt] Haproxy-notify-info"
mail_body_plain = notify_type + 'alive,please check it now'
mail_body_html = '<b><font color=red>' + notify_type + 'alive,please check it now'

msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = Header(mail_title,'utf-8')
msgRoot['From'] = strFrom
msgRoot['To'] = strTo

msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)

msgText = MIMEText(mail_body_plain, 'plain', 'utf-8')
msgAlternative.attach(msgText)

msgText = MIMEText(mail_body_html, 'html', 'utf-8')
msgAlternative.attach(msgText)

smtp = smtplib.SMTP()
smtp.connect(smtp_server)
smtp.login(smtp_user,smtp_pass)
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
smtp.quit()
到此完成了简单的haproxy+keepalived负载均衡主从热备功能。
不明之处请参阅haproxy官方cofiguration.txt文件及keepalived官方文档。