原創做者: 冒飛mysql
文章摘要:linux
DBLE 是一款企業級的開源分佈式中間件,江湖人送外號 「MyCat Plus」。爲了分擔 DBLE 的集羣管理壓力同時避免單點故障,須要爲 DBLE 尋找一個負載均衡方案。咱們找來了 Lvs+Keepalived 爲 DBLE 實現負載均衡。git
本文概要:github
1、負載均衡環境介紹算法
1. 部署架構sql
2. 架構軟件版本shell
2、環境搭建數據庫
1. 安裝 DBLE 2. 安裝 MySQL 3. 安裝 Lvs+Keepalived後端
3、負載均衡實驗服務器
1. 場景一:DBLE被停掉到恢復過程當中的負載均衡 2. 場景二:DBLE從網絡斷開到恢復的過程當中的負載均衡 3. 場景三:整套環境在壓力測試下負載均衡的穩定性
4、環境搭建過程當中可能遇到的問題
外部對虛擬IP地址發出請求,負載均衡層一主一備避免單點故障,而後由 Lvs+Keepalived 實現 DBLE 層的負載均衡,DBLE 層用指定規則將數據在 MySQL 集羣中分片存儲。
操做系統:Ubuntu 17.10 內核:Linux R820-08 4.13.0-32-generic x86_64 GNU/Linux 負載均衡:ipvsadm:v1.2.1 Keepalived:v1.3.2 (06/17,2017) 數據庫:MySQL 5.7.13 壓力測試工具:sysbench v1.0 DBLE:5.6.29-dble-2.19.01.0
shell>wget -c https://github.com/actiontech/dble/releases/download/2.19.01.0%2Ftag/actiontech-dble-2.19.01.0.tar.gz
shell>tar xvf actiontech-dble-2.19.01.0.tar.gz
庫名:lvs_test ;表名:sbtest1;分片數:2;規則:簡單hash 每臺數據庫對應1個分片,具體配置方式可參考《開源分佈式中間件DBLE快速入門指南》
shell>$install/bin/dble start
shell>apt install -y ipvsadm keepalived
shell>vi /etc/keepalived/keepalived.conf
其中重要配置說明:
本架構採用的是主備架構模式,Keepalived的配置包含master跟slave兩個部分。
vrrpinstance模塊用來定義虛擬路由器,其中部分參數說明以下:
state:服務器的初始狀態,指定爲master或slave,但實際master的選舉是按照下面配置的優先級來定,優先級高的爲master
interface:實例綁定的網卡,由於在配置虛擬IP的時候必須是在已有的網卡上添加的
virtual router id:相同的VRID爲一個組
priority:設置本節點的優先級,優先級高的爲master
advert int:檢查的時間間隔
virtualipaddress:虛擬ip地址
virtualserver模塊,是Lvs+Keepalived中Lvs部分的配置,指定real server所屬的virtual server:VIP:Vport
delayloop:服務輪詢的時間隔
lbalgo:LVS調度算法,本次測試使用的是rr
lbkind:LVS集羣模式,本次測試使用的是DR
protocol:健康檢查方式
realserver:後端真實節點主機的權重等設置
weight :每臺的權重
HTTPGET:健康檢查方式
connecttimeout:鏈接超時時間
nbgetretry:重連次數
delaybefore_retry:重連間隔
keepalived.conf(master)
global_defs { router_id LVS_MASTER vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_instance VI_1 { state MASTER interface eno3 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.186.17.150 } } virtual_server 10.186.17.150 8066 { delay_loop 6 lb_algo rr lb_kind DR protocol TCP real_server 10.186.17.105 8066 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 8066 } } real_server 10.186.17.107 8066 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 8066 } } }
keepalived.conf(slave)
global_defs { router_id LVS_SLAVE vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_instance VI_1 { state BACKUP interface eno3 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.186.17.150 } } virtual_server 10.186.17.150 8066 { delay_loop 6 lb_algo rr lb_kind DR protocol TCP real_server 10.186.17.105 8066 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 8066 } } real_server 10.186.17.107 8066 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 8066 } } }
shell>systemctl start keepalived
shell>systemctl status keepalived
master狀態
keepalived.service - Keepalive Daemon (LVS and VRRP) Loaded: loaded (/lib/systemd/system/keepalived.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2019-03-11 15:17:10 CST; 20h ago Process: 13289 ExecStart=/usr/sbin/keepalived $DAEMON_ARGS (code=exited, status=0/SUCCESS) Main PID: 13300 (keepalived) Tasks: 3 (limit: 14745) Memory: 3.7M CPU: 11.980s CGroup: /system.slice/keepalived.service ├─13300 /usr/sbin/keepalived ├─13304 /usr/sbin/keepalived └─13306 /usr/sbin/keepalived Mar 11 15:17:16 R820-02 Keepalived_healthcheckers[13304]: Removing service [10.186.17.107]:8066 from VS [10.186.17.150]:8066 Mar 11 15:17:19 R820-02 Keepalived_healthcheckers[13304]: TCP connection to [10.186.17.105]:8066 failed. Mar 11 15:17:19 R820-02 Keepalived_healthcheckers[13304]: Check on service [10.186.17.105]:8066 failed after 1 retry. Mar 11 15:17:19 R820-02 Keepalived_healthcheckers[13304]: Removing service [10.186.17.105]:8066 from VS [10.186.17.150]:8066 Mar 11 15:17:19 R820-02 Keepalived_healthcheckers[13304]: Lost quorum 1-0=1 > 0 for VS [10.186.17.150]:8066 Mar 11 15:28:55 R820-02 Keepalived_healthcheckers[13304]: TCP connection to [10.186.17.107]:8066 success. Mar 11 15:28:55 R820-02 Keepalived_healthcheckers[13304]: Adding service [10.186.17.107]:8066 to VS [10.186.17.150]:8066 Mar 11 15:28:55 R820-02 Keepalived_healthcheckers[13304]: Gained quorum 1+0=1 <= 1 for VS [10.186.17.150]:8066 Mar 11 15:40:58 R820-02 Keepalived_healthcheckers[13304]: TCP connection to [10.186.17.105]:8066 success. Mar 11 15:40:58 R820-02 Keepalived_healthcheckers[13304]: Adding service [10.186.17.105]:8066 to VS [10.186.17.150]:8066
slave狀態
keepalived.service - Keepalive Daemon (LVS and VRRP) Loaded: loaded (/lib/systemd/system/keepalived.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2019-03-11 16:13:35 CST; 19h ago Process: 38895 ExecStart=/usr/sbin/keepalived $DAEMON_ARGS (code=exited, status=0/SUCCESS) Main PID: 38909 (keepalived) Tasks: 3 (limit: 14745) Memory: 3.5M CPU: 11.928s CGroup: /system.slice/keepalived.service ├─38909 /usr/sbin/keepalived ├─38910 /usr/sbin/keepalived └─38912 /usr/sbin/keepalived Mar 11 16:13:35 R820-01 Keepalived_vrrp[38912]: Registering Kernel netlink command channel Mar 11 16:13:35 R820-01 Keepalived_healthcheckers[38910]: Unknown keyword 'nb_get_retry' Mar 11 16:13:35 R820-01 Keepalived_healthcheckers[38910]: Unknown keyword 'nb_get_retry' Mar 11 16:13:35 R820-01 Keepalived_vrrp[38912]: Registering gratuitous ARP shared channel Mar 11 16:13:35 R820-01 Keepalived_vrrp[38912]: Opening file '/etc/keepalived/keepalived.conf'. Mar 11 16:13:35 R820-01 Keepalived_healthcheckers[38910]: Using LinkWatch kernel netlink reflector... Mar 11 16:13:35 R820-01 Keepalived_healthcheckers[38910]: Activating healthchecker for service [10.186.17.105]:8066 Mar 11 16:13:35 R820-01 Keepalived_healthcheckers[38910]: Activating healthchecker for service [10.186.17.107]:8066 Mar 11 16:13:35 R820-01 Keepalived_vrrp[38912]: Using LinkWatch kernel netlink reflector... Mar 11 16:13:35 R820-01 Keepalived_vrrp[38912]: VRRP_Instance(VI_1) Entering BACKUP STATE
shell>/sbin/ifconfig eno3:0 10.186.17.150 broadcast 10.186.17.150 netmask 255.255.255.255 up shell>/sbin/route add -host 10.186.17.150 dev eno3:0 shell>sysctl -w net.ipv4.conf.all.arp_ignore=1 shell>sysctl -w net.ipv4.conf.all.arp_announce=2 shell>sysctl -w net.ipv4.conf.lo.arp_ignore=1 shell>sysctl -w net.ipv4.conf.lo.arp_announce=2 shell>sysctl -p
在負載均衡服務器上執行:ipvsadm -ln
查看默認超時時間
shell>ipvsadm -L --timeout
說明:一條TCP的鏈接通過Lvs後,Lvs會把記錄保存15分鐘,容易對測試過程當中的觀測過程形成不便。
調整超時時間
shell>ipvsadm --set 1 2 1
數據準備:用sysbench用具的插入數據腳本爲兩臺MySQL的lvs_test庫建立數據表並插入數據。測試數據一共10000條,根據分片數和規則致使數據各5000條。
shell>cd $sysbench/bin
//進入sysbench安裝目錄
shell>./sysbench --mysql-db=lvs_test --db-driver=mysql --mysql-host=10.186.17.150 \ --mysql-port=8066 --mysql-table-engine=innodb --mysql-user=action \ --oltp_auto_inc=off --mysql-password=action --test=../db/insert.lua \ --oltp_tables_count=1 --oltp-table-size=10000 prepare
shell>./sysbench --mysql-db=lvs_test --mysql-host=10.186.17.150 \ --mysql-port=8066 --mysql-table-engine=innodb --mysql-user=action \ --mysql-password=action --test=../db/select.lua --oltp_tables_count=1 \ --oltp-table-size=10000 --num-threads=200 --report-interval=1 \ --default-charset=utf8 --max-time=600000 --max-requests=0 \ --percentile=95 --mysql-ignore-errors=2013 run
shell>ipvsadm -ln
結論:存活鏈接各100個,符合比重1:1的預期。
進入DBLE目錄執行:
shell>$install/bin/dble stop
檢測DBLE狀態:
shell>$install/bin/dble status
負載均衡狀況:
結論:鏈接異常的DBLE1被踢出,全部的流量所有打到DBLE2上
進入DBLE目錄執行:
shell>$install/bin/dble start
檢測DBLE狀態:
shell>$install/bin/dble status
負載均衡狀況:
結論:DBLE1被從新加進來,但原有的存活的長鏈接不會被轉移到DBLE1上
結束原有的sysbench壓力後,重複步驟一
負載均衡狀況:
結論:該場景中DBLE實現負載均衡。
sysbench命令
shell>./sysbench --mysql-db=lvs_test --mysql-host=10.186.17.150 \ --mysql-port=8066 --mysql-table-engine=innodb --mysql-user=action \ --mysql-password=action --test=../db/select.lua --oltp_tables_count=1 \ --oltp-table-size=10000 --num-threads=200 --report-interval=1 \ --default-charset=utf8 --max-time=600000 --max-requests=0 \ --percentile=95 --mysql-ignore-errors=2013 run
命令:ipvsadm -ln
結論:存活鏈接各100個,符合比重1:1的預期。
進入DBLE1環境執行:
shell>iptables -A INPUT -p tcp --dport 8066 -j DROP
負載均衡狀況:
結論:鏈接異常的DBLE1被踢出,全部的流量所有打到DBALE2上。
進入DBLE1環境執行:
shell>iptables -D INPUT -p tcp --dport 8066 -j DROP
負載均衡狀況:
結論:DBLE1被從新加進來,但原有存活的長鏈接不會被轉移到DBLE1上。
結束原有的sysbench壓力後,重複步驟一
負載均衡狀況:
結論:該場景中DBLE實現負載均衡。
穩定性sysbench命令
shell>nohup ./sysbench --mysql-db=lvs_test --mysql-host=10.186.17.150 \ --mysql-port=8066 --mysql-table-engine=innodb --mysql-user=action \ --mysql-password=action --test=../db/select.lua --oltp_tables_count=1 \ --oltp-table-size=10000 --num-threads=1024 --oltp-read-only=off \ --report-interval=1 --default-charset=utf8 --max-time=950400 \ --max-requests=0 --percentile=95 run &
測試結果:
結論:11天的穩定性測試沒有出現異常, Lvs+Keepalived完成了對DBLE的負載均衡實踐,適應性及穩定性獲得了驗證。
總結:Lvs+Keepalived實現了DBLE負載均衡方案,壓力測試中配合過程無異常,組件之間適應性好,總體架構穩定。
1. Keepalived slave報錯
緣由:缺乏包:libipset.so.3 解決方案:apt install ipset
2. 注意防火牆和selinux可能引發的網絡連通性的問題