lvs+keepalived实现Mycat的负载均衡--高可用

上一篇的mysql主备已经完成,但是mycat只在130主上有一个,接下来在140master2上通过docker运行一个一模一样的mycat,就可以来配置lvs+keepalived了。

  1. 再准备两台虚拟机150(keepalived主),151(keepalived从)
  2. 下载安装keepalived ipvsadm 执行
    yum install keepalived ipvsadm
    cp /etc/keepalived/keepalived.conf $PWD/keepalived.conf.bak
    vim /etc/keepalived/keepalived.conf

    keepalived.conf内容:(主)

    ! Configuration File for keepalived
    
    global_defs {
       notification_email {
         acassen@firewall.loc
         failover@firewall.loc
         sysadmin@firewall.loc
       }
       notification_email_from Alexandre.Cassen@firewall.loc
       smtp_server 192.168.200.1
       smtp_connect_timeout 30
       router_id LVS_DEVEL
    }
    
    vrrp_instance VI_1 {
        state MASTER
        interface eth0
        virtual_router_id 51
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            192.168.209.100/24 dev eth0 label eth0:3
        }
    }
    
    virtual_server 192.168.209.100 8066 { 
        delay_loop 6
        lb_algo rr
        lb_kind DR
        nat_mask 255.255.255.0
        persistence_timeout 0
        protocol TCP
    
    
    
        real_server 192.168.209.130 8066 {
    
                  weight 1
             TCP_CHECK {
                connect_timeout 10
                nb_get_retry 3
                delay_before_retry 3
            }
    
        }
    
     real_server 192.168.209.140 8066 {
    
            weight 1
                TCP_CHECK {
                connect_timeout 10
                nb_get_retry 3
                delay_before_retry 3
            }
    
    
        }
    
    
    }
    virtual_server端口必须与real_server端口一样是8066 否则无法通过vip连接mycat


    keepalived.conf内容:(备)

    ! Configuration File for keepalived
    
    global_defs {
       notification_email {
         acassen@firewall.loc
         failover@firewall.loc
         sysadmin@firewall.loc
       }
       notification_email_from Alexandre.Cassen@firewall.loc
       smtp_server 192.168.200.1
       smtp_connect_timeout 30
       router_id LVS_DEVEL
    }
    
    vrrp_instance VI_1 {
        state BACKUP
        interface eth0
        virtual_router_id 52
        priority 90
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            192.168.209.100/24 dev eth0 label eth0:3
        }
    }
    
    virtual_server 192.168.209.100 8066 {
        delay_loop 6
        lb_algo rr
        lb_kind DR
        nat_mask 255.255.255.0
        persistence_timeout 0
        protocol TCP
    
        real_server 192.168.209.130 8066 {
    
                  weight 1
             TCP_CHECK {
                connect_timeout 10
                nb_get_retry 3
                delay_before_retry 3
            }
    
        }
    
     real_server 192.168.209.140 8066 {
    
            weight 1
                TCP_CHECK {
                connect_timeout 10
                nb_get_retry 3
                delay_before_retry 3
            }
    
    
        }
    }

    配置完成后启动keepalived:

    service keepalived start

     

  3. 将这两台 Mycat 服务器都配置成 lvs 的 real server,编辑 realserver 脚本文件:vim /etc/init.d/realserver
    然后在两台mycat中执行以下脚本内容:
    #!/bin/bash
    
    VIP=192.168.209.100
    
    /etc/rc.d/init.d/functions
    
    case "$1" in
    start)
           ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
           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)
           ifconfig lo:0 down
           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

    保存脚本文件后更改该文件权限:chmod -R 755 /etc/init.d/realserver 

    同时再更改 functions 文件权限:chmod -R 755 /etc/init.d/functions

    首先启动 mysql,接着启动 mycat,然后启动 realserver(./realserver start),再启动 keepalived

  4. 测试高可用:
    关掉130(mysql01 和mycat01)



    开启130关掉150(keepalived主)

    无论哪种情况,通过sqlyog连接192.168.209.100:8066 服务均可用,即实现了mysql集群高可用

 

posted @ 2021-01-15 18:16  陈扬天  阅读(179)  评论(0编辑  收藏  举报