上一篇:企业实战_19_MyCat初始化ZK并配置Mycat支持ZK
https://gblfy.blog.csdn.net/article/details/100087824
解决了引入多个mycat节点之间配置文件信息同步问题
如何在多个mycat之间进行负载均衡的问题?
在某一个mycat节点出现宕机之后,我们还可以在集群中,将这个宕机的节点提出到负载之外,需要引入HAPpoxy和keepalived
文章目录
- 一、mycat01节点安装和配置haproxy
- 1. 安装haproxy
- 2. 编辑/etc/haproxy/haproxy.cfg
- 3. 使用48700端口来对mycat监控
- 4. 新建mycatchk文件
- 5. 新建mycat_status脚本
- 6. 赋予可执行权限
- 7. 脚本验证
- 8. 添加端口
- 9. 重新xinetd服务时生效
- 10. 查看48700是否启动正常
- 11. 启动haproxy
- 12. 查看服务是否启动
- 二、mycat02节点安装和配置haproxy
- 2.1. 安装haproxy
- 2.2. 编辑/etc/haproxy/haproxy.cfg
- 2.3. 使用48700端口来对mycat监控
- 2.4. 新建mycatchk文件
- 2.5. 新建mycat_status脚本
- 2.6. 赋予可执行权限
- 2.7. 脚本验证
- 2.8. 添加端口
- 2.9. 重新xinetd服务时生效
- 2.10. 查看48700是否启动正常
- 2.11. 启动haproxy
- 2.12. 查看服务是否启动
- 三、验证
- 3.1. 连接mycat
- 3.2. haproxy管理页面
HAPpoxy是什么?
7层的代理服务,本身是没有状态的,因此,我们可以通过部署多台HAPpoxy服务的方式,来实现HAPpoxy的高可用,不过具体要使用哪一个HAPpoxy来提供服务呢?
这时候,我们需要使用另外一个组件keepalived,来进行判断了,这里使用keepalived呢,主要是对HAPpoxy来进行监控,并且对外提供一个虚拟ip,来访问HAPpoxy服务,以达到HAPpoxy的高可用,接下来需要对HAPpoxy进行相应的配置,把访问mycat的请求,均匀的将球分配到后面mycat节点上,把直接访问mycat的方式,修改为访问HAPpoxy提供的虚拟ip的方式,来访问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. See the
# 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. A line like the following can be added to# /etc/sysconfig/syslog## local2.* /var/log/haproxy.log#log 127.0.0.1 local2chroot /var/lib/haproxypidfile /var/run/haproxy.pidmaxconn 4000user haproxygroup haproxydaemon# turn on stats unix socketstats socket /var/lib/haproxy/stats#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaultsmode httplog globaloption httplogoption dontlognulloption http-server-closeoption forwardfor except 127.0.0.0/8option redispatchretries 3timeout http-request 10stimeout queue 1mtimeout connect 10stimeout client 1mtimeout server 1mtimeout http-keep-alive 10stimeout check 10smaxconn 3000# 在这里开始添加配置
# 添加haproxy管理端口 用来监控hapeoxy的运行状态
listen admin_statusbind 0.0.0.0:48800stats uri /admin-statusstats auth admin:admin# 对后端mycat的监听服务
listen allmycat_service# 对外提供的服务端口,mycat启动之后,使用8096访问mycat服务# 后期我们的程序也是通过8096访问数据库bind 0.0.0.0:8096# 访问的模式tcpmode tcp# 日志格式采用tcplog格式option tcplogoption 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 3server mycat_04 192.168.92.104:8066 check port 48700 inter 5s rise 2 fall 3# 对mycat的管理端口来进行监控
listen allmycat_adminbind 0.0.0.0:8097mode tcpoption tcplogoption httpchk OPTIONS * HTTP/1.1\r\nHost:\ wwwbalance roundrobin# mycat的管理端口server mycat_01 192.168.92.101:9066 check port 48700 inter 5s rise 2 fall 3server mycat_04 192.168.92.104:9066 check port 48700 inter 5s rise 2 fall 3
3. 使用48700端口来对mycat监控
- 启动48700端口,需要安装xinetd
yum install xinetd
4. 新建mycatchk文件
在/etc/xinetd.d/目录下面,新建mycatchk文件
#新建mycatchk脚本
vim /etc/xinetd.d/mycatchk
#添加内容如下:
# default: on
# description: monitor for mycat
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
}
注释:这个地方脚本的路径在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. It will
# 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
fi
注意:脚本中的mycat安装目录,一定要写对了,根据自己安装的实际目录为准
6. 赋予可执行权限
#给这个脚本赋予可执行权限
chmod a+x /app/mycat/bin/mycat_status
7. 脚本验证
#执行这个脚本,返货http200说明成功,对mycat检测的
[root@node1 conf]# /app/mycat/bin/mycat_statusHTTP/1.1 200 OK
Content-Type: text/plain
Connection: close
Content-Length: 40MyCAT Cluster Node is synced.
8. 添加端口
# 编辑services这个文件
vim /etc/services# 在最后一行添加内容:
mycatchk 48700/tcp #mycatchk
9. 重新xinetd服务时生效
# centos6.xservice xinetd restart# centos7.x
systemctl restart xinetd.service
10. 查看48700是否启动正常
[root@node1 conf]# netstat -nltp |grep 48700
tcp6 0 0 :::48700 :::* LISTEN 22330/xinetd
11. 启动haproxy
haproxy -f /etc/haproxy/haproxy.cfg
12. 查看服务是否启动
netstat -nltp
二、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. See the
# 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. A line like the following can be added to# /etc/sysconfig/syslog## local2.* /var/log/haproxy.log#log 127.0.0.1 local2chroot /var/lib/haproxypidfile /var/run/haproxy.pidmaxconn 4000user haproxygroup haproxydaemon# turn on stats unix socketstats socket /var/lib/haproxy/stats#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaultsmode httplog globaloption httplogoption dontlognulloption http-server-closeoption forwardfor except 127.0.0.0/8option redispatchretries 3timeout http-request 10stimeout queue 1mtimeout connect 10stimeout client 1mtimeout server 1mtimeout http-keep-alive 10stimeout check 10smaxconn 3000# 在这里开始添加配置
# 添加haproxy管理端口 用来监控hapeoxy的运行状态
listen admin_statusbind 0.0.0.0:48800stats uri /admin-statusstats auth admin:admin# 对后端mycat的监听服务
listen allmycat_service# 对外提供的服务端口,mycat启动之后,使用8096访问mycat服务# 后期我们的程序也是通过8096访问数据库bind 0.0.0.0:8096# 访问的模式tcpmode tcp# 日志格式采用tcplog格式option tcplogoption 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 3server mycat_04 192.168.92.104:8066 check port 48700 inter 5s rise 2 fall 3# 对mycat的管理端口来进行监控
listen allmycat_adminbind 0.0.0.0:8097mode tcpoption tcplogoption httpchk OPTIONS * HTTP/1.1\r\nHost:\ wwwbalance roundrobin# mycat的管理端口server mycat_01 192.168.92.101:9066 check port 48700 inter 5s rise 2 fall 3server mycat_04 192.168.92.104:9066 check port 48700 inter 5s rise 2 fall 3
2.3. 使用48700端口来对mycat监控
- 启动48700端口,需要安装xinetd
yum install xinetd
2.4. 新建mycatchk文件
在/etc/xinetd.d/目录下面,新建mycatchk文件
#新建mycatchk脚本
vim /etc/xinetd.d/mycatchk
#添加内容如下:
# default: on
# description: monitor for mycat
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
}
注释:这个地方脚本的路径在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. It will
# 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
fi
注意:脚本中的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: 40MyCAT Cluster Node is synced.
2.8. 添加端口
# 编辑services这个文件
vim /etc/services# 在最后一行添加内容:
mycatchk 48700/tcp #mycatchk
2.9. 重新xinetd服务时生效
# centos6.xservice xinetd restart# centos7.x
systemctl restart xinetd.service
2.10. 查看48700是否启动正常
[root@node4 conf]# netstat -nltp |grep 48700
tcp6 0 0 :::48700 :::* LISTEN 22330/xinetd
2.11. 启动haproxy
haproxy -f /etc/haproxy/haproxy.cfg
2.12. 查看服务是否启动
netstat -nltp
三、验证
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 -ADatabase changed
mysql> select count(1) from order_master;
+--------+
| COUNT0 |
+--------+
| 1 |
+--------+
1 row in set (0.28 sec)mysql>
正常返回数据
3.2. haproxy管理页面
http://192.168.92.101:48800/admin-status
http://192.168.92.104:48800/admin-status
下一篇:企业实战_21_MyCat_keepalived 安装配置验证
https://gblfy.blog.csdn.net/article/details/100103518