MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(三)

时间:2022-12-14 03:19:51

设置Keepalived VIP切换邮件告警

修改keepalived.conf配置:

[root@server01 keepalived]# cat keepalived.conf 
! Configuration File for keepalived

global_defs {
     notification_email {
     saltstack@163.com
   }
   notification_email_from dba@dbserver.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id MySQL-HA
}
vrrp_script vs_mysql_100 {
    script "python /etc/keepalived/checkProxySQL.py -h localhost -P 6033"
    interval 5
}
vrrp_instance VI_1 {
    state BACKUP
    nopreempt
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1
    nopreempt

    authentication {
    auth_type PASS
    auth_pass 1111
    }
    track_script {
       vs_mysql_100
    }
    virtual_ipaddress {
        10.200.22.178
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}

通知 notify.sh:

MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(三)MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(三)
#!/bin/bash

contact='root@localhost'

notify() {
    local mailsubject="$(hostname) to be $1, vip floating"
    local mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
    python /etc/keepalived/sendMail.py "$mailsubject" "$mailbody"
    echo $mailbody >> /etc/keepalived/notify.log
}

case $1 in
    master)
        notify master
        ;;
    backup)
        notify backup
        ;;
    fault)
        notify fault
        ;;
    *)
    echo "Usage: $(basename $0) {master|backup|fault}"
    exit 1
    ;;
esac
View Code

发送邮件 sendMail.py:

MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(三)MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(三)
#!/usr/bin/python
# -*- coding: utf-8 -*-
# author  xuaiqi
# date    20190418

import os
import time
import shutil
import MySQLdb
import smtplib
#import requests
import datetime

from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Encoders
from datetime import datetime

import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)

# email info
mailserver = "smtp.vcredit.com" 
EMAIL_USERNAME = 'dbmonitor@vcredit.com'
EMAIL_PASSWORD = 'dbmonitor1234!!'

title=sys.argv[1]
contents=sys.argv[2]

#发送邮件
def send_mail(title, to_list):
    me = "dbmonitor@vcredit.com"

    def _create_msg():
        msg = MIMEMultipart('related')
        msg['Subject'] =  title
        msg['From'] = me
        msg['To'] = ';'.join(to_list)
        msg.preamble = 'This is a multi-part message in MIME format.'            
        msg_text = MIMEText(contents, 'html','utf-8')
        msg_alternative = MIMEMultipart('alternative')
        msg_alternative.attach(msg_text)
        msg.attach(msg_alternative)
        #print msg
        return msg
    try:
        server = smtplib.SMTP()
        server.connect(mailserver)
        server.login(EMAIL_USERNAME, EMAIL_PASSWORD)
        server.sendmail(me, to_list, _create_msg().as_string())
        server.close()
        print 'send mail Ok!'
    except Exception, e:
        print e



print"--------------------------------------", datetime.now().strftime('%Y-%m-%d %H:%M:%S'),"Start sending mail --------------------"
if __name__ == '__main__':
    user=['yujun02@vcredit.com','zhanglin04@vcredit.com','xuaiqi@vcredit.com']
    print "发送的用户",user
    print "内容",contents
    send_mail(title, user)
print"---------------------------------", datetime.now().strftime('%Y-%m-%d %H:%M:%S'),"Sucessfull sending mail --------------------"
View Code

 

设置MHA切换邮件通知

报告脚本 send_report.py:

MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(三)MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(三)
 1 #!/usr/bin/env python
 2 #-*- encoding:utf-8 -*-
 3 #-------------------------------------------------------------------------------
 4 # Name:        send_report.py
 5 # Author:      zhoujy
 6 #----------------------------------------------
 7 import os
 8 import sys
 9 import time
10 import datetime
11 import smtplib
12 import subprocess
13 import fileinput
14 import getopt
15 from email.mime.text import MIMEText
16 from email.mime.multipart import MIMEMultipart
17 from email.Utils import COMMASPACE, formatdate
18 
19 reload(sys)
20 sys.setdefaultencoding('utf8')
21 
22 def send_mail(to, subject, text, from_mail, server="localhost"):
23     message = MIMEMultipart()
24     message['From'] = from_mail
25     message['To'] = COMMASPACE.join(to)
26     message['Date'] = formatdate(localtime=True)
27     message['Subject'] = subject
28     message.attach(MIMEText(text,_charset='utf-8'))
29     smtp = smtplib.SMTP(server)
30     smtp.login(from_mail, 'dbmonitor1234!!')
31     smtp.sendmail(from_mail, to, message.as_string())
32     smtp.close()
33 
34 if __name__ == "__main__":
35     opts,args = getopt.getopt(sys.argv[1:],"h",["orig_master_host=","new_master_host=","new_slave_hosts=","conf=","subject=","body=","app_vip=","new_master_ssh_port=","ssh_user="])
36 #    print opts,args
37     for lines in opts:
38         key,values = lines
39         if key == '--orig_master_host':
40             orig_master_host = values
41         if key == '--new_master_host':
42             new_master_host = values
43         if key == '--new_slave_hosts':
44             new_slave_hosts = values
45         if key == '--subject':
46             subject = values
47         if key == '--body':
48             body = values
49 #    text = sys.stdin.read()
50     mail_list = ['xuaiqi@vcredit.com','yujun02@vcredit.com','zhanglin04@vcredit.com']
51     send_mail(mail_list, subject.encode("utf8"), body, "dbmonitor@vcredit.com", server="smtp.vcredit.com")
View Code

Master基础配置:

MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(三)MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(三)
 1 [server default]
 2 user=root
 3 password=iforgot
 4 ssh_user=root
 5 repl_user=repl
 6 repl_password=repl
 7 ping_interval=1
 8 #master_binlog_dir= /var/lib/mysql,/var/log/mysql
 9 secondary_check_script=masterha_secondary_check -s 10.200.22.136 -s 10.200.22.138 -s 10.200.22.223
10 master_ip_failover_script="/etc/mha/scripts/master_ip_failover"
11 master_ip_online_change_script="/etc/mha/scripts/master_ip_online_change"
12 report_script="/etc/mha/scripts/send_report.py"
View Code

 

测试验证

keepalived vip切换

 MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(三)

 

 MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(三)

 

mha切换

MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(三)