企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡
  XHDrlE4yaUzu 2023年11月02日 62 0


上一篇:企业实战_19_MyCat初始化ZK并配置Mycat支持ZK​

解决了引入多个mycat节点之间配置文件信息同步问题
如何在多个mycat之间进行负载均衡的问题?
在某一个mycat节点出现宕机之后,我们还可以在集群中,将这个宕机的节点提出到负载之外,需要引入HAPpoxy和keepalived

文章目录

HAPpoxy是什么?
7层的代理服务,本身是没有状态的,因此,我们可以通过部署多台HAPpoxy服务的方式,来实现HAPpoxy的高可用,不过具体要使用哪一个HAPpoxy来提供服务呢?
这时候,我们需要使用另外一个组件keepalived,来进行判断了,这里使用keepalived呢,主要是对HAPpoxy来进行监控,并且对外提供一个虚拟ip,来访问HAPpoxy服务,以达到HAPpoxy的高可用,接下来需要对HAPpoxy进行相应的配置,把访问mycat的请求,均匀的将球分配到后面mycat节点上,把直接访问mycat的方式,修改为访问HAPpoxy提供的虚拟ip的方式,来访问mycat

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_MyCat

主机名

IP地址

角色

mycat01

192.168.92.101

MYCAT/MYSQL/ZK/HAProxy/keepalied

node1

192.168.92.102

ZK/MYSQL

node2

192.168.92.103

ZK/MYSQL

mycat02

192.168.92.104

MYCAT/MYSQL/HAProxy/keepalied

温馨提醒:建议把HAProxy和keepalied部署到和mycat同一节点上,MYSQL数据建议部署在不同服务器上

在mycat01节点和mycat02节点,安装haproxy

一、mycat01节点安装和配置haproxy
1. 安装haproxy
yum install haproxy -y
2. 编辑/etc/haproxy/haproxy.cfg
vim /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application.
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file.
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1

chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon

# turn on stats unix socket
stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000

# 在这里开始添加配置
# 添加haproxy管理端口 用来监控hapeoxy的运行状态
listen admin_status
bind 0.0.0.0:48800
stats uri /admin-status
stats auth admin:admin

# 对后端mycat的监听服务
listen allmycat_service
# 对外提供的服务端口,mycat启动之后,使用8096访问mycat服务
# 后期我们的程序也是通过8096访问数据库
bind 0.0.0.0:8096
# 访问的模式tcp
mode tcp
# 日志格式采用tcplog格式
option tcplog
option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
# 负载均衡算法 轮询算法
balance roundrobin
# 对后端mycat服务配置
# 通过48700端口来进行监控
# 每隔5s监控一次,失败后重试3次
server mycat_01 192.168.92.101:8066 check port 48700 inter 5s rise 2 fall 3
server mycat_04 192.168.92.104:8066 check port 48700 inter 5s rise 2 fall 3

# 对mycat的管理端口来进行监控
listen allmycat_admin
bind 0.0.0.0:8097
mode tcp
option tcplog
option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
balance roundrobin
# mycat的管理端口
server mycat_01 192.168.92.101:9066 check port 48700 inter 5s rise 2 fall 3
server mycat_04 192.168.92.104:9066 check port 48700 inter 5s rise 2 fall 3

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_MyCat_02


企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_mysql_03

3. 使用48700端口来对mycat监控
  • 启动48700端口,需要安装xinetd
yum install xinetd
4. 新建mycatchk文件

在/etc/xinetd.d/目录下面,新建mycatchk文件

#新建mycatchk脚本
vim /etc/xinetd.d/mycatchk
#添加内容如下:
# default:
# description: monitor for
service mycatchk
{
flags = REUSE
socket_type = stream
port = 48700
wait = no
user = root
server =/app/mycat/bin/mycat_status
log_on_failure += USERID
disable = no
per_source = UNLIMITED
}

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_MyCat_04


注释:这个地方脚本的路径在mycat的安装目录,我的安装目录为/app/mycat

5. 新建mycat_status脚本

#在/usr/local/bin目录下面,新建mycat_status脚本

#新建mycat_status脚本
vim /app/mycat/bin/mycat_status
#脚本内容如下
#!/bin/bash
#/usr/local/bin/mycat_status.sh
# This script checks if a mycat server is healthy running on localhost.
# return:
#
# "HTTP/1.x 200 OK\r" (if mycat is running smoothly)
#
# "HTTP/1.x 503 Internal Server Error\r" (else)
mycat=`/app/mycat/bin/mycat status |grep 'not running'| wc -l`
if [ "$mycat" = "0" ];
then
/bin/echo -en "HTTP/1.1 200 OK\r\n"
/bin/echo -en "Content-Type: text/plain\r\n"
/bin/echo -en "Connection: close\r\n"
/bin/echo -en "Content-Length: 40\r\n"
/bin/echo -en "\r\n"
/bin/echo -en "MyCAT Cluster Node is synced.\r\n"
exit 0
else
/bin/echo -en "HTTP/1.1 503 Service Unavailable\r\n"
/bin/echo -en "Content-Type: text/plain\r\n"
/bin/echo -en "Connection: close\r\n"
/bin/echo -en "Content-Length: 44\r\n"
/bin/echo -en "\r\n"
/bin/echo -en "MyCAT Cluster Node is not synced.\r\n"
exit 1

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_vim_05


注意:脚本中的mycat安装目录,一定要写对了,根据自己安装的实际目录为准

6. 赋予可执行权限

#给这个脚本赋予可执行权限

chmod a+x /app/mycat/bin/mycat_status
7. 脚本验证

#执行这个脚本,返货http200说明成功,对mycat检测的

[root@node1 conf]# /app/mycat/bin/mycat_status


HTTP/1.1 200 OK
Content-Type: text/plain
Connection: close
Content-Length: 40

MyCAT Cluster Node is synced.

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_mysql_06

8. 添加端口
# 编辑services这个文件
vim /etc/services

# 在最后一行添加内容:
mycatchk 48700/tcp #mycatchk

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_vim_07

9. 重新xinetd服务时生效
# centos6.x
service xinetd restart

# centos7.x
systemctl restart xinetd.service

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_mysql_08

10. 查看48700是否启动正常
[root@node1 conf]#  netstat -nltp |grep 48700
tcp6 0 0 :::48700 :::* LISTEN 22330/xinetd

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_vim_09

11. 启动haproxy
 haproxy -f /etc/haproxy/haproxy.cfg

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_vim_10

12. 查看服务是否启动
netstat -nltp

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_可执行_11

二、mycat02节点安装和配置haproxy
2.1. 安装haproxy
yum install haproxy -y
2.2. 编辑/etc/haproxy/haproxy.cfg
vim /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application.
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file.
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1

chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon

# turn on stats unix socket
stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000

# 在这里开始添加配置
# 添加haproxy管理端口 用来监控hapeoxy的运行状态
listen admin_status
bind 0.0.0.0:48800
stats uri /admin-status
stats auth admin:admin

# 对后端mycat的监听服务
listen allmycat_service
# 对外提供的服务端口,mycat启动之后,使用8096访问mycat服务
# 后期我们的程序也是通过8096访问数据库
bind 0.0.0.0:8096
# 访问的模式tcp
mode tcp
# 日志格式采用tcplog格式
option tcplog
option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
# 负载均衡算法 轮询算法
balance roundrobin
# 对后端mycat服务配置
# 通过48700端口来进行监控
# 每隔5s监控一次,失败后重试3次
server mycat_01 192.168.92.101:8066 check port 48700 inter 5s rise 2 fall 3
server mycat_04 192.168.92.104:8066 check port 48700 inter 5s rise 2 fall 3

# 对mycat的管理端口来进行监控
listen allmycat_admin
bind 0.0.0.0:8097
mode tcp
option tcplog
option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
balance roundrobin
# mycat的管理端口
server mycat_01 192.168.92.101:9066 check port 48700 inter 5s rise 2 fall 3
server mycat_04 192.168.92.104:9066 check port 48700 inter 5s rise 2 fall 3

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_mysql_12

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_mysql_03

2.3. 使用48700端口来对mycat监控
  • 启动48700端口,需要安装xinetd
yum install xinetd
2.4. 新建mycatchk文件

在/etc/xinetd.d/目录下面,新建mycatchk文件

#新建mycatchk脚本
vim /etc/xinetd.d/mycatchk
#添加内容如下:
# default:
# description: monitor for
service mycatchk
{
flags = REUSE
socket_type = stream
port = 48700
wait = no
user = root
server =/app/mycat/bin/mycat_status
log_on_failure += USERID
disable = no
per_source = UNLIMITED
}

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_可执行_14

注释:这个地方脚本的路径在mycat的安装目录,我的安装目录为/app/mycat

2.5. 新建mycat_status脚本

#在/usr/local/bin目录下面,新建mycat_status脚本

#新建mycat_status脚本
vim /app/mycat/bin/mycat_status
#脚本内容如下
#!/bin/bash
#/usr/local/bin/mycat_status.sh
# This script checks if a mycat server is healthy running on localhost.
# return:
#
# "HTTP/1.x 200 OK\r" (if mycat is running smoothly)
#
# "HTTP/1.x 503 Internal Server Error\r" (else)
mycat=`/app/mycat/bin/mycat status |grep 'not running'| wc -l`
if [ "$mycat" = "0" ];
then
/bin/echo -en "HTTP/1.1 200 OK\r\n"
/bin/echo -en "Content-Type: text/plain\r\n"
/bin/echo -en "Connection: close\r\n"
/bin/echo -en "Content-Length: 40\r\n"
/bin/echo -en "\r\n"
/bin/echo -en "MyCAT Cluster Node is synced.\r\n"
exit 0
else
/bin/echo -en "HTTP/1.1 503 Service Unavailable\r\n"
/bin/echo -en "Content-Type: text/plain\r\n"
/bin/echo -en "Connection: close\r\n"
/bin/echo -en "Content-Length: 44\r\n"
/bin/echo -en "\r\n"
/bin/echo -en "MyCAT Cluster Node is not synced.\r\n"
exit 1

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_mysql_15

注意:脚本中的mycat安装目录,一定要写对了,根据自己安装的实际目录为准

2.6. 赋予可执行权限

#给这个脚本赋予可执行权限

chmod a+x /app/mycat/bin/mycat_status
2.7. 脚本验证

#执行这个脚本,返货http200说明成功,对mycat检测的

[root@node4 ~]#  /app/mycat/bin/mycat_status
HTTP/1.1 200 OK
Content-Type: text/plain
Connection: close
Content-Length: 40

MyCAT Cluster Node is synced.

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_mysql_16

2.8. 添加端口
# 编辑services这个文件
vim /etc/services

# 在最后一行添加内容:
mycatchk 48700/tcp #mycatchk

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_MyCat_17

2.9. 重新xinetd服务时生效
# centos6.x
service xinetd restart

# centos7.x
systemctl restart xinetd.service

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_MyCat_18

2.10. 查看48700是否启动正常
[root@node4 conf]#  netstat -nltp |grep 48700
tcp6 0 0 :::48700 :::* LISTEN 22330/xinetd

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_mysql_19

2.11. 启动haproxy
 haproxy -f /etc/haproxy/haproxy.cfg
2.12. 查看服务是否启动
netstat -nltp

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_MyCat_20

三、验证
3.1. 连接mycat

通过haproxy,使用mysql客户端连接mycat

# 这里虚拟地址待定
mysql -uapp_imooc -p -h192.168.92.101 -P8096
[root@node4 ~]# mysql -uapp_imooc -p -h192.168.92.101 -P8096
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.29-mycat-1.6.5-release-20180122220033 MyCat Server (OpenCloundDB)

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 imooc_db;
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> select count(1) from order_master;
+--------+
| COUNT0 |
+--------+
| 1 |
+--------+
1 row in set (0.28 sec)

mysql>

正常返回数据

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_vim_21

3.2. haproxy管理页面

​http://192.168.92.101:48800/admin-status​

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_vim_22

​http://192.168.92.104:48800/admin-status​

企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡_mysql_23

下一篇:企业实战_21_MyCat_keepalived 安装配置验证


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  ehrZuhofWJiC   2024年05月17日   49   0   0 mysql
  IEgV2R47Wr6T   2023年11月12日   70   0   0 数据mysqldocker
XHDrlE4yaUzu