MySQL高可用HA——keepalived配置

xiaobaif 2020-01-29

0、 Keepalived介绍

? Keepalived是基于VRRP(Virtual Router Redundancy Protocol,虚拟路由器冗余协议)协议的一款高可用软件。Keepailived有一台主服务器(master)和多台备份服务器(backup),在主服务器和备份服务器上面部署相同的服务配置,使用一个虚拟IP地址对外提供服务,当主服务器出现故障时,虚拟IP地址会自动漂移到备份服务器。

? 首先需要配置MySQL的双主配置,传送门https://www.cnblogs.com/zhqin/p/12239225.html

1、hadoop106、hadoop107
? 通过yum方式安装

[ ~]$ sudo yum install -y keepalived
[ ~]$ sudo yum install -y keepalived

2、修改hadoop06的Keepalived配置文件/etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
    router_id MySQL-ha
}
vrrp_instance VI_1 {
    state master #初始状态
    interface eth0 #网卡
    virtual_router_id 51 #虚拟路由id
    priority 100 #优先级
    advert_int 1 #Keepalived心跳间隔
    nopreempt #只在高优先级配置,原master恢复之后不重新上位
    authentication {
        auth_type PASS #认证相关
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.100 #虚拟ip
    }
}
#声明虚拟服务器
virtual_server 192.168.1.100 3306 {
    delay_loop 6
    persistence_timeout 30
    protocol TCP
    #声明真实服务器
    real_server 192.168.1.106 3306 {
        notify_down /var/lib/mysql/killkeepalived.sh #真实服务故障后调用脚本
        TCP_CHECK {
            connect_timeout 3 #超时时间
            nb_get_retry 1 #重试次数
            delay_before_retry 1 #重试时间间隔
        }
    }
}

3、修改hadoop107的Keepalived配置文件/etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
    router_id MySQL-ha
}
vrrp_instance VI_1 {
    state backup #初始状态
    interface eth0 #网卡
    virtual_router_id 51 #虚拟路由id
    priority 50 #优先级
    advert_int 1 #Keepalived心跳间隔
    authentication {
        auth_type PASS #认证相关
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.100 #虚拟ip
    }
}
#声明虚拟服务器
virtual_server 192.168.1.100 3306 {
    delay_loop 6
    persistence_timeout 30
    protocol TCP
    #声明真实服务器
    real_server 192.168.1.107 3306 {
        notify_down /var/lib/mysql/killkeepalived.sh #真实服务故障后调用脚本
        TCP_CHECK {
            connect_timeout 3 #超时时间
            nb_get_retry 1 #重试次数
            delay_before_retry 1 #重试时间间隔
        }
    }
}

4、在hadoop106、hadoop107 编辑脚本文件/var/lib/mysql/killkeepalived.sh

[ ~]$ sudo vim /var/lib/mysql/killkeepalived.sh
#! /bin/bash
sudo service keepalived stop

5、给两台机器的killkeepalived.sh脚本加执行权限

sudo chmod +x /var/lib/mysql/killkeepalived.sh

6、在两台机器上设置开机自启

sudo chkconfig keepalived on

7、在两台机器上检查开机自启状态

sudo chkconfig --list | grep keepalived

MySQL高可用HA——keepalived配置

MySQL高可用HA——keepalived配置

8、确保MySQL先于Keepalived启动
第一步:查看MySQL启动次序

[ ~]$ sudo vim /etc/init.d/mysql

MySQL高可用HA——keepalived配置

第二步:查看Keepalived启动次序

sudo vim /etc/init.d/keepalived

MySQL高可用HA——keepalived配置

保证64<86即可,如果keepalived中的启动顺序在MySQL的前面,将这两个数值交换即可。(为了避免和其他的进程造成冲突)

9、在两台机器上分别启动keepalived

sudo service keepalived start

10、在其他机器上通过配置的虚拟ip:192.168.1.100是否可以连接mysql数据库

MySQL高可用HA——keepalived配置

11、将hadoop106的MySQL服务停掉,看是否会自动切换到hadoop107上

[ ~]$ sudo service mysql stop

12、再将MySQL服务启动

[ ~]$ sudo service mysql start

13、再将keepalived服务启动

[ ~]$ sudo service keepalived start

相关推荐