溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Haproxy+Keepalived+MySQL實現讀均衡負載

發(fā)布時間:2020-08-11 00:12:47 來源:ITPUB博客 閱讀:124 作者:kunlunzhiying 欄目:MySQL數據庫

環(huán)境說明:

     本系統(tǒng)對數據安全性不高,TPS操作很低,主要是查詢的環(huán)境。

本方案架構圖如下:
     應用服務器對只讀的應用程序連接虛擬IP地址,連接到haproxy,然后通過haproxy將TCP協(xié)議轉移到下面的3個數據庫服務器中。
Haproxy在此做4層的TCP交換服務。keepalived為了防止haproxy單點故障。
      
為什么不用LVS?
    我們的環(huán)境服務器少,haproxy和lvs相比性能差不多,且haproxy自帶有mysql check,不需要額外的寫mysql偵測腳本。

Haproxy+Keepalived+MySQL實現讀均衡負載

1.haproxy安裝


下載地址:http://www.haproxy.org/download/1.5/src/haproxy-1.5.5.tar.gz

依賴包安裝:
#yum install gcc gcc-c++ make zlib-devel bzip2-devel openssl-devel

安裝pcre,自帶的pcre-7.8-3.1.el6.x86_64貌似版本低了,導致無法安裝proxy
#tar xzvf pcre-8.34.tar.gz
#cd pcre-8.34
#./configure --prefix=/usr \
--docdir=/usr/share/doc/pcre-8.34 \
--enable-utf --enable-unicode-properties \
--enable-pcregrep-libz --enable-pcregrep-libbz2
#make
#make check
#make install

script "/etc/keepalived/check_haproxy.sh"

interval 2
weight 2


global_defs {
router_id LVS_DEVEL
}

vrrp_instance VI_1 {
state MASTER 
interface bond0
virtual_router_id 51
priority 150 
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
 
track_script {
chk_http_port
}
 
virtual_ipaddress {
192.168.231.18 
}
}
}

配置偵測haproxy狀態(tài)腳本:
#vi /etc/keepalived/check_haproxy.sh
#!/bin/bash
A=`ps -C haproxy --no-header |wc -l`
if [ $A -eq 0 ];then
  /usr/local/sbin/haproxy -f /etc/haproxy/haproxy.conf
sleep 3

if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
  /etc/init.d/keepalived stop
fi
fi

啟動keepalived服務:
# /etc/init.d/keepalived start

添加新的協(xié)議地址:
#ip add
[root@mss-dn03 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 90:e2:ba:23:87:04 brd ff:ff:ff:ff:ff:ff
3: eth2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 90:e2:ba:23:87:05 brd ff:ff:ff:ff:ff:ff

9: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
    link/ether 6c:ae:8b:26:52:3b brd ff:ff:ff:ff:ff:ff
    inet 192.168.231.7/24 brd 192.168.231.255 scope global bond0
    inet6 fe80::6eae:8bff:fe26:523b/64 scope link tentative dadfailed 
       valid_lft forever preferred_lft forever

設置開啟自啟動:
# chkconfig  --level 0123456 keepalived on
# chkconfig --list keepalived


測試過程中發(fā)現,master宕機后,VIP需要幾十秒才能ping通,切換很慢:
arping -I bond0 -c 5 -s VIP GATEWAY
arping -I bond0 -c 5 -s 192.168.231.18 192.168.231.254



Getting started with HAProxy and MySQL replication(能實現讀寫路由):
    http://blog.yannickjaquier.com/linux/getting-started-with-haproxy-and-mysql-replication.html
Haproxy配置日志:
    http://www.linuxidc.com/Linux/2012-06/63585.htm


來自為知筆記(Wiz)


向AI問一下細節(jié)
推薦閱讀:
  1. 配置MST實現負載均衡
  2. Nginx是怎么實現負載均衡的?Nginx負載均衡實現案例

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

猜你喜歡

AI