hi 歡迎閱讀此文,此文主要講解 haproxy 的安裝與配置,還有兩個shell script :php
haproxy_install.sh 用於haproxy安裝與配置css
haproxy.sh 用於管理haproxy 服務,功能 開啓 關閉 重啓html
此文沒有介紹haproxy ,由於既然您來到這裏,說明您很關心haproxy 可能早就聞其大名了(4,7層負載均衡軟件),haproxy 優勢與強大的功能我就不重複了,好了下面開始!前端
1 haproxy 配置文檔,#號爲註釋,用於功能說明java
update 20120904nginx
$ cat /usr/local/haproxy/haproxy.cfg web
- #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
- mode http
- option forwardfor
- balance roundrobin #負載均衡的方式,輪詢方式
- cookie SERVERID insert indirect #插入serverid到cookie中,serverid後面能夠定義
- option httpchk HEAD /check.html
- #option httpchk HEAD /index.php HTTP/1.1\r\nHost:monitor.test.com #HTTP && Host
- server bbs01 10.0.100.75:80 cookie bbs01 check inter 2000 rise 3 fall 3 weight 3
- backend blog.test.com
- mode http
- option forwardfor
- balance roundrobin
- cookie SERVERID
- option httpchk HEAD /check.html
- server blog01 10.0.100.76:80 cookie blog01 check inter 2000 rise 3 fall 3 weight 3
- backend www.test.com
- mode http
- option forwardfor
- balance roundrobin #負載均衡的方式,輪詢方式
- cookie SERVERID
- option httpchk HEAD /check.html
- server www01 10.0.100.71:80 cookie www01 check inter 2000 rise 3 fall 3 weight 3
- backend cache.test.com
- mode http
- option forwardfor
- #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
- mode http
- option forwardfor
- balance roundrobin
- cookie SERVERID
- 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 安裝腳本redis
這裏
shell
3 haproxy 服務腳本apache
4 運行 haproxy
- ./haproxy.sh
- usage: ./haproxy.sh {start|stop|restart}
- ./haprroxy.sh start
5 haproxy 監控頁面
http://yourip:8080/haproxy_stats_url
用戶名與密碼:admin
結束
shell 腳本若有bug ,歡迎反饋!
mail:dngood@sina.com
qq羣: 37275208
#update 20120701
TProxy透明代理相關配置
http://yupengyan.com/quick-installation-of-haproxy-on-centos6-2.html
Configure HAProxy with TPROXY kernel for full transparent proxy
http://blog.loadbalancer.org/configure-haproxy-with-tproxy-kernel-for-full-transparent-proxy/
haproxy-doc