[toc]
## 集群架構圖

參考: [MySQL高可用讀寫分離集群 ](https://www.roncoo.com/view/4)
HAProxy負責將請求分發到MyCat上,起到負載均衡的作用,同時HAProxy也能檢測到MyCat是否存活,HAProxy只會將請求轉發到存活的MyCat上。如果一臺MyCat服務器宕機,HAPorxy轉發請求時不會轉發到宕機的MyCat上,所以MyCat依然可用。
## 安裝HAProxy
HAProxy 是一款提供高可用性、負載均衡以及基于TCP(第四層)和HTTP(第七層)應用的代理軟件,支持虛擬主機,它是免費、快速并且可靠的一種解決方案。
~~~
wget https://github.com/haproxy/haproxy/archive/v2.0.0.tar.gz
tar -zxvf v2.0.0.tar.gz
cd haproxy-2.0.0/
# 安裝編譯所需的依賴包
yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
make TARGET=linux-glibc ARCH=x86_64 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 PREFIX=/usr/local/haproxy
mkdir /usr/local/haproxy
make install PREFIX=/usr/local/haproxy
# 創建配置文件目錄
mkdir -p /usr/local/haproxy/conf
mkdir -p /etc/haproxy/
cp /usr/local/src/haproxy-2.0.0/examples/option-http_proxy.cfg /usr/local/haproxy/conf/http_proxy.cfg
ln -s /usr/local/haproxy/conf/haproxy.cfg /etc/haproxy/haproxy.cfg
# 錯誤頁面配置
cp -r /usr/local/src/haproxy-2.0.0/examples/errorfiles /usr/local/haproxy/
ln -s /usr/local/haproxy/errorfiles /etc/haproxy/errorfiles
# 啟動文件,設置開機啟動
cp /usr/local/src/haproxy-2.0.0/examples/haproxy.init /etc/rc.d/init.d/haproxy
chmod +x /etc/rc.d/init.d/haproxy
ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin
chkconfig --add haproxy
chkconfig haproxy on
~~~
**docker容器化**
# 掛載主要是看原鏡像的dockerfile
docker run --name haproxy -e LANG=en_US.UTF-8 -v /showcase/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg --restart=always --net host -d haproxy:latest
如果你掛載haproxy配置并且修改了你的haproxy.cfg文件,可以使用如下命令優雅的重載配置:
docker kill -s HUP my-running-haproxy
## 安裝xinetd配置MyCat狀態檢查服務
MyCat服務主機(mycat-01、mycat-02)上需要增加mycat服務的狀態檢測腳本,并開放相應的檢測端口,以提供給HAProxy對MyCat的服務狀態進行檢測判斷。可以使用xinetd來實現,通過xinetd,HAProxy可以用httpchk來檢測MyCat的存活狀態。(xinetd即extended internet daemon,xinetd是新一代的網絡守護進程服務程序,又叫超級Internet服務器。經常用來管理多種輕量級Internet服務。xinetd提供類似于inetd+tcp\_wrapper的功能,但是更加強大和安全。xinetd為linux系統的基礎服務)
~~~
yum -y install xinetd
# 檢查/etc/xinetd.conf的末尾是否有 includedir /etc/xinetd.d ,沒有就加上
vim /etc/xinetd.conf
~~~

~~~
# 檢查 /etc/xinetd.d 目錄是否存在,不存在剛創建
ll /etc/xinetd.d/
mkdir /etc/xinetd.d/
# 增加MyCat存活狀態檢測服務配置
touch /etc/xinetd.d/mycat_status
vi /etc/xinetd.d/mycat_status
service mycat_status
{
flags = REUSE
## 使用該標記的 socket_type 為 stream,需要設置 wait 為 no
socket_type = stream ## 封包處理方式,Stream 為 TCP 數據包
port = 48700 ## 服務監聽端口
wait = no ## 表示不需等待,即服務將以多線程的方式運行
user = root ## 執行此服務進程的用戶
server =/usr/local/bin/mycat_status ## 需要啟動的服務腳本
log_on_failure += USERID ## 登錄失敗記錄的內容
disable = no ## 要啟動服務,將此參數設置為 no
}
~~~
~~~
# 添加 /usr/local/bin/mycat_status 服務腳本
touch /usr/local/bin/mycat_status
vi /usr/local/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=`/usr/local/mycat/bin/mycat status | grep 'not running' | wc -l`
if [ "$mycat" = "0" ]; then
/bin/echo -e "HTTP/1.1 200 OK\r\n"
else
/bin/echo -e "HTTP/1.1 503 Service Unavailable\r\n"
fi
# 給新增腳本賦予可執行權限
chmod a+x /usr/local/bin/mycat_status
~~~
~~~
# 在 /etc/services 中加入 mycat_status 服務
vi /etc/services
# 在末尾加入:
mycat_status 48700/tcp # mycat_status
# 保存后,重啟 xinetd 服務
service xinetd restart
# 驗證是否成功
netstat -antup|grep 48700
~~~
## HAProxy配置MyCat負載均衡集群
HAProxy支持TCP(第四層)和HTTP(第七層)應用的代理,本節課程我們使用HAProxy來做MyCat的負載均衡代理使用的是TCP模式。在4層模式下HAProxy僅在客戶端和服務器之間轉發雙向流量。HAProxy配置簡單,擁有非常不錯的服務器健康檢查功能,當其代理的后端服務器出現故障,HAProxy會自動將該服務器摘除,故障恢復后會自動將該服務器加入進來?
具體參數說明可參考官方配置文檔: http://cbonte.github.io/haproxy-dconv/2.0/configuration.html
# todo
1. 理解haproxy的配置項和使用原理
2. 制作xinetd版的mycat鏡像
主要參考:https://www.cnblogs.com/happy1983/p/9265358.html
https://www.cnblogs.com/Richardzhu/p/3344676.html
- 【mysql的編程專題①】流程控制與其他語法
- 【mysql的編程專題②】觸發器
- 【mysql的編程專題③】內置函數
- 【mysql的編程專題④】存儲過程
- 【mysql的編程專題⑤】自定義函數
- 【mysql的編程專題⑥】視圖
- 【mysql的設計與優化專題(1)】ER圖,數據建模與數據字典
- 【mysql的設計與優化專題(2)】數據中設計中的范式與反范式
- 【mysql的設計與優化專題(3)】字段類型與合理的選擇字段類型
- 【mysql的設計與優化專題(4)】表的垂直拆分和水平拆分
- 【mysql的設計與優化專題(5)】慢查詢詳解
- 【mysql的設計與優化專題(6)】mysql索引攻略
- 【Mysql問題集錦(1)】mysql不能使用innodb存儲引擎
- 【Mysql進階技巧(2)】利用mysql生成唯一序號
- 【Mysql進階技巧(1)】MySQL的多表關聯與自連接
- 【Mysql高可用架構(1)】基于日志點的主從復制
- 【Mysql高可用架構(2)】主從管理的系統視圖
- 【Mysql高可用架構(3)】基于GTID的主從復制
- 【Mysql高可用架構(4)】在線變更復制類型
- 【Mysql高可用架構(5)】多源復制(多主一從)
- 【Mysql高可用架構(6)】多線程復制
- 【Mysql高可用架構(7)】在線設置復制過濾
- 【Mysql高可用架構(8)】解決主從不一致
- 【Mysql高可用架構(9)】初識mycat以及制作mycat鏡像
- 【Mysql高可用架構(10)】mycat配置mysql讀寫分離
- MyCat 集群部署(HAProxy + MyCat)
- 常用復雜sql語句整理