本文通過一個具體的實例詳細講解和演示HAProxy下虛擬主機的實現(xiàn)過程以及HAProxy是如何實現(xiàn)負載均衡和故障轉(zhuǎn)移的。
1、通過HAProxy的ACL規(guī)則配置虛擬主機
下面通過HAProxy的ACL功能配置一套基于虛擬主機的負載均衡系統(tǒng),這里的操作系統(tǒng)環(huán)境為CentOS release 6.3,HAProxy版本為haproxy-1.4.24,要實現(xiàn)的功能如圖1所示。
圖1 基于虛擬主機的HAPro y應(yīng)用實例
本實例有一個電商網(wǎng)站服務(wù)器群、一個論壇服務(wù)器群、一個博客服務(wù)器群和默認服務(wù)器群,4個服務(wù)器群都由多臺服務(wù)器組成,而4個服務(wù)器群又組成了一個應(yīng)用服務(wù)器群組,在每個服務(wù)器群的前端有一個基于HAProxy的負載均衡調(diào)度器,整個應(yīng)用架構(gòu)要實現(xiàn)的功能為:當客戶端通過域名www.tb.com或tb.com訪問時,HAProxy將請求提交到電商網(wǎng)站服務(wù)器群,進而實現(xiàn)電商網(wǎng)站的負載均衡;當客戶端通過域名bbs.tb.com訪問時就將請求調(diào)度到論壇服務(wù)器群,實現(xiàn)論壇的負載均衡;當客戶端通過blog.tb.com訪問時則將請求調(diào)度到博客服務(wù)器群中,實現(xiàn)博客的負載均衡;如果客戶端通過除上面三種方式外的任意方式請求服務(wù)時,就將請求調(diào)度到缺省服務(wù)器群。
要實現(xiàn)上述功能,如果使用四層的LVS負載均衡器,則需要一個代理服務(wù)器配合LVS負載均衡器才能實現(xiàn),而通過HAProxy實現(xiàn)時,僅需要一個HAProxy負載調(diào)度器再結(jié)合ACL規(guī)則即可輕松實現(xiàn)。
(1)配置HAProxy
HAProxy的安裝非常簡單,這里直接進入HAProxy的配置過程,配置好的文件內(nèi)容如下:
global
log 127.0.0.1 local0 info
maxconn 4096
user nobody
group nobody
daemon
nbproc 1
pidfile /usr/local/haproxy/logs/haproxy.pid
defaults
mode http
retries 3
timeout connect 5s
timeout client 30s
timeout server 30s
timeout check 2s
listen admin_stats
bind 0.0.0.0:19088
mode http
log 127.0.0.1 local0 err
stats refresh 30s
stats uri /haproxy-status
stats realm welcome login\\ Haproxy
stats auth admin:xxxxxx
stats auth admin1:xxxxxx
stats hide-version
stats admin if TRUE
frontend www
bind *:80
mode http
option httplog
option forwardfor
log global
acl host_www hdr_reg(host) -i ^(www.tb.com|tb.com)
acl host_bbs hdr_dom(host) -i bbs.tb.com
acl host_blog hdr_beg(host) -i blog.
use_backend server_www if host_www
use_backend server_bbs if host_bbs
use_backend server_blog if host_blog
default_backend server_default
backend server_default
mode http
option redispatch
option abortonclose
balance roundrobin
cookie SERVERID
option httpchk GET /check_status.html
server default1 192.168.88.90:8000 cookie default1 weight 3 check inter 2000 rise 2 fall 3
server default2192.168.88.91:8000 cookie default2weight 3 check inter 2000 rise 2 fall 3
backend server_www
mode http
option redispatch
option abortonclose
balance source
cookie SERVERID
option httpchk GET /check_status.jsp
server www1 192.168.88.80:80 cookie www1 weight 6 check inter 2000 rise 2 fall 3
server www2 192.168.88.81:80 cookie www2 weight 6 check inter 2000 rise 2 fall 3
server www3 192.168.88.82:80 cookie www3 weight 6 check inter 2000 rise 2 fall 3
backend server_bbs
mode http
option redispatch
option abortonclose
balance source
cookie SERVERID
option httpchk GET /check_status.php
server bbs1 192.168.88.83:8080 cookie bbs1 weight 8 check inter 2000 rise 2 fall 3
server bbs2 192.168.88.84:8090 cookie bbs2 weight 8 check inter 2000 rise 2 fall 3
backend server_blog
mode http
option redispatch
option abortonclose
balance roundrobin
cookie SERVERID
option httpchk GET /check_blog.php
server blog1 192.168.88.85:80 cookie blog1 weight 5 check inter 2000 rise 2 fall 3
server blog2 192.168.88.86:80 cookie blog2 weight 5 check inter 2000 rise 2 fall 3
這里重點看一下HAProxy配置文件中frontend部分中關(guān)于ACL配置部分的內(nèi)容,這個是實現(xiàn)虛擬主機的核心配置部分。另外,這個配置文件定義了server_www、server_bbs、server_blog、server_default4個backend,分別對應(yīng)上面的4個服務(wù)器群,對于server_www群和server_bbs群,采用了基于請求源IP的負載均衡算法,其他兩個群均采用基于權(quán)重進行輪叫調(diào)度的算法。這也是根據(jù)Web應(yīng)用的特點而定的。每個backend中都定義了httpchk的檢測方式,因此要保證這里指定的URL頁面是可訪問到的。
為了驗證負載均衡的功能,這里需要將后端真實服務(wù)器做一個訪問標記,這個架構(gòu)一共加入了9臺后端真實服務(wù)器,共分為四組,這里將server_www的三臺后端服務(wù)器默認的Web頁面設(shè)置如下:
[root@www1 app]# echo "This is www1192.168.88.80" > /var/www/html/index.html
[root@www2 app]# echo "This is www2 192.168.88.81" > /var/www/html/index.html
[root@www3 app]# echo "This is www3 192.168.88.82" > /var/www/html/index.html
同理,將server_bbs的兩臺后端服務(wù)器默認的Web頁面設(shè)置如下:
[root@bbs1 app]# echo "This is bbs1 192.168.88.83" > /var/www/html/index.html
[root@bbs2 app]# echo "This is bbs2 192.168.88.84" > /var/www/html/index.html
接著,將server_blog的兩臺后端服務(wù)器默認的Web頁面設(shè)置如下:
[root@blog1 app]# echo "This is bbs1 192.168.88.85" > /var/www/html/index.html
[root@blog2 app]# echo "This is bbs2 192.168.88.86" > /var/www/html/index.html
最后,將server_default的兩臺后端服務(wù)器默認的Web頁面設(shè)置如下:
[root@default1 app]# echo "This is default1 192.168.88.90" > /var/www/html/index.html
[root@default2 app]# echo "This isdefault2 192.168.88.91" > /var/www/html/index.html
這樣就為接下來的測試做好了準備。
(2)啟動HAProxy
HAProxy安裝完成后,會在安裝根目錄的sbin目錄下生成一個可執(zhí)行的二進制文件haproxy,對HAProxy的啟動、關(guān)閉、重啟等維護操作都是通過這個二進制文件實現(xiàn)的,執(zhí)行“haproxy -h”命令即可得到此文件的用法。
haproxy[ -f <配置文件> ][ -vdVD ][ -n 并發(fā)連接總數(shù) ][ -N 默認的連接數(shù) ]
haproxy常用的參數(shù)以及含義如表1所示。
表1 haproxy常用參數(shù)及含義
介紹完HAProxy常用的參數(shù)后,下面開始啟動HAProxy,操作如下:
[root@haproxy-server haproxy]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
如果要關(guān)閉HAProxy,執(zhí)行如下命令即可:
[root@haproxy-server haproxy]# killall -9 haproxy
如果要平滑重啟HAProxy,可執(zhí)行如下命令:
[root@haproxy-server haproxy]#/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg -st `cat /usr/local/haproxy/logs/haproxy.pid`
有時候為了管理和維護方便,也可以把HAProxy的啟動與關(guān)閉寫成一個獨立的腳本,這里給出一個例子,腳本內(nèi)容如下:
#!/bin/bash
#config:/usr/local/haproxy/conf/haproxy.cfg
#pidfile:/usr/local/haproxy/logs/haproxy.pid
#Source function library
. /etc/rc.d/init.d/functions
#Source networking configuration
. /etc/sysconfig/network
#Check that networking is up
[ "$NETWORKING" = "no" ] && exit 0
config="/usr/local/haproxy/conf/haproxy.cfg"
exec="/usr/local/haproxy/sbin/haproxy"
prog=$(basename $exec)
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
lockfile=/var/lock/subsys/haproxy
check() {
$exec -c -V -f $config
}
start() {
$exec -c -V -f $config
if [ $? -ne 0 ];then
echo "Erros in configuration file, check with $prog check."
return 1
fi
echo -n $"Starting $prog:"
#start it up here, usually something like "daemon $exec"
daemon $exec -D -f $config -p /usr/local/haproxy/logs/$prog.pid
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n "Stopping $prog:"
# stop it here, often "killproc $prog"
killproc $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
$exec -c -q -f $config
if [ $? -ne 0 ];then
echo "Errors in configuration file, check with $prog check."
return 1
fi
stop
start
}
reload() {
$exec -c -q -f $config
if [ $? -ne 0 ];then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Reloading $prog:"
$exec -D -f $config -p /usr/local/haproxy/logs/$prog.pid -sf $(cat /usr/local/haproxy/logs/$prog.pid)
retval=$?
echo
return $retval
}
force_reload() {
restart
}
fdr_status() {
status $prog
}
case $1 in
start|stop|restart|reload)
$1
;;
force-reload)
force_reload
;;
checkconfig)
check
;;
status)
fdr_status
;;
condrestart|try-restart)
[ ! -f $lockfile ] || restart
;;
*)
echo $"Usage: $0 {start|stop|status|checkconfig|restart|try-restart|reload|force_reload}"
exit 2
esac
將此腳本命名為haproxy,放在系統(tǒng)的/etc/init.d/目錄下,下面是此腳本的用法:
[root@haproxy-server logs]#/etc/init.d/haproxyUsage:/etc/init.d/haproxy{start|stop|status|checkconfig|restart|try-restart reload|force-reload}
HAProxy啟動后,就可以測試HAProxy所實現(xiàn)的各種功能了。
2、測試HAProxy實現(xiàn)虛擬主機和負載均衡功能
首先通過不同IP的客戶端以www.tb.com或者tb.com域名訪問網(wǎng)站,如果HAProxy運行正常,并且ACL規(guī)則設(shè)置正確,server_www的三臺后端服務(wù)器默認的Web頁面信息將會依次出現(xiàn),這說明HAProxy對電商網(wǎng)站實現(xiàn)了負載均衡,同時,不會出現(xiàn)其他后端服務(wù)器的默認Web頁面信息,說明ACL規(guī)則生效,實現(xiàn)虛擬主機功能。
同理,當通過不同IP的客戶端以bbs.tb.com訪問網(wǎng)站時,server_bbs的兩臺后端服務(wù)器默認的Web頁面信息將輪換出現(xiàn)。這表示實現(xiàn)了論壇的負載均衡功能,同時,不會出現(xiàn)其他后端服務(wù)器的默認Web頁面信息,說明ACL規(guī)則生效,實現(xiàn)虛擬主機功能。
用同樣的方法可以驗證blog.tb.com是否實現(xiàn)了虛擬主機功能以及負載均衡功能。最后,當通過HAProxy服務(wù)器的IP或者其他方式訪問時,訪問請求將被調(diào)度到server_default指定的兩臺后端真實服務(wù)器上。
3、測試HAProxy的故障轉(zhuǎn)移功能
測試HAProxy的故障轉(zhuǎn)移功能也非常簡單,這里假定將server_www組的一臺后端服務(wù)器192.168.88.82的httpd服務(wù)停止,那么當通過www.tb.com或者tb.com域名訪問網(wǎng)站時,這個失效的節(jié)點將不會被訪問到,因為當httpd服務(wù)被停止后,HAProxy通過httpchk方式將立刻檢測到此節(jié)點無法返回數(shù)據(jù),從而屏蔽此節(jié)點對外提供服務(wù)的功能,這樣就實現(xiàn)了故障轉(zhuǎn)移功能。通過類似的方法可以測試其他節(jié)點的應(yīng)用。
4、使用HAProxy的Web監(jiān)控平臺
雖然HAProxy實現(xiàn)了服務(wù)的故障轉(zhuǎn)移功能,但是在主機或者服務(wù)出現(xiàn)故障的時候,并不能發(fā)出通知告知運維人員,這對于及時性要求很高的業(yè)務(wù)系統(tǒng)來說,是非常不便的。不過,HAProxy似乎也考慮到了這一點,在新的版本中HAProxy推出了一個基于Web的監(jiān)控平臺,通過這個平臺可以查看此集群系統(tǒng)所有后端服務(wù)器的運行狀態(tài),在后端服務(wù)或服務(wù)器出現(xiàn)故障時,監(jiān)控頁面會通過不同的顏色來展示故障信息,這在很大程度上解決了后端服務(wù)器故障報警的問題,運維人員可通過監(jiān)控這個頁面來第一時間發(fā)現(xiàn)節(jié)點故障,進而修復故障,如圖2所示。
圖2 HAProxy的Web監(jiān)控頁面
在這個監(jiān)控頁面中,詳細記錄了HAProxy中配置的frontend、backend等信息。在backend中有各臺后端真實服務(wù)器的運行狀態(tài),正常情況下,所有后端服務(wù)器都以淺綠色展示,當某臺后端服務(wù)器出現(xiàn)故障時,將以深橙色顯示。其實每種顏色代表什么狀態(tài),在上面這個圖中都有詳細說明。
在這個監(jiān)控頁面中,還可以執(zhí)行關(guān)閉自動刷新、隱藏故障狀態(tài)的節(jié)點、手動刷新、導出數(shù)據(jù)為CSV文件等各種操作。在新版的HAProxy中,又增加了對backend后端節(jié)點的管理功能,例如,可以在Web頁面下執(zhí)行Disable、Enable、Soft Stop、Soft Start等對后端節(jié)點的管理操作。這個功能在后端節(jié)點升級、故障維護時非常有用。
標題名稱:Linux運維:基于虛擬主機的HAProxy負載均衡系統(tǒng)配置實例\"
轉(zhuǎn)載注明:http://vcdvsql.cn/article16/chdpgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護、搜索引擎優(yōu)化、小程序開發(fā)、建站公司、云服務(wù)器、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)