且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

HAProxy 负载均衡

更新时间:2022-09-21 15:25:19

最近公司的某个业务发展速度之快,导致前端服务器的压力非常的大.为了能够尽快的解决此问题,向公司提交个采购服务器的要求,很快公司也批了,服务器也到位了.现在的问题怎么合理的利用新采购服务器的资源呢(原来就一台前端服务器),第一就想到用nginx来做负载均衡,大家都知道nginx好是好,但是session没办法同步,是否没有办法了?办法是有的,可以利用HAProxy 来祢补nginx的不足的地方.

     
 

HAProxy 负载均衡



一:安装 HAProxy 

 
 

[root@xutest src]cd /usr/local/  
[root@xutest src]wget http://www.dbasky.net/tool/haproxy-1.3.15.7.tar.gz   
[root@xutest src]tar -zxvf haproxy-1.3.15.7.tar.gz   
[root@xutest src]cd haproxy-1.3.15.7   
[root@xutest src]make TARGET=linux26 USE_STATIC_PCRE=1  
[root@xutest src]make install PREFIX=/home   


二:配置

1)新建haproxy 用户组和用户:

 

[root@xutest src]groupadd haproxy
[root@xutest src]useradd haproxy -g haproxy


2)查看用户ID:

 

[root@xutest src]vi /etc/passwd
haproxy:x:500:500::/home/haproxy:/bin/bash


3)新建HAProxy目录:

 

[root@xutest opt]#mkdir /opt/haproxy
[root@xutest opt]#touch /opt/haproxy/haproxy.cfg


4)配置haproxy.cfg

 

[root@xutest opt]#vi /opt/haproxy/haproxy.cfg
global
        log 127.0.0.1   local0
        maxconn 4096
        chroot /opt/haproxy
        uid 500
        gid 500
        daemon
        nbproc 1
        #pidfile /home/haproxy/logs/haproxy.pid
        #debug
        #quiet

defaults
        log     127.0.0.1       local3
        mode    http
        option  httplog
        option  httpclose
        option  dontlognull
        option  forwardfor
        option  redispatch
        retries 2
        maxconn 2000
        balance roundrobin
        stats   uri     /haproxy-stats
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000

listen  web_proxy 0.0.0.0:8888
        option  httpchk

        server  web1 192.168.1.250:8080 weight 3 check
        server  web2 192.168.1.238:8080 weight 3 check



三:启动HAProxy主进程:

 

[root@xutest haproxy]# /usr/local/sbin/haproxy -f /opt/haproxy/haproxy.cfg


 停止HAProxy:

 

[root@xutest haproxy]# ps aux|grep haproxy
[root@xutest haproxy]# kill -9 + 进程ID


到此HAProxy配置完,我们可以通过浏览器查看:http://192.168.1.6:8888/haproxy-stats/

HAProxy 负载均衡






本文转自 freeterman 51CTO博客,原文链接:http://blog.51cto.com/myunix/1094986,如需转载请自行联系原作者