HAProxy IP:192.168.240.132 Centos5.5-32bit
Web1 IP:192.168.240.128 Centos5.5-32bit
Web2 IP:192.168.240.130 Centos5.5-32bit
具体安装及配置文档如下:
一、HAProxy的下载及安装过程:
wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz
# 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
mkdir conf
cd conf
vim haproxy.cfg
内容 如下:
global
log 127.0.0.1 local0
maxconn 4096
chroot /usr/local/haproxy
uid 99
gid 99
daemon
nbproc 1
pidfile /usr/local/haproxy/logs/haproxy.pid
debug
defaults
log 127.0.0.1 local3
mode http
option httplog
option httpclose
option dontlognull
option forwardfor
option redispatch
retries 2
maxconn 2000
balance roundrobin
stats uri /haproxy-stats
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen web_proxy 192.168.240.132:80
#option httpchk HEAD /index.php HTTP/1.0
server web1 192.168.240.128:80 cookie app1inst1 check inter 2000 rise 2 fall 5
server web2 192.168.240.130:80 cookie app1inst2 check inter 2000 rise 2 fall 5
配置文件刚从服务器上copy下来的,保证可用。这里有个事情说明一下,有时候我们进入页面http://192.168.4.192/就会报如下错误:
503 Service UnavailableNo server is available to handle this request.
option httpchk HEAD /check.txt HTTP/1.0
此问题出在这句话上面,它的意思是Haproxy会判断你的后端web的根上存在check.txt没有,以此作为haproxy-status的监控状态依据,将它#掉即可;如果是生产环境,你可将check.txt改为index.jsp或index.php即可;
另外,建议配置一个HAProxy的启动、关闭、重启脚本,放在/etc/init.d下,我们在平时的工作中应该养成一个习惯,服务器的启动或重启应该是最简单的,万一到了生产环境下出错时,我们可以以最快的方式启动,如果不用脚本的话,我们就要尝试以如下方式启动:
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg
以下脚本生成后,我们直接可以用/etc/init.d/haproxy start来启动HAProxy服务了。
#!/bin/bash
BASE_DIR="/usr/local/haproxy"
ARGV="$@"
start()
{
echo "START HAPoxy SERVERS"
$BASE_DIR/sbin/haproxy -f $BASE_DIR/conf/haproxy.cfg
}
stop()
{
echo "STOP HAPoxy Listen"
kill -TTOU $(cat $BASE_DIR/logs/haproxy.pid)
echo "STOP HAPoxy process"
kill -USR1 $(cat $BASE_DIR/logs/haproxy.pid)
}
case $ARGV in
start)
start
ERROR=$?
;;
stop)
stop
ERROR=$?
;;
restart)
stop
start
ERROR=$?
;;
*)
echo "hactl.sh [start|restart|stop]"
esac
exit $ERROR
我们在Firefox或IE上输入http://192.168.240.132 就可以轮询的看到后端的web的页面了,另外我们可以随时监控页面http://192.168.240.132/haproxy-status/,效果图如下: