keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群

系统集群架构图如下:

keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群_第1张图片

一、部署90主机和77主机的MySQL主主配置和90主机和89主机的MySQL主从配置

详情请见(

MySQL主主同步:https://blog.csdn.net/baidu_38432732/article/details/80773634

MySQL主从同步:https://blog.csdn.net/baidu_38432732/article/details/80653873

二、部署mycat的读写分离控制,详情请见(https://blog.csdn.net/baidu_38432732/article/details/80708847)

三、配置keepalived+LVS对mycat的高可用及负载均衡集群

1、在117主机和116主机上分别安装keepalived+LVS

[root@mysql ~]# yum -y install keepalived ipvsadm
安装完后修改配置文件:

117Master主机

[root@localhost ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
#   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_instance VI_1 {
    state MASTER  #(从的改成backup)
    interface ens33
    virtual_router_id 51 
    priority 100  #(从主机的值低于100即可)
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.120
    }
}
virtual_server 192.168.1.120 8066 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 0 #注意改成0,否则会出现异常
    protocol TCP
    real_server 192.168.1.130 8066 {
        weight 1
        SSL_GET {
            url {
              path /
              digest ff20ad2481f97b1754ef3e12ecd3a9cc
            }
            url {
              path /mrtg/
              digest 9b3a0c85a887a256d6939da88aabd8cd
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 8066
        }
    }
    real_server 192.168.1.76 8066 {
        weight 1
        SSL_GET {
            url {
              path /
              digest ff20ad2481f97b1754ef3e12ecd3a9cc
            }
            url {
              path /mrtg/
              digest 9b3a0c85a887a256d6939da88aabd8cd
            }
            connect_timeout 3
            nb_get_retry 3

            delay_before_retry 3
            connect_port 8066
        }
    }
}

2、在130主机和76主机上分别执行以下脚本:

[root@mysql ~]# vim realserver.sh


#!/bin/bash
#description: Config realserver
VIP=192.168.1.120
/etc/rc.d/init.d/functions
case "$1" in
start)
       /sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
       /sbin/route add -host $VIP dev lo:0
       echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
       echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
       echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
       echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
       sysctl -p >/dev/null 2>&1
       echo "RealServer Start OK"
       ;;
stop)
       /sbin/ifconfig lo:0 down
       /sbin/route del $VIP >/dev/null 2>&1
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
       echo "RealServer Stoped"
       ;;
*)
       echo "Usage: $0 {start|stop}"
       exit 1
esac
exit 0

两台服务器上执行一样的,然后执行以下,命令验证是否执行成功,当两台主机都出现以下则成功

keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群_第2张图片

3、回到117和116主机启动keepalived

[root@localhost ~]# service keepalived start
Redirecting to /bin/systemctl start keepalived.service
此时验证后虚拟IP在Master主机上,具体那个获取IP要看先启动那个

keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群_第3张图片

keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群_第4张图片

至此已经部署完毕

四、终极验证

1、部署环境将所有的数据库建表并插入数据

keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群_第5张图片

2、为了方便验证现在统一用navicat登陆数据库

VIP访问后得到所有的数据

keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群_第6张图片

情景一:停掉90Master主机,验证数据是否正常显示

keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群_第7张图片

数据可以正常访问

keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群_第8张图片

情景二:启动90主机,停掉77的Master主机


keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群_第9张图片

keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群_第10张图片

此时我们用120IP访问:还可以获取数据

keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群_第11张图片

情景一、二:证明两个数数据库可以达到高可用

情景三:关闭一个130mycat服务

keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群_第12张图片keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群_第13张图片

第一遍访问报错

keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群_第14张图片

第二遍访问:

keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群_第15张图片

重复多少点几次发现是一边正常一遍不正常说明起到负载作用而且是权重比相等的负载均衡

情景三:我们停止任何一个keepalived都可以访问数据库数据

情景四:验证读写分离

构造环境,在从服务器上插入数据,以便数据读取来区分

刚开始是如下数据

keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群_第16张图片

现插入数据如下:

keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群_第17张图片此时查看从服务器数据如下:

keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群_第18张图片

则此时直接访问主数据库无法看到刚插入的数据,但用120IP访问后就可以看到刚插入的数据

keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群_第19张图片

keepalived+LVS+Mycat+MySQL双主及主从高可用负载均衡集群_第20张图片


你可能感兴趣的:(负载均衡,MySQL,Mycat,Keepalived,LVS,云深海阔专栏)