Centos7快速安装haproxy

HAProxy是一个使用C语言编写的自由及开放源代码软件[1],其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理
HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。

安装Haproxy
#yum -y install gcc           
#tar zxf haproxy-1.4.21.tar.gz
#mv haproxt-1.4.21 haproxy
#make TARGET=linux31 PREFIX=/usr/local/haproxy    将haproxy安装到指定目录
#make install PREFIX=/usr/local/haproxy

配置    
#安装完后会在安装目录下生成三个目录
    doc sbin  share
#cd /usr/local/haproxy
#vi haproxy.cfg             #默认没有这个文件,需要自己手动创建
global
    maxconn 51200
    uid 99
    gid 99
    daemon
    pidfile /usr/local/haproxy/logs/haproxy.pid
        mode http #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK  
        #retries 2 #两次连接失败就认为是服务器不可用,也可以通过后面设置  
        option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器  
        option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接  
        timeout connect 5000ms #连接超时  
        timeout client 30000ms #客户端超时  
        timeout server 30000ms #服务器超时  
        #timeout check 2000 #=心跳检测超时  
        log 127.0.0.1 local0 err #[err warning info debug]
          balance roundrobin                     #负载均衡算法  
#        option  httplog                        #日志类别,采用httplog  
#        option  httpclose   #每次请求完毕后主动关闭http通道,ha-proxy不支持keep-alive,只能模拟>这种模式的实现  
#        option  dontlognull  
#        option  forwardfor  #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip  

listen admin_stats
        bind 0.0.0.0:1080 #监听端口  
        option httplog #采用http日志格式  
        stats refresh 30s #统计页面自动刷新时间  
        stats uri /stats #统计页面url  
        stats realm Haproxy Manager #统计页面密码框上提示文本  
        stats auth admin:admin #统计页面用户名和密码设置  
        #stats hide-version #隐藏统计页面上HAProxy的版本信息
listen test2 :80
       option httpclose
       option forwardfor
       server s1 192.168.0.168:80 check weight 1 minconn 1 maxconn 3 check inter 40000
       server s2 192.168.0.198:80 check weight 1 minconn 1 maxconn 3 check inter 40000
启动haproxy
      /usr/local/haproxy/sbin/haproxy -f haproxy.cfg
最后进行测试
访问http://ip:1080/stats
访问http://ip:80

posted on 2017-10-25 18:06  标配的小号  阅读(136)  评论(0编辑  收藏  举报

导航