MYSQL高可用架构之MHA实战三 mha+keepalive

在前面我们做了MHA

  MYSQL高可用架构之MHA实战一 数据库主从配置(真实可用)_ZeroMaster的博客-CSDN博客

MYSQL高可用架构之MHA实战二 安装和配置MHA架构(真实可用)

由于我们准备了一主两从,但是我们提供给外部的应该是一个ip。否则每次主服务器关了,所有的项目的数据库配置都需要改。我们用MHA做了数据库的主从。接下来用Keepalive做虚拟ip

keepalived原理:

  1:keepalived安装在两台物理服务器上 并互相监控对方是否正常运行

 2:当A工作正常的时候 会将VIP对应的MAC地址为节点A网卡的MAC地址

 3:当A发生故障的时候 节点B上的keepalived会检测到 并将VIP的MAC等于B的MAC地址

 4:Keepalived的部署比较容易,直接可以yum 安装,主要的就是他的参数文件,参数文件是如何控制主备的。

安装keepalive

master,slave1,slave2都需要安装(并且步骤都一样)。所以我们以master为例。

创建keepalive文件

mkdir /mnt/keepalive

将包导入进来有如下包:

ipvsadm_1658803458872.zip

keepalived-2.0.6.tar.gz

解压并且配置相应的文件

tar -xzvf keepalived-2.0.6.tar.gz
yum -y install openssh-clients openssl-devel
cd keepalived-2.0.6
rpm -ivh ../ipvsadm/libnl-1.1.4-3.el7.x86_64.rpm 
rpm -ivh ../ipvsadm/libnl-devel-1.1.4-3.el7.x86_64.rpm
rpm -ivh ../ipvsadm/libnfnetlink-devel-1.0.1-4.el7.x86_64.rpm
./configure --prefix=/usr/local/keepalived

继续运行:
make && make install

mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp keepalived/etc/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/sbin/keepalived   /usr/sbin/

chmod +x /etc/init.d/keepalived
systemctl enable keepalived
cd /etc/keepalived/
将修改好的keepalived.conf放到/etc/keepalived下面替换原有配置
systemctl start keepalived

其中在/etc/keepalived下的keepalived.conf进行修改。

! Configuration File for keepalived
       
global_defs {
notification_email {
[email protected]
[email protected]
}
#添加内容如下:
script_user root
enable_script_security
   
notification_email_from [email protected]
smtp_server 127.0.0.1 
smtp_connect_timeout 30
router_id MASTER-HA
}
       
vrrp_script chk_mysql_port {     #检测mysql服务是否在运行。有很多方式,比如进程,用脚本检测等等
    script "/mnt/keepalive/chk_mysql.sh"   #这里通过脚本监测
    interval 2                   #脚本执行间隔,每2s检测一次
    weight -5                    #脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5
    fall 2                    #检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)
    rise 1                    #检测1次成功就算成功。但不修改优先级
}
       
vrrp_instance VI_1 {
    state BACKUP
    #非抢占模式
    nopreempt    
    interface eth0      #指定虚拟ip的网卡接口 eth0需要更换为自己的
    mcast_src_ip 10.103.142.165   #自己的ip
    virtual_router_id 60    #路由器标识,MASTER和BACKUP必须是一致的
    priority 101            #定义优先级,数字越大,优先级越高,在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级。这样MASTER故障恢复后,就可以将VIP资源再次抢回来 
    advert_int 1         
    authentication {   
        auth_type PASS 
        auth_pass 1111     
    }
    virtual_ipaddress {    
        10.103.142.176    #vip ip,虚拟的
    }
      
track_script {               
   chk_mysql_port             
}
}

监控mysql的脚本chk_mysql.sh

#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3307"|wc -l)
if [ "${counter}" -eq 0 ]; then
    /etc/init.d/keepalived stop
fi

启动keepalive

###启动keepalive
systemctl start keepalived
### 查看启动状态。
systemctl status keepalived


参考:MHA集群部署(GTID复制+VIP+邮件提醒+binlogserver)

你可能感兴趣的:(研发,mysql,数据库)