Haproxy

原创
2013/03/12 11:41
阅读数 740

环境:

    haproxy 10.1.6.31

    backend 10.1.6.13、10.1.6.16、10.1.6.159、10.1.6.228 (每个web都设置不同内容)

1 下载源码包

wget ‘http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.22.tar.gz’
2 安装

tar zxvf haproxy-1.4.22.tar.gz
cd haproxy-1.4.22/
make TARGET=linux26
make install
    默认bin文件在:/usr/local/sbin/下

   默认没有生成配置文件,在源码包的examples目录下有配置文件示例haproxy.cfg

3 设置运行环境

mkdir /etc/haproxy
cp examples/haproxy.cfg /etc/haproxy/
cp /usr/local/sbin/haproxy /sbin/

4 设置init启动脚本

   先修改/etc/default/haproxy

#/etc/default/haproxy
# Set ENABLED to 1 if you want the init script to start haproxy.
ENABLED=1
# Add extra flags here.
EXTRAOPTS="-de -m 16"
PATH=/sbin:/usr/sbin:/bin:/usr/bin
PIDFILE=/var/run/haproxy.pid
CONFIG=/etc/haproxy/haproxy.cfg
HAPROXY=/sbin/haproxy
EXTRAOPTS=
ENABLED=0
test -x $HAPROXY || exit 0
test -f "$CONFIG" || exit 0
if [ -e /etc/default/haproxy ]; then
	. /etc/default/haproxy
fi
test "$ENABLED" != "0" || exit 0
[ -f /etc/default/rcS ] && . /etc/default/rcS
. /lib/lsb/init-functions
haproxy_start()
{
	start-stop-daemon --start --pidfile "$PIDFILE" \
		--exec $HAPROXY -- -f "$CONFIG" -D -p "$PIDFILE" \
		$EXTRAOPTS || return 2
	return 0
}
haproxy_stop()
{
	if [ ! -f $PIDFILE ] ; then
		# This is a success according to LSB
		return 0
	fi
	for pid in $(cat $PIDFILE) ; do
		/bin/kill $pid || return 4
	done
	rm -f $PIDFILE
	return 0
}
haproxy_reload()
{
	$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
	case "$?" in
	0|1)
		log_end_msg 0
		;;
	2)
		log_end_msg 1
		;;
	esac
	;;
restart)
	log_daemon_msg "Restarting haproxy" "haproxy"
	haproxy_stop
	haproxy_start
	case "$?" in
	0)
		log_end_msg 0
		;;
	1)
		log_end_msg 1
		;;
	2)
		log_end_msg 1
		;;
	esac
	;;
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
:
5 修改/etc/haproxy/haproxy.cfg

global
	log 127.0.0.1	local0
	maxconn 4096
	uid 99
	gid 99
	daemon
        nbproc 1
defaults
	log	global
	mode	http
	option	httplog
	option	dontlognull
        option  forwardfor
	retries	3
	option redispatch
	maxconn	2000
	contimeout	5000
	clitimeout	50000
	srvtimeout	50000
listen  web_test 0.0.0.0:80
        option httpchk
        balance roundrobin
        server s_1 10.1.6.13:80 check inter 20000 rise 2 fall 5
        server s_2 10.1.6.159:80 check inter 20000 rise 2 fall 5 
        server s_3 10.1.6.16:80 check inter 20000 rise 2 fall 5 
        server s_4 10.1.6.228:80 check inter 20000 rise 2 fall 5
6 启动haproxy

/etc/init.d/haproxy start
7 检验结果,在浏览器中输入10.1.6.31,然后每次刷新会出现不同的结果



http://cbonte.github.com/haproxy-dconv/configuration-1.4.html#option%20http-pretend-keepalive



展开阅读全文
加载中

作者的其它热门文章

打赏
0
1 收藏
分享
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部
返回顶部
顶部