Mycat高可用搭建Keepalived + HAproxy

背景:
需要搭建Mycat商用环境,使用4台虚拟机操练一下,最终的目标是:

Mycat高可用搭建Keepalived + HAproxy_第1张图片

服务器情况:

机器IP

机器名

安装组件

192.168.239.144

douzi9

Mysql Master1|Mycat1

192.168.239.145

douzi10

Mysql Slave1|HAProxy Keepalived Master

192.168.239.146

douzi11

Mysql Master2|Mycat2

192.168.239.147

douzi12

Mysql Slave2|HAProxy Keepalived Savle

正文:

Mycat集群搭建

1.复制douzi9上的mycat到douzi11的/usr/local/下

 

2.下载HAProxy

http://download.openpkg.org/components/cache/haproxy/

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

3.安装HAProxy

  • 将下载下来的Haproxy放到Linux中, 解压文件

tar -zxvf haproxy-2.1.3.tar.gz -C /usr/local/src/

  • yum安装GCC

yum install gcc-c++

  • 编译与安装Haproxy

cd /usr/local/src/haproxy-2.1.3

查看linux内核: uname -r

进入Haproxy文件夹,编译执行命令:make TARGET=linux310 (linux内核版本)

安装特定位置:make install PREFIX=/usr/local/haproxy

  • 进入创建的文件夹cd /usr/local/haproxy,修改配置

           创建一个文件夹,mkdir conf config文件夹

           进入该文件夹,在文件夹中,创建 一个文件执行命令: touch haproxy.cnf

  • 编辑配置文件添加如下信息

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/local/haproxy/conf/haproxy.pid

    uid 99

    gid 99

    daemon #后台方式运行

    #debug

    #quiet

 

defaults

    log  global

    mode tcp    #默认的模式mode { tcp|http|health }tcp4层,http7层,health只会返回OK

    option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接

    option redispatch  #serverId对应的服务器挂掉后,强制定向到其他健康的服务器

    retries 3       #三次连接失败就认为是服务器不可用,也可以通过后面设置

    maxconn 2000          #默认的最大连接数

    timeout connect 5000  #连接超时

    timeout client  50000 #客户端超时

    timeout server  50000 #服务器超时

    #timeout check  2000  #=心跳检测超时

 

listen proxy_status       #这里是配置负载均衡,proxy_status是名字,可以任意

    bind :48066       #这里是监听的IP地址和端口,端口号可以在0-65535之间,要避免端口冲突

                  mode tcp          #连接的协议,这里是tcp协议

                  balance roundrobin

                  server mycat_1 192.168.239.144:8066 check inter 10s

                  server mycat_1 192.168.239.146:8066 check inter 10s

                 

frontend admin_stats   #管理端设置

    bind :7777      #注意bind后的空格

                  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

  • 启动对应的mycat服务后,启动haproxy

/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cnf

查看是否启动

ps -ef | grep haproxy

浏览器访问 http://douzi10:7777/admin

Mycat高可用搭建Keepalived + HAproxy_第2张图片

  • 命令访问 使用48066端口访问mycat

mysql -uroot -p123456 -h douzi9 -P 48066

  • 也可以使用客户端连接

Mycat高可用搭建Keepalived + HAproxy_第3张图片

4.下载Keepalived

  • 官网下载源码包

wget https://www.keepalived.org/software/keepalived-2.0.17.tar.gz

  • 安装keepalived所需要的 一些依赖包

yum install openssl-devel popt-devel libnl-devel kernel-devel gcc -y

  • 解压源码,安装到/usr/local/keepalived

tar -zxvf keepalived-2.0.17.tar.gz -C /usr/local/src/

cd /usr/local/src/keepalived-2.0.17/

./configure -prefix=/usr/local/keepalived

make && make install

  • 运行前配置

cp /usr/local/src/keepalived-2.0.17/keepalived/etc/init.d/keepalived /etc/init.d

mkdir /etc/keepalived

cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

cp /usr/local/src/keepalived-2.0.17/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

  • 修改douzi10服务器Master配置文件

vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived
 
global_defs {
   notification_email {
       [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_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_instance VI_1 {
    # 主机配MASTER,备机配置BACKUP
    state MASTER
    interface eno16777736
    virtual_router_id 51
# 数值越大优先级越高
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
# 虚拟IP
        192.168.239.200
    }
}
# 虚拟机不需要配置下面部分,如果客户端连接不上,可以不配置下面部分
virtual_server 192.168.239.200 48066 {
            delay_loop 6
            lb_algo rr
            lb_kind NAT
            persistence_timeout 50
            protocol TCP
            real_server 192.168.239.145 48066 {
                       weight 1
                       TCP_CHECK {
                                  connect_timeout 3
                                  retry 3
                                  delay_before_retry 3
                       }
            }
            
            real_server 192.168.239.147 48066 {
                       weight 1
                       TCP_CHECK {
                                  connect_timeout 3
                                  retry 3
                                  delay_before_retry 3
                       }
            }
}
  • 启动keepalived并验证

systemctl start keepalived

systemctl status keepalived

Mycat高可用搭建Keepalived + HAproxy_第4张图片

 

  • 修改douzi12 服务器BackUP配置文件

vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived
 
global_defs {
   notification_email {
       [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_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_instance VI_1 {
    # 主机配MASTER,备机配置BACKUP
    state BACKUP
    interface eno16777736
    virtual_router_id 51
# 数值越大优先级越高
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
# 虚拟IP
        192.168.239.200
    }
}
# 虚拟机不需要配置下面部分,如果客户端连接不上,可以不配置下面部分
virtual_server 192.168.239.200 48066 {
            delay_loop 6
            lb_algo rr
            lb_kind NAT
            persistence_timeout 50
            protocol TCP
            real_server 192.168.239.145 48066 {
                       weight 1
                       TCP_CHECK {
                                  connect_timeout 3
                                  retry 3
                                  delay_before_retry 3
                       }
            }
            
            real_server 192.168.239.147 48066 {
                       weight 1
                       TCP_CHECK {
                                  connect_timeout 3
                                  retry 3
                                  delay_before_retry 3
                       }
            }

}

  • 验证keepalived连接

mysql -uroot -p123456 -h 192.168.239.200 -P 48066

Mycat高可用搭建Keepalived + HAproxy_第5张图片

  • 客户端连接

Mycat高可用搭建Keepalived + HAproxy_第6张图片

Mycat高可用搭建Keepalived + HAproxy_第7张图片

  • 设置keepalived服务开机启动

# chkconfig keepalived on

你可能感兴趣的:(技术贴)