文章目錄
- Linux雲計算架構-使用HAProxy實作負載均衡叢集
-
- 1. HAProxy介紹
- 2. HAProxy部署
- 3. 配置後端伺服器
- 4. 測試haproxy負載均衡
- 5. 注意點
Linux雲計算架構-使用HAProxy實作負載均衡叢集
1. HAProxy介紹
從HAProxy的字面意思可以看到,是由
HA+Proxy
,即可以實作高可用和代理。HAProxy是一種提供高可用、負載均衡以及基于TCP和HTTP應用的代理的,支援虛拟主機的,免費、快速、可靠的解決方案。官方資料表明最高極限支援10G的并發通路。
HAproxy也是通過反向代理的方式實作web的負載均衡,但是與nginx實作負載均衡不同的是,nginx本身是屬于web伺服器的,而HAproxy隻是一款用于負載均衡的軟體,本身并不提供web服務。
HAproxy配置簡單,并且擁有伺服器健康檢查功能和專門的系統狀态監控頁面,當代理的後端伺服器出現故障,HAproxy會将其從負載均衡叢集中剔除,恢複後會将伺服器自動加入負載均衡叢集。
2. HAProxy部署
環境介紹:
主機名 | IP位址 | 作用 |
---|---|---|
server | 192.168.8.128 | haproxy代理伺服器,負責負載均衡排程 |
rs1 | 192.168.8.129 | 後端伺服器 |
rs2 | 192.168.8.130 | 後端伺服器 |
拓撲圖:
下載下傳haproxy軟體包:
https://github.com/haproxy/haproxy/releases/
[[email protected] ~]# ll haproxy-2.3-dev8.tar.gz
-rw-r--r--. 1 root root 2905297 10月 29 20:35 haproxy-2.3-dev8.tar.gz
[[email protected] ~]# tar xzf haproxy-2.3-dev8.tar.gz
[[email protected] ~]# cd haproxy-2.3-dev8/
[[email protected] haproxy-2.3-dev8]# cat INSTALL
[[email protected] ~]# uname -r
3.10.0-957.el7.x86_64
# 通過檢視INSTALL檔案,可以看到要安裝haproxy軟體包,需要核心在linux2.6.28以上版本,我這裡的是3.10.0
# 安裝依賴
[[email protected] haproxy-2.3-dev8]# yum -y install make gcc gcc-c++ openssl-devel
# 編譯安裝
# 可以提前在Makefile檔案中修改TARGET和PREFIX參數值,然後直接make && make install
# 否則隻能make和make install都指定PREFIX參數
[[email protected] haproxy-2.3-dev8]# make TARGET=linux3100 PREFIX=/usr/local/haproxy
[[email protected] haproxy-2.3-dev8]# make install PREFIX=/usr/local/haproxy
[[email protected] haproxy]# pwd
/usr/local/haproxy
[[email protected] haproxy]# ll
總用量 0
drwxr-xr-x. 3 root root 21 10月 29 23:12 doc
drwxr-xr-x. 2 root root 21 10月 29 23:12 sbin
drwxr-xr-x. 3 root root 17 10月 29 23:12 share
建立haproxy的配置檔案:
/usr/local/haproxy/etc/haproxy.cfg
[[email protected] ~]# mkdir /usr/local/haproxy/etc
[[email protected] ~]# cd /usr/local/haproxy/etc
[[email protected] etc]# vim haproxy.cfg
global # 全局配置
log 127.0.0.1 local0
maxconn 4096
chroot /usr/local/haproxy # haproxy安裝目錄
uid 99 # 運作使用者,預設存在的,使用者名為nobody,可用id 99檢視
gid 99 # 運作使用者組
daemon # 背景運作haproxy
nbproc 1 # 啟動的程序數,計劃在2.5中删除該參數,改用多線程代替多線程。可以注釋掉或者改為1個程序
pidfile /usr/local/haproxy/run/haproxy.pid # 程序PID檔案,記錄所有的程序
defaults # 預設配置
log global
log 127.0.0.1 local3 # 日志檔案的輸入定向,日志級别為local3
mode http # 工作模式,預設為http模式,也可以是tcp模式。
option httplog # 日志類别,記錄http日志
option httpclose # 每次請求完畢後主動關閉http連接配接通道,haproxy不支援長連接配接。
option dontlognull # 不記錄空連接配接産生的日志
option forwardfor # 若後端真實伺服器需要擷取用戶端的IP,則需要配置該參數,可用http header 中擷取用戶端的IP位址。
option redispatch # 當serverid對應的伺服器挂了,強制定向到其他健康的伺服器。
retries 2 # 檢查伺服器是否可用的嘗試次數
maxconn 2000 # 最大連接配接數
balance roundrobin # 負載均衡算法,這裡是rr輪詢
stats uri /haproxy-stats # haproxy監控頁面的通路位址
timeout connect 5000 # 連接配接逾時時間,機關ms,即5s
timeout client 50000 # 用戶端連接配接逾時時間
timeout server 50000 # 服務端連接配接逾時時間
mode http
option httpchk GET /index.html # 健康檢測頁面,應該是一個小頁面,每個1s進行檢測該頁面
frontend http # 前端配置,http名稱可自定義
bind 0.0.0.0:80 # 發起http請求80端口,會被轉發到該IP位址和端口
default_backend http_back # 後端伺服器叢集名稱
backend http_back # 後端伺服器配置
server s1 192.168.8.129:80 weight 3 check # 後端節點配置
server s2 192.168.8.130:80 weight 3 check
#####################################################################
# server node1 後端IP位址 inter 2000 rise 3 fall 3 weight 30
# inner 2000 健康檢查時間間隔為2s
# rise 3 健康檢查次數,檢查3次健康才算健康
# fall 3 失敗檢查次數,檢查3次失敗才算失敗
# weight 30 權重
#####################################################################
# balance 負載均衡算法
source 根據請求源IP位址分發
static-rr 根據權重分發
leastconn 最少連接配接者先處理
uri 根據uri分發
uri_param 根據請求的uri參數分發
rdp_cookie 根據cookie來分發,并hash每一次請求
hdr 根據http的請求頭分發每一次http請求
roundrobin 輪詢
配置haproxy啟動腳本:
[[email protected] ~]# cp ./haproxy-2.3-dev8/examples/haproxy.init /etc/init.d/haproxy
[[email protected] ~]# ll /etc/init.d/haproxy
-rw-r--r--. 1 root root 2381 10月 30 22:06 /etc/init.d/haproxy
[[email protected] ~]# chmod 755 /etc/init.d/haproxy
[[email protected] ~]# vim /etc/init.d/haproxy # 修改5個位置
#!/bin/sh
#
# chkconfig: - 85 15
# description: HA-Proxy is a TCP/HTTP reverse proxy which is particularly suited \
# for high availability environments.
# processname: haproxy
# config: /etc/haproxy/haproxy.cfg
# pidfile: /var/run/haproxy.pid
# Script Author: Simon Matter <[email protected]>
# Version: 2004060600
# Source function library.
if [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
. /etc/rc.d/init.d/functions
else
exit 0
fi
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
# This is our service name
BASENAME=haproxy # 定義變量
BIN=/usr/sbin/haproxy # 定義haproxy二進制指令的位置
CFG=/usr/local/haproxy/etc/haproxy.cfg # 定義haproxy的配置檔案位置
[ -f $CFG ] || exit 1
PIDFILE=/usr/local/haproxy/run/haproxy.pid # PID程序檔案
LOCKFILE=/usr/local/haproxy/run/haproxy # 檔案存在,就說明haproxy在運作
RETVAL=0
start() {
quiet_check
if [ $? -ne 0 ]; then
echo "Errors found in configuration file, check it with '$BASENAME check'."
return 1
fi
echo -n "Starting $BASENAME: "
daemon $BIN -D -f $CFG -p $PIDFILE
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $LOCKFILE
return $RETVAL
}
stop() {
echo -n "Shutting down $BASENAME: "
killproc $BASENAME -USR1
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $LOCKFILE
[ $RETVAL -eq 0 ] && rm -f $PIDFILE
return $RETVAL
}
restart() {
quiet_check
if [ $? -ne 0 ]; then
echo "Errors found in configuration file, check it with '$BASENAME check'."
return 1
fi
stop
start
}
reload() {
if ! [ -s $PIDFILE ]; then
return 0
fi
quiet_check
if [ $? -ne 0 ]; then
echo "Errors found in configuration file, check it with '$BASENAME check'."
return 1
fi
$BIN -D -f $CFG -p $PIDFILE -sf $(cat $PIDFILE)
}
check() {
$BIN -c -q -V -f $CFG
}
quiet_check() {
$BIN -c -q -f $CFG
}
rhstatus() {
status $BASENAME
}
condrestart() {
[ -e $LOCKFILE ] && restart || :
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
condrestart)
condrestart
;;
status)
rhstatus
;;
check)
check
;;
*)
echo $"Usage: $BASENAME {start|stop|restart|reload|condrestart|status|check}"
exit 1
esac
exit $?
# 配置二進制檔案BIN
[[email protected] ~]# cp /usr/local/haproxy/sbin/haproxy /usr/sbin/
# 配置PIDFILE和LOCKFILE檔案的存放目錄
[[email protected] ~]# mkdir -p /usr/local/haproxy/run
[[email protected] ~]# chown -R nobody /usr/local/haproxy/
# 配置日志收集,否則無法收集日志。
[[email protected] ~]# vim /etc/rsyslog.conf
15 $ModLoad imudp
16 $UDPServerRun 514
74 local3.* /var/log/haproxy.log
75 local0.* /var/log/haproxy.log
啟動和停止haproxy:
# 啟動haproxy
[[email protected] ~]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
# 檢視haproxy的狀态
[[email protected] ~]# ps aux | grep haproxy
nobody 8725 0.0 0.0 8480 1496 ? Ss 22:33 0:00 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
root 8731 0.0 0.0 112724 988 pts/0 S+ 22:33 0:00 grep --color=auto haproxy
[[email protected] ~]# netstat -antup |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8725/haproxy
# 關閉haproxy
# 若無killall指令,可使用yum install psmisc -y 安裝
[[email protected] ~]# killall haproxy
# 配置開機自啟
[[email protected] ~]# chkconfig --add haproxy
[[email protected] ~]# chkconfig haproxy on
#################################################################
# 也可以用腳本啟動,但看起來像是前台啟動,會卡住。故建議用以上方法進行啟動和關閉haproxy
[[email protected] ~]# /etc/init.d/haproxy start
[[email protected] ~]# systemctl restart haproxy.service
3. 配置後端伺服器
[[email protected] ~]# yum install -y httpd
[[email protected] ~]# echo "rs1" > /var/www/html/index.html
[[email protected] ~]# systemctl start httpd
[[email protected] ~]# systemctl enable httpd
[[email protected] ~]# netstat -antup | grep httpd
tcp6 0 0 :::80 :::* LISTEN 6870/httpd
[[email protected] ~]# yum install -y httpd
[[email protected] ~]# echo "rs2" > /var/www/html/index.html
[[email protected] ~]# systemctl start httpd
[[email protected] ~]# systemctl enable httpd
[[email protected] ~]# netstat -antup | grep httpd
tcp6 0 0 :::80 :::* LISTEN 6853/httpd
# 防火牆開放80端口号,三台主機都要開。
[[email protected] ~]# firewall-cmd --list-port
80/tcp
4. 測試haproxy負載均衡
輸出網址:
http://192.168.8.128/haproxy-stats
檢視負載均衡統計頁面
輸入網址:
http://192.168.8.128/
随着重新整理,每次看到的頁面都不一樣,說明
haproxy
确實有在做負載均衡。
停止rs1的httpd服務,可以看到統計頁面上也會顯示rs1已經DOWN了,自動将rs1從叢集中剔除:
重新開機rs1上的httpd服務,可以看到rs1又自動加入叢集中:
5. 注意點
①haproxy通過檢查後端伺服器的一個小頁面去判斷後端伺服器是否存活,這個小頁面最後是靜态小頁面。
②由于使用源碼包安裝,haproxy不存在配置檔案,故需要自行建立配置檔案,這個估計得自行思考如何配置。
③haproxy的啟動腳本,需要掌握shell腳本程式設計才好了解,但幸好改動的地方不是太多。
④haproxy的統計界面,主要作用是監控後端伺服器是否故障,當發現後端伺服器故障時,及時修複後端伺服器。