首页 > Linux系统管理 > 安装haproxy
2016
05-23

安装haproxy

最近给个项目新加台负载,安装haproxy

wget http://www.haproxy.org/download/1.6/src/haproxy-1.6.1.tar.gz
tar xvf haproxy-1.6.1.tar.gz
make TARGET=linux26 PREFIX=/usr/local/haproxy 
make install PREFIX=/usr/local/haproxy
mkdir /usr/local/haproxy/etc

因为我原来安装有haproxy,所以直接把原来的配置拷贝过来etc下
然后启动测试下

# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg 
[WARNING] 143/152146 (10696) : parsing [/usr/local/haproxy/etc/haproxy.cfg:27] : the 'contimeout' directive is now deprecated in favor of 'timeout connect', and will not be supported in future versions.
[WARNING] 143/152146 (10696) : parsing [/usr/local/haproxy/etc/haproxy.cfg:29] : the 'clitimeout' directive is now deprecated in favor of 'timeout client', and will not be supported in future versions.
[WARNING] 143/152146 (10696) : parsing [/usr/local/haproxy/etc/haproxy.cfg:30] : the 'srvtimeout' directive is now deprecated in favor of 'timeout server', and will not be supported in future versions.
[ALERT] 143/152146 (10696) : parsing [/usr/local/haproxy/etc/haproxy.cfg:33] : 'listen' cannot handle unexpected argument '0.0.0.0:10081'.
[ALERT] 143/152146 (10696) : parsing [/usr/local/haproxy/etc/haproxy.cfg:33] : please use the 'bind' keyword for listening addresses.
[ALERT] 143/152146 (10696) : Error(s) found in configuration file : /usr/local/haproxy/etc/haproxy.cfg
[WARNING] 143/152146 (10696) : config : proxy 'stats' has no 'bind' directive. Please declare it as a backend if this was intended.
[WARNING] 143/152146 (10696) : config : missing timeouts for proxy 'stats'.
   | While not properly invalid, you will certainly encounter various problems
   | with such a configuration. To fix this, please ensure that all following
   | timeouts are set to a non-zero value: 'client', 'connect', 'server'.
[ALERT] 143/152146 (10696) : Proxy 'http_80': unable to find required use_backend: 'zone_test'.
[WARNING] 143/152146 (10696) : stats socket will not work as expected in multi-process mode (nbproc > 1), you should force process binding globally using 'stats bind-process' or per socket using the 'process' attribute.
[WARNING] 143/152146 (10696) : Proxy 'stats': in multi-process mode, stats will be limited to process assigned to the current request.
[ALERT] 143/152146 (10696) : Fatal errors found in configuration.

原有版本的参数,在此版本中已经废弃了
contimeout 被 timeout connect取代

clitimeout 被timeout client取代

srvtimeout 被timeout server取代
‘listen’ cannot handle unexpected argument ‘0.0.0.0:10081’.
这个是指需要加上bind bind 0.0.0.0:10081
[WARNING] 143/153218 (11081) : stats socket will not work as expected in multi-process mode (nbproc > 1), you should force process binding globally using ‘stats bind-process’ or per socket using the ‘process’ attribute.
[WARNING] 143/153218 (11081) : Proxy ‘stats’: in multi-process mode, stats will be limited to process assigned to the current request.
这2个WARNING 是由于我的这台机器cpu才2核,原来的机器有开了8个线程,改成1个线程就没WARNING了

cat haproxy.cfg
global
        log     127.0.0.1 local0
        maxconn 4000
        ulimit-n 80170
        uid     500
        gid     500
#       chroot  /tmp
        nbproc  1
        daemon
        pidfile  /usr/local/haproxy/haproxy.pid
        stats socket  /var/run/haproxy.sock
#       debug
#       quiet
 
defaults
        log     127.0.0.1       local3
        mode    http
        option  httplog
        option  httpclose
        option  dontlognull
        option  abortonclose
        option  forwardfor
        option  redispatch
        retries 2
        maxconn 2000
        balance roundrobin
        timeout connect      5000
        timeout check   20000
        timeout client     150000
        timeout server      150000
 
 
listen stats
        bind 0.0.0.0:10081
        mode    http
        log     127.0.0.1 local0 err
        stats   uri     /hastats
        stats auth    admin:admin
        stats refresh 30s
        stats hide-version
        #option httpchk GET /Login.d
 
 
frontend http_80
        option forwardfor
        bind :80
        mode http
        maxconn 1500
        log     global
        option httplog
        # log the name of the virtual server
        capture request header Host len 30
        # log the beginning of the referrer
        capture request header Referer len 50
 
 
        acl  test  hdr_beg(host) -i  test.proxy.com
 
 
        use_backend zone_test  if test
 
 
backend  zone_test
        mode http
        option forwardfor
        option httpchk GET / HTTP/1.1\r\nHost:test.proxy.com
        server web01 192.168.7.138:80 check inter 9000 rise 3 fall 3
        server web02 192.168.7.139:80 check inter 9000 rise 3 fall 3
        server web03 192.168.40.254:80 check inter 9000 rise 3 fall 3
cat /etc/init.d/haproxy
#!/bin/bash
#===============================================================================
#
#         FILE:  ops_control_haproxy.sh
#
#        USAGE:  ./haproxy [start|stop|reload] 
# haproxy:  Start HAProxy
# chkconfig: 2345 20 80
# description:  To [start|stop|stop] haproxy service
#
#      OPTIONS:  [start|stop|reload]
# REQUIREMENTS:  None
#         BUGS:  None
#        NOTES:  None
#      CREATED:  2013/02/21 17时14分54秒
#     REVISION:  ---
#===============================================================================
PRO_HOME="/usr/local/haproxy"
PRO_NAME="haproxy"
CON_FILE="$PRO_HOME/etc/$PRO_NAME.cfg"
PID_FILE="$PRO_HOME/$PRO_NAME.pid"
DAEMON="$PRO_HOME/sbin/$PRO_NAME"
 
 
[ -x $DAEMON ] || exit 1
 
 
#start haproxy
start()
{
  echo -n "Starting HAPoxy ........"
  $DAEMON -f $CON_FILE
  echo "[OK]"
}
 
stop()
{
  echo  -n "STOP HAPoxy Listen ........"
  kill -TTOU $(cat $PID_FILE)
  echo "[OK]"
  echo  -n "STOP HAPoxy process ........"
  kill -USR1 $(cat $PID_FILE)
  echo "[OK]"
  [ -f ${PID_FILE} ] && rm -f ${PID_FILE}
}
 
 
reload()
{
  echo -n "Reload HAPoxy ........" 
  $DAEMON -f $CON_FILE -p $PID_FILE -sf `cat $PID_FILE`
  echo "[OK]"
}
 
case "$1" in
start)
start
;;
stop)
stop
;;
reload|restart)
reload
;;
*)
echo "Useage: $0 [start|stop|reload]"
;;
esac

启动后 http://192.168.7.247:10081/hastats 可以看下详细信息

最后编辑:
作者:saunix
大型互联网公司linux系统运维攻城狮,专门担当消防员

留下一个回复