Haproxy 的配置相当简单,
# tar zcvf haproxy-1.3.20.tar.gz # cd haproxy-1.3.20 # make TARGET=linux26 PREFIX=/usr/local/haprpxy # make install PREFIX=/usr/local/haproxy
安装完毕后,进入安装目录创建配置文件 # cd /usr/local/haproxy
# vi haproxy.cfg
配置内容如下: global
log 127.0.0.1 local0
#log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
chroot /usr/local/haproxy
uid 99 #所属运行的用户uid
gid 99 #所属运行的用户组
daemon
nbproc 1
pidfile /usr/local/haproxy/run/haproxy.pid
#debug
#quiet defaults
log global
log 127.0.0.1 local3 #日志文件的输出定向
mode http #所处理的类别
option httplog #日志类别
option httpclose
option dontlognull
option forwardfor
option redispatch
retries 2 #设置多个haproxy并发进程提高性能
maxconn 2000
balance roundrobin #负载均衡算法
stats uri /haproxy-stats #haproxy 监控页面的访问地址
# 可通过 http://localhost:1080/haproxy-stats 访问
contimeout 5000
clitimeout 50000
srvtimeout 50000 listen localhost 0.0.0.0:1080 #运行的端口及主机名
mode http
option httpchk GET /index.htm #健康检测
server s1 127.0.0.1:3121 weight 3 check #后端的主机 IP &权衡
server s2 127.0.0.1:3122 weight 3 check #后端的主机 IP &权衡
启动服务:
# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg 重启服务:
# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg -st `cat /usr/local/haproxy/logs/haproxy.pid` (没有换行) 停止服务:
# killall haproxy 当然,为了方便系统在开机时加载,还可以创建启动脚本:
# vim /etc/rc.d/init.d/haproxy 内容如下: #! /bin/sh
set -e
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/haproxy/sbin
PROGDIR=/usr/local/haproxy
PROGNAME=haproxy
DAEMON=$PROGDIR/sbin/$PROGNAME
CONFIG=$PROGDIR/conf/$PROGNAME.conf
PIDFILE=$PROGDIR/run/$PROGNAME.pid
DESC="HAProxy daemon"
SCRIPTNAME=/etc/init.d/$PROGNAME
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
start()
{
echo -n "Starting $DESC: $PROGNAME"
$DAEMON -f $CONFIG
echo "."
}
stop()
{
echo -n "Stopping $DESC: $PROGNAME"
haproxy_pid=cat $PIDFILE
kill $haproxy_pid
echo "."
}
restart()
{
echo -n "Restarting $DESC: $PROGNAME"
$DAEMON -f $CONFIG -p $PIDFILE -sf $(cat $PIDFILE)
echo "."
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
exit 1
;;
esac
exit 0 保存后赐予可执行权限
# chmod +x /etc/rc.d/init.d/haproxy 就可以使用 service haproxy start|stop|restart 来控制服务的启动停止跟重启。
并通过以下命令加载到开机服务启动列表
# chkconfig --add haproxy
配置日志:
# vim /etc/syslog.conf 在最下边增加
local3.* /var/log/haproxy.log
local0.* /var/log/haproxy.log
重启核心日志服务使配置起效
# service syslog restart
然后就可查看日志了
# tail –f /var/log/harpoxy.log Aug 22 15:32:06 localhost haproxy[64136]: Proxy www started.
Aug 22 15:32:06 localhost haproxy[64136]: Proxy cherokee started.
Aug 22 15:32:06 localhost haproxy[64136]: Proxy wap started.
Aug 22 15:32:06 localhost haproxy[64136]: Proxy pic started.
Aug 22 15:32:06 localhost haproxy[64136]: Proxy img started.
Aug 22 15:32:06 localhost haproxy[64136]: Proxy public started.
Aug 22 15:32:06 localhost haproxy[64136]: Proxy public started.
Aug 22 15:32:59 localhost haproxy[64137]: 219.142.128.30:6416 [22/Aug/2009:15:32:59.754] public stats/<STATS> 0/-1/-1/-1/0 200 17329 - - PR-- 0/0/0/0/0 0/0 "GET /?stats HTTP/1.1"
Aug 22 15:32:59 localhost haproxy[64137]: 219.142.128.30:6416 [22/Aug/2009:15:32:59.754] public stats/<STATS> 0/-1/-1/-1/0 200 17329 - - PR-- 0/0/0/0/0 0/0 "GET /?stats HTTP/1.1"
|