hi 歡迎閱讀此文,此文主要講解 haproxy 的安裝與配置,還有兩個shell script :
haproxy_install.sh 用于haproxy安裝與配置
haproxy.sh 用于管理haproxy 服務,功能 開啟 關閉 重新開機
此文沒有介紹haproxy ,因為既然您來到這裡,說明您很關心haproxy 可能早就聞其大名了(4,7層負載均衡軟體),haproxy 優點與強大的功能我就不重複了,好了下面開始!
1 haproxy 配置文檔,#号為注釋,用于功能說明
update 20120904
$ cat /usr/local/haproxy/haproxy.cfg
#HAProxy配置中分成五部分内容,當然這些元件不是必選的,可以根據需要選擇部分作為配置。
#global:參數是程序級的,通常和作業系統(OS)相關。這些參數一般隻設定一次,如果配置無誤,就不需要再次配置進行修改
#defaults:配置預設參數的,這些參數可以被利用配置到frontend,backend,listen元件
#frontend:接收請求的前端虛拟節點,Frontend可以根據規則直接指定具體使用後端的backend(可動态選擇)。
#backend:後端服務叢集的配置,是真實的伺服器,一個Backend對應一個或者多個實體伺服器。
#listen:Frontend和Backend的組合體。
global
log 127.0.0.1 local1
maxconn 65000 #最大連接配接數
chroot /usr/local/haproxy #安裝目錄
uid 99 #使用者haproxy
gid 99 #組haproxy
daemon #守護程序運作
nbproc 2 #程序數量
pidfile /usr/local/haproxy/logs/haproxy.pid #haproxy pid
defaults
log global
mode http #7層#預設的模式mode {tcp|http|health},tcp是4層,http是7層,health隻會傳回OK
option httplog #http 日志格式
option httpclose #主動關閉http通道,HA-Proxy不支援keep-alive模式
option redispatch #serverId對應的伺服器挂掉後,強制定向到其他健康的伺服器
option forwardfor #後端伺服器需要獲得用戶端的真實IP,将從Http Header中獲得用戶端IP
option dontlognull #來防止記錄 Alteo(4層負載均衡)發出的健康檢測,如果一個 session 互動沒有資料,這個 session就不會被記錄
maxconn 50000 #最大連接配接數
contimeout 5000 #連接配接逾時(毫秒)
clitimeout 50000 #用戶端逾時(毫秒)
srvtimeout 50000 #伺服器逾時(毫秒)
#errorfile 502 /usr/local/haproxy/html/maintain.html
#errorfile 503 /usr/local/haproxy/html/maintain.html
#errorfile 504 /usr/local/haproxy/html/maintain.html
frontend test.com #定義前端伺服器(haproxy)
bind *:80 #監聽位址
#
acl static path_end -i .jpg .png .bmg .gif .css .js
#acl web-client path_beg -i /vsphere-client
acl bbs hdr_reg(host) -i ^(bbs.test.com|shequ.test.com|forum|phpwind|home)
acl blog hdr_reg(host) -i ^(blog.test.com|t)
#acl jsp path_end -i .jsp .do
acl monitor hdr_beg(host) -i monitor.test.com #定義ACL名稱,對應的請求的主機頭是monitor.test.com
acl www hdr_beg(host) -i www.test.com
acl jsp hdr_reg(host) -i ^(center.test.com|java|jsp)
use_backend tomcat.test.com if jsp
use_backend cache.test.com if static
use_backend monitor.test.com if monitor
use_backend bbs.test.com if bbs
use_backend blog.test.com if blog
use_backend www.test.com if www
#use_backend vsphere-client if web-client
default_backend www.test.com #指定預設的後端伺服器
backend monitor.test.com #定義後端伺服器群(web server/apache/nginx/iis..)
mode http
option forwardfor #後端伺服器(apache/nginx/iis/*),從Http Header中獲得用戶端IP
balance leastconn #負載均衡的方式,最小連接配接
cookie SERVERID #插入serverid到cookie中,serverid後面可以定義
option httpchk HEAD /check.html #用來做健康檢查html文檔
#option httpchk HEAD /index.php HTTP/1.1\r\nHost:monitor.test.com #HTTP && Host
server monitor 10.0.100.70:80 cookie monitor check inter 2000 rise 3 fall 3 weight 3
#伺服器定義:
#cookie server1表示serverid為server1;
#check inter 2000 是檢測心跳頻率(check 預設 );
#rise 3 表示 3次正确認為伺服器可用;
#fall 3 表示 3次失敗認為伺服器不可用;
#weight 表示權重。
backend bbs.test.com
option forwardfor
balance roundrobin #負載均衡的方式,輪詢方式
cookie SERVERID insert indirect #插入serverid到cookie中,serverid後面可以定義
option httpchk HEAD /check.html
server bbs01 10.0.100.75:80 cookie bbs01 check inter 2000 rise 3 fall 3 weight 3
backend blog.test.com
balance roundrobin
cookie SERVERID
server blog01 10.0.100.76:80 cookie blog01 check inter 2000 rise 3 fall 3 weight 3
backend www.test.com
server www01 10.0.100.71:80 cookie www01 check inter 2000 rise 3 fall 3 weight 3
backend cache.test.com
#balance uri len 15 #url hash
balance roundrobin
cookie SERVERID
server squid01 10.0.100.72:80 cookie squid01 check inter 2000 rise 3 fall 3 weight 3
server squid02 10.0.100.73:80 cookie squid02 check inter 2000 rise 3 fall 3 weight 3
backend tomcat.test.com
option httpchk HEAD /index.html
server tomcat01 10.0.100.77:8080 cookie tomcat01 check inter 2000 rise 3 fall 3 weight 3
server tomcat02 10.0.100.78:8080 cookie tomcat02 check inter 2000 rise 3 fall 3 weight 3
#backend vsphere-client
# mode http
# option forwardfor header ORIG_CLIENT_IP
# balance roundrobin
# server server1 10.0.100.81:80 redir https://192.168.57.81:443 check inter 2000 rise 3 fall 3 weight 3
listen admin_stat #status
bind 0.0.0.0:8080 #監聽端口
mode http #http的7層模式
stats refresh 30s #統計頁面自動重新整理時間
stats uri /haproxy_stats_url #統計頁面URL
stats realm Haproxy\ Statistics #統計頁面密碼框上提示文本
stats auth admin:admin #統計頁面使用者名和密碼設定
stats hide-version #隐藏統計頁面上HAProxy的版本資訊
stats admin if TRUE #手工啟用/禁用,後端伺服器
2 haproxy 安裝腳本
<a target="_blank" href="http://dngood.blog.51cto.com/446195/703347/#haproxy_install">這裡</a>
3 haproxy 服務腳本
<a target="_blank" href="http://dngood.blog.51cto.com/446195/703347/#haproxy_sbin">這裡</a>
4 運作 haproxy
./haproxy.sh
usage: ./haproxy.sh {start|stop|restart}
./haprroxy.sh start
5 haproxy 監控頁面
http://yourip:8080/haproxy_stats_url
使用者名與密碼:admin
<a target="_blank" href="http://blog.51cto.com/attachment/201112/135351121.png"></a>
結束
shell 腳本如有bug ,歡迎回報!
mail:[email protected]
qq群: 37275208
#update 20120701
TProxy透明代理相關配置
<a href="http://yupengyan.com/quick-installation-of-haproxy-on-centos6-2.html" target="_blank">http://yupengyan.com/quick-installation-of-haproxy-on-centos6-2.html</a>
Configure HAProxy with TPROXY kernel for full transparent proxy
<a href="http://blog.loadbalancer.org/configure-haproxy-with-tproxy-kernel-for-full-transparent-proxy/" target="_blank">http://blog.loadbalancer.org/configure-haproxy-with-tproxy-kernel-for-full-transparent-proxy/</a>
haproxy-doc
<a href="http://code.google.com/p/haproxy-docs/w/list" target="_blank">http://code.google.com/p/haproxy-docs/w/list</a>
本文轉自 dongnan 51CTO部落格,原文連結:http://blog.51cto.com/dngood/738634