HAProxy提供高可用性、負載均衡以及基於TCP和HTTP應用的代理,支持虛擬主機,它是免費、快速而且可靠的一種解決方案。根據官方數據,其最高極限支持10G的併發。
HAProxy特別適用於那些負載特大的web站點, 這些站點一般又須要會話保持或七層處理。HAProxy運行在當前的硬件上,徹底能夠支持數以萬計的併發鏈接。而且它的運行模式使得它能夠很簡單安全的整合進您當前的架構中, 同時能夠保護你的web服務器不被暴露到網絡上。
其支持從4層至7層的網絡交換,即覆蓋全部的TCP協議。就是說,Haproxy 甚至還支持 Mysql的均衡負載。php
相同點:在功能上,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 能夠下載
實驗拓撲圖:html
[root@xuegod63 ~]# yum -y install make gcc gcc-c++ openssl-devel
[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
[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 輪詢方式前端
[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
特殊啓動方法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
配置後端服務器: 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
http://192.168.1.63/haproxy-stats,相似以下圖node
測試:反向代理功能
http://192.168.1.63/linux
注:
相關配置文件和啓動腳本能夠從這個配置模版中得到c++
[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