一、HAProxy概述: HAProxy提供高可用性、負載均衡以及基於TCP和HTTP應用的代理,支持虛擬主機,它是免費、快速並且可靠的一種解決方案。根據官方數據,其最高極限支持10G的併發。 HAProxy特別適用於那些負載特大的web站點, 這些站點通常又需要會話保持或七層處理。HAProxy運 ...
一、HAProxy概述:
HAProxy提供高可用性、負載均衡以及基於TCP和HTTP應用的代理,支持虛擬主機,它是免費、快速並且可靠的一種解決方案。根據官方數據,其最高極限支持10G的併發。
HAProxy特別適用於那些負載特大的web站點, 這些站點通常又需要會話保持或七層處理。HAProxy運行在當前的硬體上,完全可以支持數以萬計的併發連接。並且它的運行模式使得它可以很簡單安全的整合進您當前的架構中, 同時可以保護你的web伺服器不被暴露到網路上。
其支持從4層至7層的網路交換,即覆蓋所有的TCP協議。就是說,Haproxy 甚至還支持 Mysql的均衡負載。
相同點:在功能上,proxy通過反向代理方式實現 WEB均衡負載。和 Nginx,ApacheProxy,lighttpd,Cheroke 等一樣。
不同點:Haproxy 並不是 web 伺服器。以上提到所有帶反向代理均衡負載的產品,都清一色是 WEB 伺服器。簡單說,就是他們能處理解析頁面的。而Haproxy 僅僅是一款的用於均衡負載的應用代理。其自身並不能提供web服務。但其配置簡單,擁有非常不錯的伺服器健康檢查功能還有專門的系統狀態監控頁面,當其代理的後端伺服器出現故障, HAProxy會自動將該伺服器摘除,故障恢復後再自動將該伺服器加入。
www.haproxy.org #打不開
http://haproxy.com/ #收費
http://haproxy.1wt.eu/ #社區版地址, 打不開
https://github.com/haproxy/haproxy/releases/ 在github 可以下載
實驗拓撲圖:
二、實戰
1.安裝依賴
1 |
[root@xuegod63 ~] # yum -y install make gcc gcc-c++ openssl-devel
|
2.安裝haproxy
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@xuegod63 ~] # tar -zxvf haproxy-1.7.9.tar.gz
[root@xuegod63 haproxy-1.7.9] # cd /root/haproxy-1.7.9
[root@xuegod63 haproxy-1.7.9] # uname -r #查看內核版本
3.10.0-693.el7.x86_64
[root@xuegod63 haproxy-1.7.9] # make TARGET=linux2628 PREFIX=/usr/local/haproxy #指定操作系統內核類型和安裝的路徑。也可以直接修改Makefile配置文件中這兩個變數的值。如下:
[root@xuegod63 haproxy-1.7.9] # vim Makefile
94 PREFIX = /usr/local/haproxy
104 TARGET =linux26
[root@xuegod63 haproxy-1.7.9] # make install PREFIX=/usr/local/haproxy
#如果沒有修改Makefile配置文件中PREFIX變數的值,就必須在此重新對,PREFIX=/usr/local/haproxy賦值,否則直接執行 make install 時,make install會直接讀取Makefile文件中PREFIX的變數值。
[root@xuegod63 haproxy-1.7.9] # ls /usr/local/haproxy/
doc sbin share
|
3. 沒有生成配置文件,自己手動寫一個HAproxy配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
[root@xuegod63 ~] # mkdir /usr/local/haproxy/etc
[root@xuegod63 ~] # vim /usr/local/haproxy/etc/haproxy.cfg #手動創建配置文件
global
log 127.0.0.1 local0
#log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
chroot /usr/local/haproxy
uid 99 #所屬運行的用戶uid
gid 99 #所屬運行的用戶組
daemon #以後臺形式運行haproxy
nbproc 1 #啟動1個haproxy實例。# #工作進程數量(CPU數量) ,實際工作中,應該設置成和CPU核心數一樣。 這樣可以發揮出最大的性能。
pidfile /usr/local/haproxy/run/haproxy .pid #將所有進程寫入pid文件
#debug #調試錯誤時用
#quiet #安靜
defaults
log global
log 127.0.0.1 local3 #日誌文件的輸出定向。產生的日誌級別為local3. 系統中local1-7,用戶自己定義
mode http #工作模式,所處理的類別,預設採用http模式,可配置成tcp作4層消息轉發
option httplog #日誌類別,記載http日誌
option httpclose #每次請求完畢後主動關閉http通道,haproxy不支持keep-alive,只能模擬這種模式的實現
option dontlognull #不記錄空連接,產生的日誌
option forwardfor #如果後端伺服器需要獲得客戶端真實ip需要配置的參數,可以從Http Header中獲得客戶端ip
option redispatch #當serverid對應的伺服器掛掉後,強制定向到其他健康伺服器
retries 2 #2次連接失敗就認為伺服器不可用,主要通過後面的check檢查
maxconn 2000 #最大連接數
balance roundrobin #負載均衡演算法
stats uri /haproxy-stats #haproxy 監控頁面的訪問地址 # 可通過 http://localhost:80/haproxy-stats 訪問
timeout connect 5000 #連接超時時間。 單位:ms 毫秒
timeout client 50000 #客戶端連接超時時間
timeout server 50000 #伺服器端連接超時時間
mode http
option httpchk GET /index .html #健康檢測#註意實際工作中測試時,應該下載某一個頁面來進行測試,因此這個頁面應該是個小頁面,而不要用首頁面。這裡是每隔一秒檢查一次頁面。
frontend http #前端配置,http名稱可自定義
bind 0.0.0.0:80 #發起http請求80埠,會被轉發到設置的ip及埠
default_backend http_back #轉發到後端 寫上後端名稱
backend http_back #後端配置,名稱上下關聯
server s1 192.168.1.62:80 weight 3 check #後端的主機 IP &權衡
server s2 192.168.1.64:80 weight 3 check #後端的主機 IP &權衡
#server node1 192.168.179.131:8081 check inter 2000 rise 3 fall 3 weight 30
# inter 2000 健康檢查時間間隔2秒
# rise 3 檢測多少次才認為是正常的
# fall 3 失敗多少次才認為是不可用的
# weight 30 權重
使用nobody用戶運行haproxy
[root@xuegod63 haproxy-1.7.9] # id nobody
uid=99(nobody) gid=99(nobody) groups =99(nobody) #id 為99
|
關於負載均衡演算法
#source 根據請求源IP
#static-rr 根據權重
#leastconn 最少連接者先處理
#uri 根據請求的uri
#url_param 根據請求的url參數
#rdp-cookie 據據cookie(name)來鎖定並哈希每一次請求
#hdr(name) 根據HTTP請求頭來鎖定每一次HTTP請求
#roundrobin 輪詢方式
4.設置haproxy啟動腳本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
[root@xuegod63 ~] # cp ./haproxy-1.7.9/examples/haproxy.init /etc/init.d/haproxy
[root@xuegod63 ~] # chmod 755 /etc/init.d/haproxy
[root@xuegod63 ~] # vim /etc/init.d/haproxy #修改後的腳本
#!/bin/sh
# chkconfig: - 85 15
# description: HA-Proxy server
# processname: haproxy
# config: /usr/local/haproxy/etc/haproxy.cfg
# pidfile: /usr/local/haproxy/run/haproxy.pid
# 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
CFG= /usr/local/haproxy/etc/haproxy .cfg
[ -f $CFG ] || exit 1
PIDFILE= /usr/local/haproxy/run/haproxy .pid
LOCKFILE= /usr/local/haproxy/run/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 $?
複製haproxy文件到 /usr/sbin 下
因為上面的haproxy.init啟動腳本預設會去 /usr/sbin 下找
[root@xuegod63 ~] #cp /usr/local/haproxy/sbin/haproxy /usr/sbin/
創建目錄和許可權
[root@xuegod63 ~] # mkdir -p /usr/local/haproxy/run
[root@xuegod63 ~] # chown nobody /usr/local/haproxy/ -R
配置日誌收集
[root@xuegod63 ~] # vim /etc/rsyslog.conf #打開以下兩行的註釋,不打開收不到日誌
$ModLoad imudp #取消註釋
$UDPServerRun 514 #取消註釋
local7.* /var/log/boot .log #下麵添加兩行
local3.* /var/log/haproxy .log
local0.* /var/log/haproxy .log
[root@xuegod63 ~] # systemctl restart rsyslog
|
5. 啟動停止haproxy服務
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
特殊啟動方法1
[root@xuegod63 etc] # /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
查看狀態:
[root@xuegod63 etc] # ps -axu | grep haproxy
nobody 3871 0.0 0.0 12228 1036 ? Ss 21:53 0:00 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy .cfg
[root@xuegod63 etc] # netstat -antup | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3871 /haproxy
##停止
[root@xuegod63 etc] # killall haproxy #沒有killall命令?安裝yum -y install psmisc
HAproxy腳本啟動方法2
[root@xuegod63 ~] # /etc/init.d/haproxy start 或 systemctl restart haproxy
|
6.配置xuegod62,xuegod64後端伺服器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
配置後端伺服器: xuegod62
配置web伺服器:
[root@xuegod62 html] # yum install httpd php -y
生成測試文件:
root@xuegod62 html] #echo 192.168.1.62 > /var/www/html/index.html
啟動apache伺服器:
[root@xuegod62 html] # service httpd restart
配置後端伺服器: xuegod64
IP: 192.168.1.64
配置web伺服器:
[root@xuegod64 html] # yum install httpd php -y
生成測試文件:
echo 192.168.1.64 > /var/www/html/index .html
[root@xuegod64 html] # service httpd restart
|
7.查看HAproxy的監控頁面
http://192.168.1.63/haproxy-stats,類似如下圖
測試:反向代理功能
http://192.168.1.63/
註:
相關配置文件和啟動腳本可以從這個配置模版中獲得
1 2 3 4 5 |
[root@xuegod63 haproxy-1.7.9] # cd /root/haproxy-1.7.9/examples/
[root@xuegod63 examples] # ls
配置隨機啟動
[root@xuegod63 examples] # chkconfig --add haproxy
[root@xuegod63 examples] # chkconfig haproxy on
|