zoukankan      html  css  js  c++  java
  • MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(三)

    设置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:

    #!/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:

     1 #!/usr/bin/python
     2 # -*- coding: utf-8 -*-
     3 # author  xuaiqi
     4 # date    20190418
     5 
     6 import os
     7 import time
     8 import shutil
     9 import MySQLdb
    10 import smtplib
    11 #import requests
    12 import datetime
    13 
    14 from email.MIMEText import MIMEText
    15 from email.MIMEImage import MIMEImage
    16 from email.MIMEMultipart import MIMEMultipart
    17 from email.MIMEBase import MIMEBase
    18 from email import Encoders
    19 from datetime import datetime
    20 
    21 import sys
    22 default_encoding = 'utf-8'
    23 if sys.getdefaultencoding() != default_encoding:
    24     reload(sys)
    25     sys.setdefaultencoding(default_encoding)
    26 
    27 # email info
    28 mailserver = "smtp.vcredit.com" 
    29 EMAIL_USERNAME = 'dbmonitor@vcredit.com'
    30 EMAIL_PASSWORD = 'dbmonitor1234!!'
    31 
    32 title=sys.argv[1]
    33 contents=sys.argv[2]
    34 
    35 #发送邮件
    36 def send_mail(title, to_list):
    37     me = "dbmonitor@vcredit.com"
    38 
    39     def _create_msg():
    40         msg = MIMEMultipart('related')
    41         msg['Subject'] =  title
    42         msg['From'] = me
    43         msg['To'] = ';'.join(to_list)
    44         msg['date'] = '2012-3-16'  #解决邮件发送时间不对
    45         msg.preamble = 'This is a multi-part message in MIME format.'            
    46         msg_text = MIMEText(contents, 'html','utf-8')
    47         msg_alternative = MIMEMultipart('alternative')
    48         msg_alternative.attach(msg_text)
    49         msg.attach(msg_alternative)
    50         #print msg
    51         return msg
    52     try:
    53         server = smtplib.SMTP()
    54         server.connect(mailserver)
    55         server.login(EMAIL_USERNAME, EMAIL_PASSWORD)
    56         server.sendmail(me, to_list, _create_msg().as_string())
    57         server.close()
    58         print 'send mail Ok!'
    59     except Exception, e:
    60         print e
    61 
    62 
    63 
    64 print"--------------------------------------", datetime.now().strftime('%Y-%m-%d %H:%M:%S'),"Start sending mail --------------------"
    65 if __name__ == '__main__':
    66     user=['yujun02@vcredit.com','zhanglin04@vcredit.com','xuaiqi@vcredit.com']
    67     print "发送的用户",user
    68     print "内容",contents
    69     send_mail(title, user)
    70 print"---------------------------------", datetime.now().strftime('%Y-%m-%d %H:%M:%S'),"Sucessfull sending mail --------------------"
    View Code

    设置MHA切换邮件通知

    报告脚本 send_report.py:

     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基础配置:

     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切换

     

     

    mha切换

  • 相关阅读:
    计算 HMAC-SHA1 阿里云消息队列RocketMQ版签名机制案例以及http调用接口案例
    按照参数名称的字典顺序对请求中所有的请求参数(包括公共请求参数和接口的自定义参数,但不包括公共请求参数中的Signature参数)进行排序
    Appium自动化(2)
    TERSUS笔记员工信息401-显示列表处理+序号+01共几条取值+08每页条数下拉菜单值设置+02共页数计算取值
    TERSUS笔记员工信息400-增加
    TERSUS笔记310-删除
    TERSUS笔记309-修改
    TERSUS笔记308-查询
    TERSUS笔记307-07GO
    TERSUS笔记306-03首页
  • 原文地址:https://www.cnblogs.com/EikiXu/p/10750593.html
Copyright © 2011-2022 走看看