企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡
上一篇:企業實戰_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
| 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 -y2. 編輯/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
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_status7. 腳本驗證
#執行這個腳本,返貨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 #mycatchk9. 重新xinetd服務時生效
# centos6.xservice xinetd restart# centos7.x systemctl restart xinetd.service10. 查看48700是否啟動正常
[root@node1 conf]# netstat -nltp |grep 48700 tcp6 0 0 :::48700 :::* LISTEN 22330/xinetd11. 啟動haproxy
haproxy -f /etc/haproxy/haproxy.cfg12. 查看服務是否啟動
netstat -nltp二、mycat02節點安裝和配置haproxy
2.1. 安裝haproxy
yum install haproxy -y2.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 32.3. 使用48700端口來對mycat監控
- 啟動48700端口,需要安裝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_status2.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 #mycatchk2.9. 重新xinetd服務時生效
# centos6.xservice xinetd restart# centos7.x systemctl restart xinetd.service2.10. 查看48700是否啟動正常
[root@node4 conf]# netstat -nltp |grep 48700 tcp6 0 0 :::48700 :::* LISTEN 22330/xinetd2.11. 啟動haproxy
haproxy -f /etc/haproxy/haproxy.cfg2.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
總結
以上是生活随笔為你收集整理的企业实战_20_MyCat使用HAPpoxy对Mycat负载均衡的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MyBatisPlus_AR篇_入门试炼
- 下一篇: 创建Git仓库的三种形式