Mycat高可用方案-HAProxy+Keepalived

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: Mycat高可用方案-HAProxy+Keepalived

概述

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

高可用方案

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

011.Mycat_高可用方案.jpg

编号 角色 IP地址
1 Mycat1 192.168.110.145
2 Mycat2 192.168.110.150
3 HAProxy(master) 192.168.110.151
4 Keepalived(master) 192.168.110.151
5 HAProxy(backup) 192.168.110.152
6 Keepalived(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/src
    wget https://src.fedoraproject.org/repo/pkgs/haproxy/haproxy-1.5.18.tar.gz/21d35f114583ef731bc96af05b46c75a/haproxy-1.5.18.tar.gz
    tar -zxvf haproxy-1.5.18.tar.gz
    cd /usr/local/src/haproxy-1.5.18
  • 安装依赖插件

    # Ubuntu 依次执行
    sudo apt-get install -y gcc
    sudo apt-get install -y g++
    sudo apt-get install -y libssl-dev
    sudo apt-get install -y daemon
    sudo apt-get install -y make
    # CentOS
    yum 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

    配置内容如下:

    global
        log 127.0.0.1 local0
        #log 127.0.0.1 local1 notice
        #log loghost local0 info
        maxconn 4096
        chroot /usr/local/haproxy
        pidfile /usr/data/haproxy/haproxy.pid
        uid 99
        gid 99
        daemon
        #debug
        #quiet
    defaults
        log global
        mode tcp
        option abortonclose
        option redispatch
        retries 3
        maxconn 2000
        timeout connect 5000
        timeout client 50000
        timeout server 50000
    # HAProxy 监听配置
    listen proxy_status
        # 监听代理端口
        bind :48066
            mode tcp
            balance roundrobin
            # 监听Mycat服务地址
            server mycat_1 192.168.110.145:8066 check inter 10s
            server mycat_2 192.168.110.150:8066 check inter 10s
    # HAProxy 控制台配置
    frontend admin_stats
        bind :7777
            mode http
            stats enable
            option httplog
            maxconn 10
            stats refresh 30s
            stats uri /admin
            stats auth admin:123456
            stats hide-version
            stats 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

      012.Mycat_HAProxy状态.jpg

    • 验证负载均衡,通过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/src
    wget https://www.keepalived.org/software/keepalived-1.4.2.tar.gz
    tar -zxvf keepalived-1.4.2.tar.gz
    cd /usr/local/src/keepalived-1.4.2
  • 安装依赖插件

    # Ubuntu 依次执行
    sudo apt-get install -y gcc
    sudo apt-get install -y g++
    sudo apt-get install -y libssl-dev
    sudo apt-get install -y daemon
    sudo apt-get install -y make
    # CentOS
    yum 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.2
    make && make install
  • 运行前配置,将keepalived设置为系统服务

    • Ubuntu配置

      mkdir /etc/keepalived
      mkdir /etc/sysconfig
      cp /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/keepalived
      cp /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/keepalived
      cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
  • 修改配置文件

    vim /etc/keepalived/keepalived.conf

    配置文件内容如下:

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

    systemctl daemon-reload
  • 启动验证

    • 启动Keepalived

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

      root@ubuntu:~# ip addr
      1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
          link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
          inet 127.0.0.1/8 scope host lo
             valid_lft forever preferred_lft forever
          inet6 ::1/128 scope host 
             valid_lft forever preferred_lft forever
      2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
          link/ether 00:0c:29:9b:eb:14 brd ff:ff:ff:ff:ff:ff
          inet 192.168.110.151/24 brd 192.168.110.255 scope global ens33
             valid_lft forever preferred_lft forever
          inet 192.168.110.200/32 scope global ens33
             valid_lft forever preferred_lft forever
          inet6 fe80::20c:29ff:fe9b:eb14/64 scope link 
             valid_lft forever preferred_lft forever
      3: 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:ff
          inet 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

      013.Mycat_Keepalived_停止Mycat.jpg

    • 通过虚拟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 48066
      mysql: [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 2
      Server 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 its
      affiliates. Other names may be trademarks of their respective
      owners.
      
      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 names
      You can turn off this feature to get a quicker startup with -A
      
      Database changed
      mysql> 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)
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
7月前
【优化篇】使用Keepalived实现简单的GrayLog高可用
【优化篇】使用Keepalived实现简单的GrayLog高可用
80 0
|
运维 负载均衡 应用服务中间件
nginx高可用选型-keepalived
Keepalived 是一个用 C 语言编写的路由软件。该项目的主要目标是为 Linux 系统和基于 Linux 的基础架构提供简单而强大的负载平衡和高可用性设施。负载平衡框架依赖于众所周知且广泛使用的LVS (IPVS) 内核模块,提供第 4 层负载平衡。
241 0
nginx高可用选型-keepalived
|
负载均衡 网络协议 关系型数据库
MyCat-集群-keepalived 启动及 haproxy 的高可用测试 | 学习笔记
快速学习 MyCat-集群-keepalived 启动及 haproxy 的高可用测试
MyCat-集群-keepalived 启动及 haproxy 的高可用测试 | 学习笔记
|
应用服务中间件 Shell Linux
nginx通过keepalived实现高可用(主备方式)
nginx通过keepalived实现高可用(主备方式)
576 0
|
Web App开发 测试技术 PHP
|
域名解析 负载均衡 应用服务中间件
|
负载均衡 JavaScript 应用服务中间件
Nginx负载均衡配置+keepalived高可用
Nginx负载均衡配置+keepalived高可用
3388 0
|
网络架构
Keepalived 介绍、安装、配置(高可用)
1、keepalived的工作原理是VRRP(Virtual Router Redundancy Protocol)虚拟路由冗余协议。 2、VRRP路由器是指运行VRRP的路由器,是物理实体,虚拟路由器是指VRRP协议创建的,是逻辑概念。
1230 0

热门文章

最新文章