#### 1. 下载安装 ~~~ #下载 wget http://www.haproxy.org/download/1.7/src/haproxy-1.7.8.tar.gz #解压 tar -zxvf haproxy-1.7.8.tar.gz cd haproxy-1.7.8 #安装 make TARGET=generic ARCH=x86_64 PREFIX=/usr/local/haproxy make install PREFIX=/usr/local/haproxy #参数说明 TARGET=linux26 #内核版本,使用uname -r查看内核,如:2.6.18-371.el5,此时该参数就为linux26;kernel 大于2.6.28的用:TARGET=linux2628 ARCH=x86_64 #系统位数 PREFIX=/usr/local/haprpxy #/usr/local/haprpxy为haprpxy安装路径 ~~~ #### 2. 配置 > * 需要手动创建/usr/local/haproxy/haproxy.cfg ~~~ sudo mkdir -p /var/run/haproxy/ sudo mkdir -p /var/run/haproxy/chroot ~~~ ~~~ global log 127.0.0.1 local2 err pidfile /var/run/haproxy/haproxy.pid chroot /var/run/haproxy/chroot maxconn 10000 user haproxy group haproxy daemon nbproc 2 defaults mode http retries 3 timeout connect 10s timeout client 50s timeout server 50s #maxconn 4096 listen stats bind 0.0.0.0:8888 stats enable stats hide-version stats refresh 30s stats uri /haproxy-status stats auth haproxy:haproxy frontend frontend_ng bind *:8080 default_backend backend_ng backend backend_ng option forwardfor option httpchk HEAD / HTTP/1.0 balance source server n1 192.168.56.131:8089 check inter 2000 rise 30 fall 15 server n2 192.168.56.132:8088 check inter 2000 rise 30 fall 15 frontend frontend_timing bind *:32001 default_backend backend_timing backend backend_timing option forwardfor option httpchk HEAD / HTTP/1.0 balance roundrobin server www1 10.30.46.31:31001 check inter 2000 rise 30 fall 15 server www2 10.30.48.211:31001 check inter 2000 rise 30 fall 15 ~~~ * 启动 `sudo /usr/local/haproxy/sbin/haproxy -D -f /usr/local/haproxy/haproxy.cfg` ~~~ roundrobin:表示简单的轮询,这个不多说,这个是负载均衡基本都具备的; static-rr:表示根据权重,建议关注; leastconn:表示最少连接者先处理,建议关注; source:表示根据请求源IP,这个跟Nginx的IP_hash机制类似,我们用其作为解决session问题的一种方法,建议关注;,会出现一个IP多次请求都发送到一个地址 ~~~ ### 3. 自启动 > 把下面这个脚本放到/etc/init.d目录下,赋予执行权限,就可以设置haproxy开机自启动了,自启动办法见Linux常用命令中自启动一节 ~~~ #!/bin/sh ### BEGIN INIT INFO # Provides: haproxy # Required-Start: $local_fs $network $remote_fs # Required-Stop: $local_fs $remote_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: fast and reliable load balancing reverse proxy # Description: This file should be used to start and stop haproxy. ### END INIT INFO #set -x # Author: Arnaud Cornet <acornet@debian.org> PATH=/sbin:/usr/sbin:/bin:/usr/bin PIDFILE=/var/run/haproxy.pid CONFIG=/etc/haproxy/haproxy.cfg HAPROXY=/usr/sbin/haproxy EXTRAOPTS= ENABLED=1 test -x $HAPROXY || exit 0 if [ -e /etc/default/haproxy ]; then . /etc/default/haproxy fi test -f "$CONFIG" || exit 0 #test "$ENABLED" != "0" || exit 0 [ -f /etc/default/rcS ] && . /etc/default/rcS . /lib/lsb/init-functions clean() { if [ -e "$tmp" ];then rm -f "$tmp" fi } trap clean EXIT check_haproxy_config() { $HAPROXY -c -f "$CONFIG" >/dev/null if [ $? -eq 1 ]; then log_end_msg 1 exit 1 fi } haproxy_start() { check_haproxy_config start-stop-daemon --quiet --oknodo --start --pidfile "$PIDFILE" \ --exec $HAPROXY -- -f "$CONFIG" -D -p "$PIDFILE" \ $EXTRAOPTS || return 2 return 0 } haproxy_stop() { tmp=$(tempfile -s .haproxy.init) if [ ! -f $PIDFILE ] ; then # This is a success according to LSB return 0 fi ret=0 for pid in $(cat $PIDFILE); do echo $pid > "$tmp" start-stop-daemon --quiet --oknodo --stop \ --retry 5 --pidfile "$tmp" --exec $HAPROXY || ret=$? done [ $ret -eq 0 ] && rm -f $PIDFILE return $ret } haproxy_reload() { check_haproxy_config $HAPROXY -f "$CONFIG" -p $PIDFILE -D $EXTRAOPTS -sf $(cat $PIDFILE) \ || return 2 return 0 } haproxy_status() { if [ ! -f $PIDFILE ] ; then # program not running return 3 fi for pid in $(cat $PIDFILE) ; do if ! ps --no-headers p "$pid" | grep haproxy > /dev/null ; then # program running, bogus pidfile return 1 fi done return 0 } case "$1" in start) log_daemon_msg "Starting haproxy" "haproxy" haproxy_start ret=$? case "$ret" in 0) log_end_msg 0 ;; 1) log_end_msg 1 echo "pid file '$PIDFILE' found, haproxy not started." ;; 2) log_end_msg 1 ;; esac exit $ret ;; stop) log_daemon_msg "Stopping haproxy" "haproxy" haproxy_stop ret=$? case "$ret" in 0|1) log_end_msg 0 ;; 2) log_end_msg 1 ;; esac exit $ret ;; reload|force-reload) log_daemon_msg "Reloading haproxy" "haproxy" haproxy_reload ret=$? case "$ret" in 0|1) log_end_msg 0 ;; 2) log_end_msg 1 ;; esac exit $ret ;; restart) log_daemon_msg "Restarting haproxy" "haproxy" haproxy_stop haproxy_start ret=$? case "$ret" in 0) log_end_msg 0 ;; 1) log_end_msg 1 ;; 2) log_end_msg 1 ;; esac exit $ret ;; status) haproxy_status ret=$? case "$ret" in 0) echo "haproxy is running." ;; 1) echo "haproxy dead, but $PIDFILE exists." ;; *) echo "haproxy not running." ;; esac exit $ret ;; *) echo "Usage: /etc/init.d/haproxy {start|stop|reload|restart|status}" exit 2 ;; esac : ~~~