HAProxy 负载均衡

怎么合理的利用新采购服务器的资源呢(原来就一台前端服务器),第一就想到用nginx来做负载均衡,大家都知道nginx好是好,但是session没办法同步,是否没有办法了?办法是有的,可以利用HAProxy 来祢补nginx的不足的地方.


213A63456-0.png



一:安装 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]groupa dd 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
#pi dfile /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 redis patch
re tries 2
maxconn 2000
bala nce roundrobin
stats uri /haproxy-stats
con timeout 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/

213A64508-1.jpg


你可能感兴趣的:(linux,负载均衡)