编译安装haproxy:

1.下载haproxy的源码包

    # wget http://www.haproxy.org/download/1.5/src/haproxy-1.5.14.tar.gz  

2.编译安装

    # tar xf haproxy-1.5.14.tar.gz

    # cd haproxy-1.5.14

    # uname -a                  ###查看linux内核版本

    # make TARGET=linux26  PREFIX=/usr/local/haproxy

    # make install PREFIX=/usr/local/haproxy

3.提供配置文件(编译安装默认是没有配置文件的)

    # mkdir /etc/haproxy

    # cd /etc/haproxy

    # vim haproxy.cfg

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.3/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
   log    127.0.0.1 local2  ###[err warning info debug]
   chroot  /usr/local/haproxy
   pidfile  /var/run/haproxy.pid ###haproxy的pid存放路径,启动进程的用户必须有权限访问此文件
   maxconn  4000         ###最大连接数,默认4000
   user   haproxy
   group   haproxy
   daemon  ###创建1个进程进入deamon模式运行。此参数要求将运行模式设置为"daemon"

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
   mode   http   ###默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
   log    global     ###采用全局定义的日志
   option  dontlognull   ###不记录健康检查的日志信息
   option  httpclose    ###每次请求完毕后主动关闭http通道
   option  httplog     ###日志类别http日志格式
   option  forwardfor   ###如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip  
   option  redispatch   ###serverId对应的服务器挂掉后,强制定向到其他健康的服务器
   timeout connect 10000  #default 10 second timeout if a backend is not found
   timeout client 300000  ###客户端连接超时
   timeout server 300000  ###服务器连接超时
   maxconn     60000  ###最大连接数
   retries     3    ###3次连接失败就认为服务不可用,也可以通过后面设置
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main *:5000
   acl url_static path_beg    -i /static /p_w_picpaths /javascript /stylesheets
   acl url_static path_end    -i .jpg .gif .png .css .js

   use_backend static if url_static ###满足策略要求,则响应策略定义的backend页面
   default_backend   app       ###不满足则响应backend的默认页面

#---------------------------------------------------------------------
# static backend for serving up p_w_picpaths, stylesheets and such
#---------------------------------------------------------------------
backend static
   balance     roundrobin           ###负载均衡模式轮询
   server      static 127.0.0.1:4331 check ###后端服务器定义

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
   balance  roundrobin           ###负载均衡模式轮询
   server  app1 127.0.0.1:5001 check   ###后端服务器定义,check进行健康检查
   server  app2 127.0.0.1:5002 check
   server  app3 127.0.0.1:5003 check
   server  app4 127.0.0.1:5004 check

5.新建haproxy用户以安全方式运行进程

    # groupadd  -r  -g  158  haproxy

    # useradd -g haproxy -r -s /sbin/nologin -u 102 haproxy

6.配置haproxy服务的日志

    # vim /etc/rsysconfig/rsyslog 

           SYSLOGD_OPTIONS="-c 2 -r"

    # vim /etc/rsyslog.conf

           local2.*                       /var/log/haproxy.log                  ###在文件的最后添加一行

    # service rsyslog  restart                                                       ###重启系统日志服务使其生效

7.检查配置文件是否错误

    # /usr/local/haproxy/sbin/haproxy -c -f /etc/haproxy/haproxy.cfg

8.启动haproxy服务

    # /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg 

    # netstat -tnulp | grep haproxy   or  ps -ef | grep haproxy

9.关闭haproxy服务

    # killall  haproxy

10.提供haproxy服务启动脚本

    # vim /etc/rc.d/init.d/haproxy

#!/bin/bash
#
# haproxy
#
# chkconfig: 35 85 15
# description: HAProxy is a free, very fast and reliable solution \
# offering high availability, load balancing, and \
# proxying for TCP and HTTP-based applications
# processname: haproxy
# config: /etc/haproxy/haproxy.cfg
# pidfile: /var/run/haproxy.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

config="/etc/haproxy/haproxy.cfg"
exec="/usr/local/haproxy/sbin/haproxy"
prog=$(basename $exec)

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

lockfile=/var/lock/subsys/haproxy

check() {
   $exec -c -V -f $config
}

start() {
   $exec -c -q -f $config
   if [ $? -ne 0 ]; then
       echo "Errors in configuration file, check with $prog check."
       return 1
   fi

   echo -n $"Starting $prog: "
   # start it up here, usually something like "daemon $exec"
   daemon $exec -D -f $config -p /var/run/$prog.pid
   retval=$?
   echo
   [ $retval -eq 0 ] && touch $lockfile
   return $retval
}

stop() {
   echo -n $"Stopping $prog: "
   # stop it here, often "killproc $prog"
   killproc $prog
   retval=$?
   echo
   [ $retval -eq 0 ] && rm -f $lockfile
   return $retval
}

restart() {
   $exec -c -q -f $config
   if [ $? -ne 0 ]; then
       echo "Errors in configuration file, check with $prog check."
       return 1
   fi
   stop
   start
}

reload() {
   $exec -c -q -f $config
   if [ $? -ne 0 ]; then
       echo "Errors in configuration file, check with $prog check."
       return 1
   fi
   echo -n $"Reloading $prog: "
   $exec -D -f $config -p /var/run/$prog.pid -sf $(cat /var/run/$prog.pid)
   retval=$?
   echo
   return $retval
}

force_reload() {
   restart
}

fdr_status() {
   status $prog
}

case "$1" in
   start|stop|restart|reload)
       $1
       ;;
   force-reload)
       force_reload
       ;;
   checkconfig)
       check
       ;;
   status)
       fdr_status
       ;;
   condrestart|try-restart)
     [ ! -f $lockfile ] || restart
   ;;
   *)
       echo $"Usage: $0 {start|stop|status|checkconfig|restart|try-restart|reload|force-reload}"
       exit 2
esac

11.而后为此脚本赋予执行权限

    # chmod +x /etc/rc.d/init.d/haproxy

12.添加至服务管理列表,并让其开机自动启动

    # chkconfig  --add haproxy

    # chkconfig  haproxy on

    # service haproxy start

13.查看服务是否正常启动

    # ps -ef | grep haproxy

14.输出haproxy的man手册至man命令的查找路径

     # vim /etc/man.config,添加如下行即可

           MANPATH  /usr/local/haproxy/share/man

15.修改PATH环境变量,让系统可以直接使用haproxy的相关命令

    #  vim /etc/profile.d/haproxy.sh

           export PATH=$PATH:/usr/local/haproxy/sbin

16.重新读取环境变量的值

    # source /etc/profile