1、安装haproxy
#tar xzvf haproxy-1.5.9.tar.gz
#cd haproxy-1.5.9
# make TARGET=linux26 PREFIX=/usr/local/haproxy
# make install PREFIX=/usr/local/haproxy
# mkdir /usr/local/haproxy/etc
# mkdir -p /data/logs/haproxy
2、编辑haproxy 配置文件
# vi /usr/local/haproxy/etc/haproxy.conf
global log 127.0.0.1 local0 maxconn 4096 chroot /usr/local/haproxy uid 0 gid 0 daemon nbproc 1 pidfile /usr/local/haproxy/haproxy.pid #debug ulimit-n 65535 defaults log global mode http retries 3 option httplog option httpclose option dontlognull option forwardfor option abortonclose log 127.0.0.1 local3 #balance roundrobin maxconn 2048 timeout connect 5000 timeout client 50000 timeout server 50000 listen admin_stats bind *:8000 stats refresh 30s stats uri /status stats realm Haproxy Manager stats auth admin:admintest stats hide-version frontend www bind *:80 acl web_fp path_beg -i / use_backend www_site if web_fp frontend web_manage bind *:8080 acl web_mgr path_beg -i /web_manage use_backend mgr_site if web_mgr backend www_site #mode http #fullconn 4000 option httpchk GET /about.jsp stats refresh 2 balance roundrobin cookie SERVERID insert indirect server web_fp1 192.168.0.2:10000 cookie 1a check inter 2000 rise 2 fall 3 weight 50 server web_fp2 192.168.0.2:11000 cookie 2a check inter 2000 rise 2 fall 3 weight 50 backend mgr_site server web_mgr 192.168.0.2:9000 check inter 2000 rise 2 fall 2
3、编辑haproxy 服务脚本
#vi /etc/init.d/haproxy
#!/bin/sh #chkconfig: 35 86 86 #description: HAProxy is a TCP/HTTP reverse proxy. #processname: haproxy # config: /usr/local/haproxy/etc/haproxy.conf # pidfile: /usr/local/haproxy/haproxy.pid # PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin BINFILE="/usr/local/haproxy/sbin/haproxy" CFGFILE="/usr/local/haproxy/etc/haproxy.conf" PIDFILE="/usr/local/haproxy/haproxy.pid" LOCKFILE="/usr/local/haproxy/haproxy.lock" RETVAL=0 start() { [[ -x $BINFILE ]] || exit 5 [[ -f $CFGFILE ]] || exit 6 $BINFILE -c -q -f $CFGFILE [[ $? -ne 0 ]] && echo "The HAProxy configure has error." && return 1 echo -n "Starting HAProxy......" $BINFILE -f $CFGFILE -p $PIDFILE RETVAL=$? echo [[ $RETVAL -eq 0 ]] && touch $LOCKFILE return $RETVAL } stop() { echo -n "Shutting down HAProxy......" while true do killall $BINFILE [[ -z `ps aux | grep sbin/haproxy | grep -v grep` ]] && break done RETVAL=$? echo [[ $RETVAL -eq 0 ]] && rm -f $LOCKFILE $PIDFILE return $RETVAL } restart() { stop sleep 1 start } reload() { [[ -z `ps aux | grep sbin/haproxy | grep -v grep` ]] && echo "The HAProxy is not running." && return 1 echo -n $"Reloading HAProxy......" if [[ -f $PIDFILE ]]; then $BINFILE -f $CFGFILE -st `cat $PIDFILE` else $BINFILE -f $CFGFILE -st `ps aux | grep sbin/haproxy | grep -v grep | awk ‘{print $2}‘` fi RETVAL=$? echo return $RETVAL } case "$1" in start) start ;; stop) stop ;; restart) restart ;; reload) reload ;; condrestart) [[ -e $LOCKFILE ]] && restart || : ;; check) $BINFILE -c -q -V -f $CFGFILE ;; *) echo "Usage: service haproxy {start|stop|restart|reload|condrestart|check}" RETVAL=1 esac exit $RETVAL
4、编辑系统日志配置
# vim /etc/rsyslog.conf
默认有下面的设置,会读取 /etc/rsyslog.d/*.conf目录下的配置文件
$IncludeConfig /etc/rsyslog.d/*.conf
5、为haproxy创建一个独立的配置文件
# vim /etc/rsyslog.d/haproxy.conf
$ModLoad imudp
$UDPServerRun 514
local3.* /var/log/haproxy.log
#如果不加下面的的配置则除了在/var/log/haproxy.log中写入日志外,也会写入message文件
6、配置rsyslog的主配置文件,开启远程日志
# vim /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS=" -c 2 -r -m 0"
#-c 2 使用兼容模式,默认是 -c 5
#-r 开启远程日志
#-m 0 标记时间戳。单位是分钟,为0时,表示禁用该功能
7、重启haproxy和rsyslog服务
#/etc/init.d/rsyslog restart
#/etc/init.d/haproxy restart
8、查看haproxy 状态
#/etc/init.d/haproxy status
haproxy 安装完成。
本文出自 “工作日记” 博客,请务必保留此出处http://3920625.blog.51cto.com/3910625/1691924
原文地址:http://3920625.blog.51cto.com/3910625/1691924