Haproxy+Keepalived+MySQL實現讀均衡負載
環境說明:
本系統對資料安全性不高,TPS操作很低,主要是查詢的環境。
本方案架構圖如下:
應用伺服器對只讀的應用程式連線虛擬IP地址,連線到haproxy,然後透過haproxy將TCP協議轉移到下面的3個資料庫伺服器中。
Haproxy在此做4層的TCP交換服務。keepalived為了防止haproxy單點故障。
為什麼不用LVS?
我們的環境伺服器少,haproxy和lvs相比效能差不多,且haproxy自帶有mysql check,不需要額外的寫mysql偵測指令碼。
1.haproxy安裝
下載地址:
依賴包安裝:
#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
haproxy安裝:
#tar -xzvf haproxy-1.5.5.tar.gz
#cd haproxy-1.5.5
# make TARGET=linux26 USE_STATIC_PCRE=1 \
USE_REGPARM=1 USE_LINUX_TPROXY=1 USE_OPENSSL=1 USE_ZLIB=1 ARCH=x86_64
# make install
--編輯啟動檔案
# vi /etc/init.d/haproxy --新增以下內容
#!/bin/sh
#
# custom haproxy init.d script, by Mattias Geniar
#
# haproxy starting and stopping the haproxy load balancer
#
# chkconfig: 345 55 45
# description: haproxy is a TCP loadbalancer
# probe: true
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -f /usr/local/sbin/haproxy ] || exit 0
[ -f /etc/haproxy/haproxy.conf ] || exit 0
# Define our actions
checkconfig() {
# Check the config file for errors
/usr/local/sbin/haproxy -c -q -f /etc/haproxy/haproxy.conf
if [ $? -ne 0 ]; then
echo "Errors found in configuration file."
return 1
fi
# We're OK!
return 0
}
start() {
# Check config
/usr/local/sbin/haproxy -c -q -f /etc/haproxy/haproxy.conf
if [ $? -ne 0 ]; then
echo "Errors found in configuration file."
return 1
fi
echo -n "Starting HAProxy: "
daemon /usr/local/sbin/haproxy -D -f /etc/haproxy/haproxy.conf -p /var/run/haproxy.pid
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/haproxy
return $RETVAL
}
stop() {
echo -n "Shutting down HAProxy: "
killproc haproxy -USR1
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/haproxy
[ $RETVAL -eq 0 ] && rm -f /var/run/haproxy.pid
return $RETVAL
}
restart() {
/usr/local/sbin/haproxy -c -q -f /etc/haproxy/haproxy.conf
if [ $? -ne 0 ]; then
echo "Errors found in configuration file."
return 1
fi
stop
start
}
check() {
/usr/local/sbin/haproxy -c -q -V -f /etc/haproxy/haproxy.conf
}
rhstatus() {
status haproxy
}
reload() {
/usr/local/sbin/haproxy -c -q -f /etc/haproxy/haproxy.conf
if [ $? -ne 0 ]; then
echo "Errors found in configuration file."
return 1
fi
echo -n "Reloading HAProxy config: "
/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.conf -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid)
success $"Reloading HAProxy config: "
echo
}
# Possible parameters
case "$1" in
start)
start
;;
stop)
stop
;;
status)
rhstatus
;;
restart)
restart
;;
reload)
reload
;;
checkconfig)
check
;;
*)
echo "Usage: haproxy {start|stop|status|restart|reload|checkconfig}"
exit 1
esac
exit 0
授權:
#chmod +x /etc/init.d/haproxy
編輯haproxy配置檔案:
#mkdir /etc/haproxy
#vi /etc/haproxy/haproxy.conf
defaults
log global
mode http
retries 3
option redispatch
maxconn 4096
timeout connect 50000
timeout client 50000
timeout server 50000
listen mysql_proxy 0.0.0.0:3307
mode tcp
balance roundrobin
option tcpka
option httpchk
option mysql-check user haproxy #在mysql中建立無任何許可權使用者haproxy,且無密碼
server mysqldb1 192.168.231.8:3306 weight 1
server mysqldb2 192.168.231.9:3306 weight 3
server mysqldb3 192.168.231.11:3306 weight 3
listen stats *:8080
mode http
option httpclose
balance roundrobin
stats uri /
stats realm Haproxy\ Statistics
stats auth admin:admin
啟動並檢查:
#service haproxy start
# netstat -plantu | grep 3307
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 23626/haproxy
建立100次短連線測試:
其中db2和db3路由了43次,db1路由了14次
2.keepalived安裝
下載地址:
#tar zxvf keepalived-1.2.13.tar.gz
#cd keepalived-1.2.13
# ln -s /usr/src/kernels/2.6.32-220.el6.x86_64 /usr/src/linux
#./configure --prefix=/ --mandir=/usr/local/share/man/ --with-kernel-dir=/usr/src/kernels/2.6.32-220.el6.x86_64
#make
#make install
# cd /etc/keepalived/
配置引數檔案:
# mv keepalived.conf keepalived.conf.default
#vi keepalived.conf
! Configuration File for keepalived
vrrp_script chk_http_port {
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狀態指令碼:
#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
新增新的協議地址:
#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: eth1: <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
測試過程中發現,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(能實現讀寫路由):
Haproxy配置日誌:
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28939273/viewspace-2051854/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- nginx實現負載均衡Nginx負載
- GRPC 負載均衡實現RPC負載
- HaProxy 實現 MySQL 負載均衡MySql負載
- Ribbon實現負載均衡負載
- Oracle負載均衡實現方式Oracle負載
- Nginx + IIS 實現負載均衡Nginx負載
- Mycat實現mysql的負載均衡讀寫分離MySql負載
- 基於Nginx的軟體負載均衡實現解讀Nginx負載
- dubbo(三):負載均衡實現解析負載
- Nginx 負載均衡原理解讀Nginx負載
- orleans叢集及負載均衡實現負載
- Nginx如何實現四層負載均衡?Nginx負載
- Haproxy搭建 Web 群集實現負載均衡Web負載
- nginx+tomcat實現負載均衡NginxTomcat負載
- Nginx實現簡單的負載均衡Nginx負載
- Python實現簡單負載均衡Python負載
- 採用Atlas+Keepalived實現MySQL讀寫分離、讀負載均衡MySql負載
- SpringCloud微服務中使用RestTemplate+Ribbon實現負載均衡(實現方法+實現原理+替換負載均衡策略)SpringGCCloud微服務REST負載
- 一文讀懂“負載均衡”負載
- Keepalived實現Nginx負載均衡高可用Nginx負載
- 伺服器負載均衡原理及實現伺服器負載
- nginx實現兩臺服務負載均衡Nginx負載
- jmeter壓力測試實現負載均衡JMeter負載
- RHEL 7配置HAProxy實現Web負載均衡Web負載
- Docker Compose+nginx實現負載均衡DockerNginx負載
- HAproxy&keepalived 實現tcp負載均衡TCP負載
- SQL Server資料庫實現負載均衡SQLServer資料庫負載
- Linux負載均衡雙機實現文件Linux負載
- 用Nginx實現Session共享的均衡負載NginxSession負載
- 在Linux中,如何實現負載均衡?Linux負載
- Nginx如何實現負載均衡釋出策略?Nginx負載
- 負載均衡負載
- 超實用:實現負載均衡技術的方式負載
- Nginx 高階篇(三)負載均衡的實現Nginx負載
- .Net Core+Nginx實現專案負載均衡Nginx負載
- SAP 應用服務負載均衡的實現負載
- 六種實現負載均衡技術的方式負載
- 淺談負載均衡演算法與實現負載演算法