1.下载并安装软件包
yum -y install make gcc pcre*
mkdir -p /data/app_platform/haproxy/conf/
useradd -r haproxy -s /sbin/nologin
wget http://www.haproxy.org/download/1.4/src/haproxy-1.4.26.tar.gz
tar zxvf haproxy-1.4.26.tar.gz
cd haproxy-1.4.26
make TARGET=linux26 PREFIX=/data/app_platform/haproxy/
make install PREFIX=/data/app_platform/haproxy/
2.添加配置文件
/data/app_platform/haproxy/conf/haproxy.cfg
global chroot /data/app_platform/haproxy/share/ log 127.0.0.1 local3 info daemon user haproxy group haproxy pidfile /var/run/haproxy.pid nbproc 1 stats socket /tmp/haproxy level admin stats maxconn 20 node master_loadbalance1 description xxxx maxconn 65536 nosplice spread-checks 3 defaults option httpchk GET /php-fpm_status log global mode http option httpclose option abortonclose no option accept-invalid-http-response option allbackups #option http-server-close #option httplog #option log-health-checks #option tcp-smart-connect option tcpka option redispatch option forwardfor except 127.0.0.1 retries 3 timeout check 60s timeout connect 600s timeout http-keep-alive 600s timeout http-request 600s timeout queue 600s timeout server 600s timeout tarpit 60s timeout client 600s frontend game_pool 0.0.0.0:80 mode http no option accept-invalid-http-request option http-server-close log global option log-separate-errors #option forwardfor except 127.0.0.1 header X-Real-IP option forwardfor except 127.0.0.1 # acl path_web_inf path_beg -i /WEB-INF # block if path_web_inf acl game_url hdr_reg(host) -i ^(s\d*.xxxx.com|s\d*.xxx.com|s\d*.xxx.com|s\d*.xxx.com)$ backend game_pool mode http balance roundrobin cookie SERVERID insert indirect # http-check disable-on-404 # http-check send-state # option httpchk GET /check.php HTTP/1.1\r\nHost:\ example.com default-server inter 2s fastinter 1s downinter 5s slowstart 60s rise 2 fall 5 weight 30 server xxx-taiwan-game1 192.168.1.183:80 cookie xxx-taiwan-game1_80 check maxconn 2000 server xxx-taiwan-game2 192.168.1.184:80 cookie xxx-taiwan-game2_80 check maxconn 2000 listen monitor1 0.0.0.0:10443 mode http balance roundrobin stats enable stats uri /nc-haproxy # - default is haproxy?stats stats realm Haproxy\ statistics # - Enable statistics and set authentication realm stats show-desc example gintama-taiwan-lb1 status page stats auth ncadmin:taiwan stats refresh 30s stats show-legends # - Enable reporting additional informations on the statistics page timeout check 10s timeout client 45s timeout connect 45s timeout http-keep-alive 45s timeout http-request 45s timeout queue 45s timeout server 45s timeout tarpit 45s
3.添加启动脚本
#!/bin/sh # # haproxy # # chkconfig: - 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: /data/app_platform/haproxy/conf/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 exec="/data/app_platform/haproxy/sbin/haproxy" prog=$(basename $exec) conf_file="/data/app_platform/haproxy/conf/haproxy.cfg" [ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog lockfile=/var/lock/subsys/haproxy check() { $exec -c -V -f $conf_file } start() { $exec -c -q -f $conf_file 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 $conf_file -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 $conf_file if [ $? -ne 0 ]; then echo "Errors in configuration file, check with $prog check." return 1 fi stop start } reload() { $exec -c -q -f $conf_file if [ $? -ne 0 ]; then echo "Errors in configuration file, check with $prog check." return 1 fi echo -n $"Reloading $prog: " $exec -D -f $conf_file -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 ;; check) check ;; status) fdr_status ;; condrestart|try-restart) [ ! -f $lockfile ] || restart ;; *) echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}" exit 2 esac
4.添加日志输出
/etc/rsyslog.d/haproxy.conf
local3.* /var/log/haproxy.log $ModLoad imudp $UDPServerAddress 127.0.0.1 $UDPServerRun 514
service rsyslog restart
5.启动HAProxy
service haproxy start
tail -f /var/log/haproxy.log Feb 9 17:43:53 127.0.0.1 haproxy[25770]: backend game_pool has no server available! Feb 9 17:44:02 127.0.0.1 haproxy[25810]: Proxy game_pool started. Feb 9 17:44:02 127.0.0.1 haproxy[25810]: Proxy game_pool started. Feb 9 17:44:02 127.0.0.1 haproxy[25810]: Proxy monitor1 started. Feb 9 17:44:03 127.0.0.1 haproxy[25828]: Proxy game_pool started. Feb 9 17:44:03 127.0.0.1 haproxy[25828]: Proxy game_pool started. Feb 9 17:44:03 127.0.0.1 haproxy[25828]: Proxy monitor1 started. Feb 9 17:44:05 127.0.0.1 haproxy[25829]: Server game_pool/xxx-taiwan-game1 is DOWN, reason: Layer4 timeout, check duration: 2001ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue. Feb 9 17:44:05 127.0.0.1 haproxy[25829]: Server game_pool/xxx-taiwan-game2 is DOWN, reason: Layer4 timeout, check duration: 2001ms. 0 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue. Feb 9 17:44:05 127.0.0.1 haproxy[25829]: backend game_pool has no server available!
6.设置HAProxy开机启动
chkconfig --level 35 haproxy on
时间: 2024-10-15 09:10:03