1、 到haproxy官网下载haproxy源码包如下
wget http://www.haproxy.org/download/1.5/src/haproxy-1.5.19.tar.gz
2、创建haproxy运行用户
groupadd -r haproxy
useradd -g haproxy -M -s /sbin/nologin haproxy
3、 编译安装haproxy
tar zxvf haproxy-1.5.19.tar.gz -C /usr/local/src
cd /usr/local/src/haproxy-1.5.19
make TARGET=linux26 ARCH=X86_64 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
注意:TARGET=Linux26 是通过uname -a 来查看Linux内核版本,如:
# uname -a
Linux host 2.6.32-220.el6.x86_64 #1 SMP Wed Nov 9 08:03:13 EST 2011 x86_64 x86_64 x86_64 GNU/Linux
4、 创建haproxy主配置文件:
mkdir /etc/haproxy/
cp haproxy-1.5.19/examples/haproxy.cfg /etc/haproxy/haproxy.cfg
四层代理示例:
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
mode tcp
log global
option tcplog
option redispatch
retries 3
timeout queue 1m
timeout connect 10000s
timeout client 300000
timeout server 300000
timeout check 10s
maxconn 60000
listen 5000
bind *:5000
mode tcp
balance roundrobin
server app1 1.1.1.1:5000
server app2 2.2.2.2:5000
server app3 3.3.3.3:5000
5、创建haproxy系统服务启动脚本:
cat >/etc/init.d/haproxy<<"EOF"
#!/bin/sh
#
# haproxy
#
# chkconfig: - 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/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
exec="/usr/local/haproxy/sbin/haproxy"
prog=$(basename $exec)
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
cfgfile=/etc/haproxy/haproxy.cfg
pidfile=/var/run/haproxy.pid
lockfile=/var/lock/subsys/haproxy
check() {
$exec -c -V -f $cfgfile $OPTIONS
}
start() {
$exec -c -q -f $cfgfile $OPTIONS
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 $cfgfile -p $pidfile $OPTIONS
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 $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
stop
start
}
reload() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Reloading $prog: "
$exec -D -f $cfgfile -p $pidfile $OPTIONS -sf $(cat $pidfile)
retval=$?
echo
return $retval
}
force_reload() {
restart
}
fdr_status() {
status $prog
}
case "$1" in
start|stop|restart|reload)
$1
;;
force-reload)
force_reload
;;
check)
check
;;
status)
fdr_status
;;
condrestart|try-restart)
[ ! -f $lockfile ] || restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"
exit 2
esac
EOF
6、授予可执行权限,启动HA服务并加入开机启动
chmod 777 /etc/init.d/haproxy
mkdir /var/lib/haproxy && touch /var/lib/haproxy/stats
service haproxy start
chkconfig --add haproxy
chkconfig --level 2345 haproxy on