haproxy 安裝與配置

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

  
  
           
  
  
  1. #HAProxy配置中分紅五部份內容,固然這些組件不是必選的,能夠根據須要選擇部分做爲配置。 
  2. #global:參數是進程級的,一般和操做系統(OS)相關。這些參數通常只設置一次,若是配置無誤,就不須要再次配置進行修改 
  3. #defaults:配置默認參數的,這些參數能夠被利用配置到frontend,backend,listen組件 
  4. #frontend:接收請求的前端虛擬節點,Frontend能夠根據規則直接指定具體使用後端的backend(可動態選擇)。 
  5. #backend:後端服務集羣的配置,是真實的服務器,一個Backend對應一個或者多個實體服務器。 
  6. #listen:Frontend和Backend的組合體。 
  7.  
  8. global 
  9.     log 127.0.0.1 local1 
  10.     maxconn 65000             #最大鏈接數 
  11.     chroot /usr/local/haproxy #安裝目錄 
  12.     uid 99                    #用戶haproxy 
  13.     gid 99                    #組haproxy 
  14.     daemon                    #守護進程運行 
  15.     nbproc 2                  #進程數量 
  16.     pidfile /usr/local/haproxy/logs/haproxy.pid #haproxy pid 
  17.  
  18. defaults 
  19.    log     global 
  20.    mode    http               #7層#默認的模式mode {tcp|http|health},tcp是4層,http是7層,health只會返回OK 
  21.    option  httplog            #http 日誌格式 
  22.    option  httpclose          #主動關閉http通道,HA-Proxy不支持keep-alive模式 
  23.    option  redispatch         #serverId對應的服務器掛掉後,強制定向到其餘健康的服務器 
  24.    option  forwardfor         #後端服務器須要得到客戶端的真實IP,將從Http Header中得到客戶端IP 
  25.    option  dontlognull        #來防止記錄 Alteo(4層負載均衡)發出的健康檢測,若是一個 session 交互沒有數據,這個 session就不會被記錄 
  26.    maxconn 50000              #最大鏈接數 
  27.    contimeout      5000       #鏈接超時(毫秒) 
  28.    clitimeout      50000      #客戶端超時(毫秒) 
  29.    srvtimeout      50000      #服務器超時(毫秒) 
  30.  
  31.    #errorfile 502 /usr/local/haproxy/html/maintain.html 
  32.    #errorfile 503 /usr/local/haproxy/html/maintain.html 
  33.    #errorfile 504 /usr/local/haproxy/html/maintain.html 
  34.   
  35.  
  36. frontend test.com             #定義前端服務器(haproxy) 
  37.         bind *:80             #監聽地址 
  38.         # 
  39.         acl static path_end -i .jpg .png .bmg .gif .css .js 
  40.         #acl web-client path_beg -i /vsphere-client 
  41.         acl bbs hdr_reg(host) -i ^(bbs.test.com|shequ.test.com|forum|phpwind|home) 
  42.         acl blog hdr_reg(host) -i ^(blog.test.com|t) 
  43.         #acl jsp path_end -i .jsp .do 
  44.         acl monitor hdr_beg(host) -i monitor.test.com    #定義ACL名稱,對應的請求的主機頭是monitor.test.com  
  45.         acl www hdr_beg(host) -i www.test.com 
  46.         acl jsp hdr_reg(host) -i ^(center.test.com|java|jsp) 
  47.         # 
  48.         use_backend  tomcat.test.com if jsp   
  49.         use_backend  cache.test.com if static    
  50.         use_backend  monitor.test.com if monitor 
  51.         use_backend  bbs.test.com if bbs  
  52.         use_backend  blog.test.com if blog 
  53.         use_backend  www.test.com if www 
  54.         #use_backend  vsphere-client if web-client 
  55.         # 
  56.         default_backend www.test.com  #指定默認的後端服務器 
  57.  
  58. backend monitor.test.com      #定義後端服務器羣(web server/apache/nginx/iis..) 
  59.         mode http 
  60.         option  forwardfor    #後端服務器(apache/nginx/iis/*),從Http Header中得到客戶端IP 
  61.         balance leastconn     #負載均衡的方式,最小鏈接 
  62.         cookie  SERVERID      #插入serverid到cookie中,serverid後面能夠定義 
  63.         option  httpchk HEAD /check.html #用來作健康檢查html文檔 
  64.         #option httpchk HEAD /index.php HTTP/1.1\r\nHost:monitor.test.com #HTTP && Host 
  65.         server  monitor 10.0.100.70:80 cookie monitor check inter 2000 rise 3 fall 3 weight 3 
  66.         #服務器定義: 
  67.         #cookie server1表示serverid爲server1; 
  68.         #check inter 2000 是檢測心跳頻率(check 默認 ); 
  69.         #rise 3 表示 3次正確認爲服務器可用; 
  70.         #fall 3 表示 3次失敗認爲服務器不可用; 
  71.         #weight 表示權重。 
  72.  
  73. backend bbs.test.com   
  74.         mode http 
  75.         option  forwardfor 
  76.         balance roundrobin    #負載均衡的方式,輪詢方式 
  77.         cookie  SERVERID insert indirect      #插入serverid到cookie中,serverid後面能夠定義 
  78.         option  httpchk HEAD /check.html  
  79.         #option httpchk HEAD /index.php HTTP/1.1\r\nHost:monitor.test.com #HTTP && Host 
  80.         server  bbs01 10.0.100.75:80 cookie bbs01 check inter 2000 rise 3 fall 3 weight 3 
  81.  
  82. backend blog.test.com 
  83.         mode http 
  84.         option  forwardfor 
  85.         balance roundrobin     
  86.         cookie  SERVERID   
  87.         option  httpchk HEAD /check.html  
  88.         server  blog01 10.0.100.76:80 cookie blog01 check inter 2000 rise 3 fall 3 weight 3 
  89.  
  90. backend www.test.com 
  91.         mode http 
  92.         option  forwardfor 
  93.         balance roundrobin    #負載均衡的方式,輪詢方式 
  94.         cookie  SERVERID   
  95.         option  httpchk HEAD /check.html  
  96.         server  www01 10.0.100.71:80 cookie www01 check inter 2000 rise 3 fall 3 weight 3 
  97.  
  98. backend cache.test.com 
  99.         mode http 
  100.         option  forwardfor 
  101.         #balance uri len 15 #url hash 
  102.         balance roundrobin 
  103.         cookie SERVERID   
  104.         server squid01 10.0.100.72:80 cookie squid01 check inter 2000 rise 3 fall 3 weight 3 
  105.         server squid02 10.0.100.73:80 cookie squid02 check inter 2000 rise 3 fall 3 weight 3 
  106.  
  107. backend tomcat.test.com 
  108.         mode http 
  109.         option  forwardfor 
  110.         balance roundrobin     
  111.         cookie  SERVERID   
  112.         option  httpchk HEAD /index.html 
  113.         server  tomcat01 10.0.100.77:8080 cookie tomcat01 check inter 2000 rise 3 fall 3 weight 3 
  114.         server  tomcat02 10.0.100.78:8080 cookie tomcat02 check inter 2000 rise 3 fall 3 weight 3 
  115.  
  116. #backend vsphere-client 
  117. #        mode http 
  118. #        option  forwardfor header ORIG_CLIENT_IP 
  119. #        balance roundrobin 
  120. #        server server1 10.0.100.81:80 redir https://192.168.57.81:443 check inter 2000 rise 3 fall 3 weight 3 
  121.  
  122.  
  123. listen admin_stat                   #status 
  124.     bind 0.0.0.0:8080               #監聽端口 
  125.     mode http                       #http的7層模式 
  126.     stats refresh 30s               #統計頁面自動刷新時間 
  127.     stats uri /haproxy_stats_url    #統計頁面URL 
  128.     stats realm Haproxy\ Statistics #統計頁面密碼框上提示文本 
  129.     stats auth admin:admin          #統計頁面用戶名和密碼設置 
  130.     stats hide-version              #隱藏統計頁面上HAProxy的版本信息 
  131.     stats admin if TRUE             #手工啓用/禁用,後端服務器 

2 haproxy 安裝腳本redis

這裏
shell

3 haproxy 服務腳本apache

這裏

4 運行 haproxy

  
  
           
  
  
  1. ./haproxy.sh 
  2. usage: ./haproxy.sh {start|stop|restart} 
  3.  
  4. ./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

http://code.google.com/p/haproxy-docs/w/list

相關文章
相關標籤/搜索