作者:微枫Micromaple

主页:欢迎关注Micromaple

简介:Java程序员、后端全栈工程师

点赞➕收藏⭐➕留言 您的支持就是我前进的动力

大家好,我是微枫Micromaple,下面是我的Mycat系列专栏(持续更新ing),有喜欢的小伙伴欢迎订阅学习呀

深入学习Mycat

Mycat系列专栏:点我学习

Mycat主从搭建、读写分离:点我学习

Mycat实现分库分表:点我学习

Mycat实现单库水平分表、按月分表:点我学习

文章目录

    • 概述
    • 高可用方案
    • 安装配置 HAProxy
    • 配置 Keepalived

概述

在实际项目中,Mycat服务也需要考虑高可用性,如果Mycat所在服务器出现宕机,或Mycat服务故障,需要有备机提供服务,需要考虑Mycat集群。

高可用方案

使用HAProxy + Keepalived配合两台Mycat搭起Mycat集群,实现高可用性。HAProxy实现了Mycat多节点的集群高可用和负载均衡,而HAProxy自身的高可用则可以通过Keepalived 来实现。

编号角色IP地址
1Mycat1192.168.110.145
2Mycat2192.168.110.150
3HAProxy(master)192.168.110.151
4Keepalived(master)192.168.110.151
5HAProxy(backup)192.168.110.152
6Keepalived(backup)192.168.110.152

安装配置 HAProxy

各版本下载地址:

https://src.fedoraproject.org/repo/pkgs/haproxy/

本文使用版本为:1.5.18

下载地址:

https://src.fedoraproject.org/repo/pkgs/haproxy/haproxy-1.5.18.tar.gz/21d35f114583ef731bc96af05b46c75a/haproxy-1.5.18.tar.gz

安装步骤:

  • 将下载好的HAProxy安装包传到/usr/local/src目录下进行解压

    cd /usr/local/srcwget https://src.fedoraproject.org/repo/pkgs/haproxy/haproxy-1.5.18.tar.gz/21d35f114583ef731bc96af05b46c75a/haproxy-1.5.18.tar.gztar -zxvf haproxy-1.5.18.tar.gzcd /usr/local/src/haproxy-1.5.18
  • 安装依赖插件

    # Ubuntu 依次执行sudo apt-get install -y gccsudo apt-get install -y g++sudo apt-get install -y libssl-devsudo apt-get install -y daemonsudo apt-get install -y make# CentOSyum install -y gcc openssl-devel popt-devel
  • 查看内核版本,进行编译

    # 查看内核版本uname -r# 查看 haproxy README文件对应的系统内核版本cat /usr/local/haproxy/README
    make TARGET=linux2628 PREFIX=/usr/local/haproxy ARCH=x86_64
    • ARGET:内核版本,使用uname -r查看内核,结合查看haproxy README文件对应的系统内核版本
    • ARCH:系统位数
    • PREFIX:为haprpxy安装路径
  • 编译完成,进行安装

    make install PREFIX=/usr/local/haproxy
  • 安装完成

    • 创建配置目录
    mkdir -p /usr/data/haproxy/
    • 创建HAProxy配置文件
    vim /usr/local/haproxy/haproxy.conf

    配置内容如下:

    globallog 127.0.0.1 local0#log 127.0.0.1 local1 notice#log loghost local0 infomaxconn 4096chroot /usr/local/haproxypidfile /usr/data/haproxy/haproxy.piduid 99gid 99daemon#debug#quietdefaultslog globalmode tcpoption abortoncloseoption redispatchretries 3maxconn 2000timeout connect 5000timeout client 50000timeout server 50000# HAProxy 监听配置listen proxy_status# 监听代理端口bind :48066mode tcpbalance roundrobin# 监听Mycat服务地址server mycat_1 192.168.110.145:8066 check inter 10sserver mycat_2 192.168.110.150:8066 check inter 10s# HAProxy 控制台配置frontend admin_statsbind :7777mode httpstats enableoption httplogmaxconn 10stats refresh 30sstats uri /adminstats auth admin:123456stats hide-versionstats admin if TRUE
  • 启动验证

    • 启动HAProxy

      /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.conf
    • 查看HAProxy进程

      ps -ef|grep haproxy
    • 打开浏览器访问

      http://192.168.110.151:7777/admin

      输入配置文件中的账号密码进行访问。admin/123456

    • 验证负载均衡,通过HAProxy访问Mycat

      mysql -umycat -p123456 -h 192.168.110.151 -P 48066

HAProxy(backup) 192.168.110.152如上配置

配置 Keepalived

各版本下载地址:

https://www.keepalived.org/download.html

本文使用版本为:1.4.2

下载地址:

https://www.keepalived.org/software/keepalived-1.4.2.tar.gz

安装步骤:

  • 将下载好的Keepalived安装包传到/usr/local/src目录下进行解压

    cd /usr/local/srcwget https://www.keepalived.org/software/keepalived-1.4.2.tar.gztar -zxvf keepalived-1.4.2.tar.gzcd /usr/local/src/keepalived-1.4.2
  • 安装依赖插件

    # Ubuntu 依次执行sudo apt-get install -y gccsudo apt-get install -y g++sudo apt-get install -y libssl-devsudo apt-get install -y daemonsudo apt-get install -y make# CentOSyum install -y gcc openssl-devel popt-devel
  • 进入解压后的目录,进行配置

    cd /usr/local/src/keepalived-1.4.2./configure --prefix=/usr/local/keepalived
  • 进行编译,完成后进行安装

    cd /usr/local/src/keepalived-1.4.2make && make install
  • 运行前配置,将keepalived设置为系统服务

    • Ubuntu配置

      mkdir /etc/keepalivedmkdir /etc/sysconfigcp /usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/cp /usr/local/src/keepalived-1.4.2/keepalived/etc/init.d/keepalived /etc/init.d/cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/ln -s /usr/local/keepalived/sbin/keepalived /sbin/
    • CentOS配置

      mkdir /etc/keepalivedcp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/cp /usr/local/src/keepalived-1.4.2/keepalived/etc/init.d/keepalived /etc/init.d/cp /usr/local/src/keepalived-1.4.2/keepalived/etc/sysconfig/keepalived /etc/default/keepalivedcp /usr/local/keepalived/sbin/keepalived /usr/sbin/
  • 修改配置文件

    vim /etc/keepalived/keepalived.conf

    配置文件内容如下:

    ! Configuration File for keepalivedglobal_defs {notification_email {xlcocoon@foxmail.com}notification_email_from keepalived@showjoy.comsmtp_server 127.0.0.1smtp_connect_timeout 30router_id LVS_DEVELvrrp_skip_check_adv_addrvrrp_garp_interval 0vrrp_gna_interval 0}vrrp_instance VI_1 {#主机配MASTER,备机配BACKUPstate MASTER#所在机器网卡interface ens33virtual_router_id 51#数值越大优先级越高priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {#虚拟IP,在HAProxy之上设定一个虚拟IP,可以在该网段内任意指定未使用的IP192.168.110.200}}virtual_server 192.168.110.200 48066 {delay_loop 6lb_algo rrlb_kind NATpersistence_timeout 50protocol TCP# HAProxy 地址real_server 192.168.110.151 48066 {weight 1TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}real_server 192.168.110.152 48066 {weight 1TCP_CHECK {connect_timeout 3nb_get_retry 3delay_before_retry 3}}}
  • 配置重载

    systemctl daemon-reload
  • 启动验证

    • 启动Keepalived

      service keepalived start
    • 启动后可以看到在ens33真实IP:192.168.110.151下面挂载了一个虚拟IP:192.168.110.200

      root@ubuntu:~# ip addr1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft foreverinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000link/ether 00:0c:29:9b:eb:14 brd ff:ff:ff:ff:ff:ffinet 192.168.110.151/24 brd 192.168.110.255 scope global ens33 valid_lft forever preferred_lft foreverinet 192.168.110.200/32 scope global ens33 valid_lft forever preferred_lft foreverinet6 fe80::20c:29ff:fe9b:eb14/64 scope linkvalid_lft forever preferred_lft forever3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default link/ether 02:42:c3:d3:e0:c7 brd ff:ff:ff:ff:ff:ffinet 172.17.0.1/16 brd 172.17.255.255 scope global docker0 valid_lft forever preferred_lft forever
    • 查看Keepalived状态

      service keepalived status
    • 登录验证

      mysql -umycat -p123456 -h 192.168.110.200 -P 48066
  • 测试高可用

    • 关闭Mycat1

    • 通过虚拟IP查询数据

      # 通过Keepalived登录数据库mysql -umycat -p123456 -h 192.168.110.200 -P 48066# 切换数据库use TESTDB;# 执行SQL命令show tables;
      root@ubuntu-mysql-master:~# mysql -umycat -p123456 -h 192.168.110.200 -P 48066mysql: [Warning] Using a password on the command line interface can be insecure.Welcome to the MySQL monitor.Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.6.29-mycat-1.6.7.6-release-20220524173810 MyCat Server (OpenCloudDB)Copyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> use TESTDB;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> show tables;+------------------+| Tables_in_orders |+------------------+| customer || dict_order_type|| login_info || orders || orders_detail|| orders_ware_info || payment_info |+------------------+7 rows in set (0.00 sec)