[转帖]HAProxy 7层 负载均衡

Posted jinanxiaolaohu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[转帖]HAProxy 7层 负载均衡相关的知识,希望对你有一定的参考价值。

 
https://www.cnblogs.com/jicki/p/5546902.html

HAProxy

 

系统 CentOS 5.8 x64

 

wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.26.tar.gz

 

cd haproxy-1.3.26

 

make TARGET=linux26 PREFIX=/opt/local/haproxy

 

make install PREFIX=/opt/local/haproxy

 

mkdir /opt/local/haproxy/conf

 

cd /opt/local/haproxy/conf/

 

vim haproxy.cfg

 

后台配置模式

------------------------------------------------------------------------------------------------

global

   log 127.0.0.1  local0

   maxconn 65535

   chroot /opt/local/haproxy

   uid 502

   gid 502

   daemon

   nbproc 8

   pidfile /opt/local/haproxy/haproxy.pid

 

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

    timeout connect   5000

    timeout client    50000

    timeout server    50000

 

listen  172.24.0.100

        bind *:80

        mode http

        option httplog

        log global

        option httpchk HEAD /index.jsp HTTP/1.0

        server web1 172.24.0.101:8080 weight 2  check inter 2000 rise 2 fall 3

        server web2 172.24.0.102:8080 weight 2  check inter 2000 rise 2 fall 3

 

 

 

 

 

 

添加 日志

 

vi /etc/syslog.conf      (centos 5.X版本)

vi /etc/rsyslog.conf     (centos 6.X版本)

 

local3.*                        /var/log/haproxy.log

 

local0.*                        /var/log/haproxy.log

 

 

vi /etc/sysconfig/syslog   (centos 5.X版本)

vi /etc/sysconfig/rsyslog  (centos 6.X版本)

 

SYSLOGD_OPTIONS="-m 0"

修改为

SYSLOGD_OPTIONS="-r -m 0"

 

重启日志服务

service syslog restart

 

 

/opt/local/haproxy/sbin/haproxy -f /opt/local/haproxy/haproxy.cfg    启动haproxy

 

 

或者用 脚本 来做启动 重启 关闭

 

vi /etc/init.d/haproxy

 

-----------------------------------------------------------------------------------------

#!/bin/bash

BASE_DIR="/opt/local/haproxy"

ARGV="$@"

 

start()

{

echo "START HAPoxy SERVERS"

$BASE_DIR/sbin/haproxy -f $BASE_DIR/conf/haproxy.cfg

}

 

stop()

{

echo "STOP HAPoxy Listen"

kill -TTOU $(cat $BASE_DIR/haproxy.pid)

echo "STOP HAPoxy process"

kill -USR1 $(cat $BASE_DIR/haproxy.pid)

}

case $ARGV in

 

start)

start

ERROR=$?

;;

 

stop)

stop

ERROR=$?

;;

 

restart)

stop

start

ERROR=$?

;;

 

*)

echo "hactl.sh [start|restart|stop]"

esac

exit $ERROR

 

--------------------------------------------------------------------------------------

 

chmod +x /etc/init.d/haproxy

 

 

 

1.4版本以后的 启动 出现

 

--------------------------------------------------------------------------------------------------

 

[WARNING] 022/092301 (26090) : Proxy ‘www.xxx.com‘: in multi-process mode, stats will be limited to process assigned to the current request.

 

那是因为配置文件 nbproc > 1 所导致的...可将 配置文件 nbproc 设置为 1 或者修改编译文件..

 

在源码配置src/cfgparse.c找到如下行

 

  if (nbproc > 1) {

   if (curproxy->uri_auth) {

   Warning("Proxy ‘%s‘: in multi-process mode, stats will be limited to process assigned to the current request. ", curproxy->id);

      if (!LIST_ISEMPTY(&curproxy->uri_auth->admin_rules)) {

   Warning("Proxy ‘%s‘: stats admin will not work correctly in multi-process mode. ", curproxy->id);

    }

 

 

修改 nbproc > 1 这个数值就可以...

----------------------------------------------------------------------------------------------------

 

 

option httpchk HEAD /index.php HTTP/1.0  这个为监控页面..在跟目录增加index.php文件,否则报503错误.

 

 

 

 

web监控 页面  http://www.xxxx.com/haproxy-stats

 

 

 

 

 

 

 

 

 

 

 

 

 

IP数据转发设置

-------------------------------------------------------------------------------

global   maxconn 65535 #最大连接数,HAProxy是数据转发,每条数据链路占用两个连接。        

daemon   #daemon方式运行        

 

nbproc 1

group nobody

user nobody

 

defaults

timeout server  86400s        

timeout client  86400s                

timeout connect 8640s                

#log 127.0.0.1 local7.* warning

 

listen  xxxxx_80  

bind 本机IP:80

mode tcp    #tcp模式,只转发,不分析        

option tcpka          

option srvtcpka        

option clitcpka        

server xxxxxx_80_src 目标IP:80 check inter 1000 rise 1 fall 2 #目标服务器定义  

 

-------------------------------------------------------------------------------------

以上是关于[转帖]HAProxy 7层 负载均衡的主要内容,如果未能解决你的问题,请参考以下文章

基于Haproxy的高可用实战

Haproxy详解以及基于Haproxy的高可用实战

haproxy+keepalive负载均衡环境部署(主主模式)

Web服务器群集——HAproxy+Nginx负载均衡集群

负载均衡集群-Lvs

Linux企业运维——haproxy负载均衡