【Haproxy负载均衡反向代理比nginx好】

 

 

HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,同时可以保护你的web服务器不被暴露到网络上。

 


HAProxy
实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space)实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。

 

 wKioL1ZtJuTAhXvVAAB-ocs0PLI037.png

 

 

 

 


【负载均衡】

【设置网卡】

在后面的两台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的光盘

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的个数 详细信息

手动关一台httpd服务 service httpd stop,再来刷新这个页面,立即出现异常