天天看點

Haproxy 負載均衡 反向代理

【Haproxy負載均衡反向代理比nginx好】

HAProxy提供高可用性、負載均衡以及基于TCP和HTTP應用的代理,支援虛拟主機,它是免費、快速并且可靠的一種解決方案。HAProxy特别适用于那些負載特大的web站點,這些站點通常又需要會話保持或七層處理。HAProxy運作在目前的硬體上,完全可以支援數以萬計的并發連接配接。并且它的運作模式使得它可以很簡單安全的整合進您目前的架構中,同時可以保護你的web伺服器不被暴露到網絡上。

HAProxy實作了一種事件驅動, 單一程序模型,此模型支援非常大的并發連接配接數。多程序或多線程模型受記憶體限制、系統排程器限制以及無處不在的鎖限制,很少能處理數千并發連接配接。事件驅動模型因為在有更好的資源和時間管理的使用者空間(User-Space)實作所有這些任務,是以沒有這些問題。此模型的弊端是,在多核系統上,這些程式通常擴充性較差。這就是為什麼他們必須進行優化以 使每個CPU時間片(Cycle)做更多的工作。

Haproxy 負載均衡 反向代理

【負載均衡】

【設定網卡】

在後面的兩台Apache伺服器,改為hostonly網絡,不用修改預設資訊

192.168.3.5   ifconfig eth2 192.168.3.5/24 up    route add default gw 192.168.3.4
192.168.3.6   ifconfig eth2 192.168.3.6/24 up    route add default gw 192.168.3.4
192.168.3.5   service iptables stop        service httpd start
192.168.3.6   service iptables stop        service httpd start      

【通路測試】http://192.168.3.6/

【通路測試】http://192.168.3.5/

【路由檢視】

[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.3.0     0.0.0.0         255.255.255.0   U    0      0        0 eth2
172.16.0.0      0.0.0.0         255.255.0.0     U    1      0        0 eth1
0.0.0.0         192.168.3.4     0.0.0.0         UG   0      0        0 eth2
0.0.0.0         172.16.0.1      0.0.0.0         UG   0      0        0 eth1      

【删除添加錯誤的路由】

route del -net 0.0.0.0/32 gw 192.168.3.4      

【安裝haproxy】

我的系統是CentOS 6.3 X64的不自帶haproxy

換CentOS 6.6 X64的CD光牒

yum clean all

yum –y install haproxy

cd/etc/haproxy/

cphaproxy.cfg haproxy.cfg.bak

【設定日志】

vim /etc/sysconfig/rsyslog
# If you want to use them,switch to compatibility mode 2 by "-c 2"
# See rsyslogd(8) for moredetails
SYSLOGD_OPTIONS="-c 2 -r"
                                  
vim /etc/rsyslog.conf
改為
# Save boot messages also to boot.log
local7.*                                               /var/log/boot.log
 
local2.*                                               /var/log/haproxy.log      

【重新開機系統日志服務】service rsyslog restart

【修改配置】

cd /etc/haproxy/
******************删了***************************
#---------------------------------------------------------------------
#main frontend which proxys to the backends 
#---------------------------------------------------------------------
frontend  main *:5000 
    acl url_static       path_beg       -i /static /p_w_picpaths /javascript/stylesheets 
    acl url_static       path_end       -i .jpg .gif .png .css .js 
 
    use_backend static          if url_static 
    default_backend             app 
 
#---------------------------------------------------------------------
#static backend for serving up p_w_picpaths, stylesheets and such 
#---------------------------------------------------------------------
backendstatic 
    balance    roundrobin 
    server     static 127.0.0.1:4331 check 
 
#---------------------------------------------------------------------
#round robin balancing between the various backends 
#---------------------------------------------------------------------
backendapp 
    balance    roundrobin 
    server app1 127.0.0.1:5001 check 
    server app2 127.0.0.1:5002 check 
    server  app3 127.0.0.1:5003 check 
server app4 127.0.0.1:5004 check
***************************删掉後在後面接着寫*******************************
listen stats
    mode http
    bind *:1080
    stats enable
    stats hide-version
    stats uri  /haproxyadmin?stats
    stats realm Haproxy\ Statistics
    stats auth admin:admin
    stats admin if TRUE
 
frontend web
    bind *:80
    log global
    option httpclose
    option logasap
    option dontlognull
    capture request header Host len 20
    capture request header Referer len 60
    default_backend servers
 
backend servers
    balance roundrobin
    server web1 192.168.3.5:80 check maxconn4000
    server web2 192.168.3.6:80 check maxconn3000      

*****************************************************************************

【重新開機服務】

service haproxy restart
netstat-tnulp |grep "80"
tcp        0     0 0.0.0.0:80                 0.0.0.0:*                   LISTEN      4639/haproxy        
tcp        0     0 0.0.0.0:1080               0.0.0.0:*                   LISTEN      4639/haproxy      

【看程序】ps auxf

haproxy   4639 0.0  0.1  47564 1588 ?        Ss   02:35  0:00 /usr/sbin/haproxy -D -f /etc/haproxy/haprox

【保證http://192.168.3.6/,http://192.168.3.5/的防火牆無礙,Apache已開啟】

【測試http://172.16.0.102/】可以達到負載均衡的效果了,IE浏覽器 火狐浏覽器 遨遊不明顯

【haproxy管理頁面】http://172.16.0.102:1080/haproxyadmin?stats admin admin

可以看到server的個數 詳細資訊