HAProxy 7層 負載均衡

丶小炒肉發表於2016-05-31

系統 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.\n", curproxy->id);

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

   Warning("Proxy '%s': stats admin will not work correctly in multi-process mode.\n", 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 #目標伺服器定義  

 

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

相關文章