欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

基于mysql主主互备实现keepalived高可用 方案

程序员文章站 2024-03-20 15:25:10
...

keepalived安装与配置

安装包获取:https://www.keepalived.org

二进制安装:

# 同步时钟源
# yum -y install ntp ntpdate
# ntpdate cn.pool.ntp.org
# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
#获取安装包
[aaa@qq.com ~]# ls
keepalived-2.0.20.tar.gz
[aaa@qq.com ~]# tar xzf keepalived-2.0.20.tar.gz -C /usr/local/src/
# 配置编译环境
# yum install openssl-devel gcc gcc-c++ make
# 创建安装目录
# mkdir -p /data/keepalived
# cd /usr/local/src/keepalived-2.0.20/
# ./configure --prefix=/data/keepalived
# echo $?
0
# make
# echo $?
0
# make install
# echo $?
0

配置规范启动

# cd /usr/local/src/keepalived-2.0.20/keepalived/etc/
# cp -R init /data/keepalived/
# cp -R init /data/keepalived/etc/
# cp -R init.d /data/keepalived/etc/
# ll /data/keepalived/etc/
总用量 0
drwxr-xr-x. 2 root root  86 7月  31 21:58 init
drwxr-xr-x. 2 root root 135 7月  31 21:58 init.d
drwxr-xr-x. 3 root root  44 7月  31 18:01 keepalived
drwxr-xr-x. 2 root root  24 7月  31 17:59 sysconfig

修改配置文件

cp -p /etc/keepalived/keepalived.conf{,.bak}
#mysql01端
! Configuration File for keepalived

global_defs {
   notification_email {
     aaa@qq.com
     aaa@qq.com
     aaa@qq.com
   }
   notification_email_from aaa@qq.com
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id HA_MYSQL01
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_script check_mysqld {
    script "/etc/keepalived/check_mysqld.sh"
    interval 2
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.10.201
    }

    track_script {
        check_mysqld
    }
}

# mysql02端
! Configuration File for keepalived

global_defs {
   notification_email {
     aaa@qq.com
     aaa@qq.com
     aaa@qq.com
   }
   notification_email_from aaa@qq.com
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id HA_MYSQL01
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script check_mysqld {
    script "/etc/keepalived/check_mysqld.sh"
    interval 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.10.201
    }

    track_script {
        check_mysqld
    }
}

验证

两端都开启keepalived —> systemctl start keepalived

基于mysql主主互备实现keepalived高可用 方案
基于mysql主主互备实现keepalived高可用 方案

mysql01端停止keepalived

基于mysql主主互备实现keepalived高可用 方案
基于mysql主主互备实现keepalived高可用 方案

注意

mysql01端恢复后ip又漂移到mysql01端上
故虽然此时能够完成故障的切换,但数据库的应用场景上不建议频繁的切换,因此将其配置为vip不抢占模式,具体配置如下
只需修改mysql01端的配置文件如下:

! Configuration File for keepalived

global_defs {
   notification_email {
     aaa@qq.com
     aaa@qq.com
     aaa@qq.com
   }
   notification_email_from aaa@qq.com
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id HA_MYSQL01
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script check_mysqld {
    script "/etc/keepalived/check_mysqld.sh"
    interval 2
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 50
    advert_int 1
    nopreempt   #添加此选项,不抢占模式
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.10.201
    }

    track_script {
        check_mysqld
    }
}

此时重启mysql01端keepalived服务

相当于故障了,并随之恢复;此时ip漂移到mysql02端且不会漂移回mysql01
基于mysql主主互备实现keepalived高可用 方案
基于mysql主主互备实现keepalived高可用 方案

此时我们将mysql02端服务停掉,ip漂移到mysql01

基于mysql主主互备实现keepalived高可用 方案
基于mysql主主互备实现keepalived高可用 方案

用master主机测试

基于mysql主主互备实现keepalived高可用 方案
基于mysql主主互备实现keepalived高可用 方案
------------------------------------------------------------------------------------------------------- 返回目录