一:环境
服务器两台;
192.168.2.165 HAproxy
192.168.2.163 nginx (已有服务,此次配置不涉及)
服务器系统 centos6.5 64位
二:安装:
下载地址:百度云
http://pan.baidu.com/s/1i3jLnIt
cd /usr/local/src
tar xzvf haproxy-1.4.24.tar.gz
cd haproxy-1.4.24
make TARGET=linux26 PREFIX=/usr/local/haproxy-1.4.24
make install PREFIX=/usr/local/haproxy-1.4.24
cd /usr/local
ln -s /usr/local/haproxy-1.4.24 ./haproxy
groupadd haproxy
useradd -g haproxy haproxy
三:配置
将以下启动脚本放到/etc/init.d/haproxy 下,并赋予执行权限
#!/bin/bash
#
# haproxy
#
# chkconfig: 35 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: /etc/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
config="/etc/haproxy.cfg"
exec="/usr/local/haproxy/sbin/haproxy"
prog=$(basename $exec)
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
lockfile=/var/lock/subsys/haproxy
check() {
$exec -c -V -f $config
}
start() {
$exec -c -q -f $config
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 $config -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 $config
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
stop
start
}
reload() {
$exec -c -q -f $config
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Reloading $prog: "
$exec -D -f $config -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
;;
checkconfig)
check
;;
status)
fdr_status
;;
condrestart|try-restart)
[ ! -f $lockfile ] || restart
;;
*)
echo $"Usage: $0 {start|stop|status|checkconfig|restart|try-restart|reload|force-reload}"
exit 2
esac
HAproxy配置文件:/etc/haproxy.cfg
global
log 127.0.0.1 local3
maxconn 20480
chroot /usr/local/haproxy
group haproxy
user haproxy
daemon
quiet
nbproc 1
pidfile /var/run/haproxy.pid
defaults
log global
mode http
maxconn 20480
option httplog
option httpclose
option forwardfor
option dontlognull
option redispatch
retries 3
balance roundrobin
contimeout 5000
clitimeout 50000
srvtimeout 50000
frontend http-in #前台
bind *:81
mode http
option httplog
log global
default_backend htmlpool #静态服务器池
backend htmlpool #后台
#balance leastconn#负载均衡算法
#option httpchk HEAD /index.htm HTTP/1.0 #健康检查
server web1 192.168.2.163:80 check port 80 inter 1000 fall 3 weight 20
frontend test-mysql
bind *:65224
mode tcp
log global
option tcplog
timeout client 3600s
backlog 4096
maxconn 50000
default_backend test-mysql-server
backend test-mysql-server
mode tcp
option log-health-checks
option redispatch
option tcplog
balance roundrobin
timeout connect 1s
timeout queue 5s
timeout server 3600s
server chaungshang-mysql-68 192.168.68.7:3306 check
四:启动
/etc/init.d/haproxy start
访问http://192.168.2.165:81 看到了163的80页面