1.安装haproxy
1) 解压包
[root@localhost ~]# tar xzvf haproxy-1.5.3.tar.gz
2)查看内核版本
[root@localhost ~]# uname -a
Linux localhost.localdomain 2.6.32-358.el6.x86_64 #1 SMP Tue Jan 29 11:47:41 EST 2013 x86_64 x86_64 x86_64 GNU/Linux
<pre name="code" class="plain">[root@localhost ~]# <span style="font-family: Arial, Helvetica, sans-serif;">make TARGET=linux26 PREFIX=/usr/local/haprpxy</span>
[root@localhost ~]# make install PREFIX=/usr/local/haproxy
4)编写配置文件
[root@localhost ~]# cd /usr/local/haproxy
[root@localhost ~]# vim 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 #以后台形式运行HAProxy
#debug
#quieti
nbproc #启动1个实例,可以启多个来提高效率
pidfile /var/run/haproxy.pid
defaults
log global
mode http
option httpclose #每次请求完毕后主动关闭http通道
option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
option forwardfor
option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
option dontlognull #保证HAProxy不记录上级负载均衡发送过来的用于检测状态没有数据的心跳包
retries 2 #重试次数
maxconn 2000 #最大连接数
balance source #如果想让HAProxy按照客户端的IP地址进行负载均衡策略,即同一IP地址的所有请求都发送到同一服务器时需要配置此选项
timeout connect 5000
timeout client 50000
timeout server 50000
listen web_proxy 0.0.0.0:80
mode http
balance roundrobin
server web1 172.16.0.181:80 cookie app1inst1 check inter 2000 rise 2 fall 5
server web2 172.16.0.182:80 cookie app1inst2 check inter 2000 rise 2 fall 5
listen admin_stats
bind *:8888 #监听端口
mode http #http的7层模式
option httplog #采用http日志格式
log 127.0.0.1 local0 err
maxconn 10
stats refresh 30s #统计页面自动刷新时间
stats uri /stats #统计页面url
stats realm XingCloud\ Haproxy #统计页面密码框上提示文本
stats auth admin:wudi32 #统计页面用户名和密码设置
stats hide-version #隐藏统计页面上HAProxy的版本信息
5) 启动haproxy
[root@localhost haproxy]# haproxy -f /usr/local/haproxy/haproxy.cfg
启动报错
[root@localhost conf]# haproxy -f /usr/local/haproxy/conf/haproxy.cfg
[WARNING] 222/220838 (1685) : parsing [/usr/local/haproxy/conf/haproxy.cfg:21]: keyword 'redispatch' is deprecated in favor of 'option redispatch', and will not be supported by future versions.
[WARNING] 222/220838 (1685) : parsing [/usr/local/haproxy/conf/haproxy.cfg:24] : the 'contimeout' directive is now deprecated in favor of 'timeout connect', and will not be supported in future versions.
[WARNING] 222/220838 (1685) : parsing [/usr/local/haproxy/conf/haproxy.cfg:25] : the 'clitimeout' directive is now deprecated in favor of 'timeout client', and will not be supported in future versions.
[WARNING] 222/220838 (1685) : parsing [/usr/local/haproxy/conf/haproxy.cfg:26] : the 'srvtimeout' directive is now deprecated in favor of 'timeout server', and will not be supported in future versions.
原有版本的参数,在此版本中已经废弃了
contimeout 被 timeout connect取代
clitimeout 被timeout client取代
srvtimeout 被timeout server取代
[root@localhost haproxy]# killall haproxy
7) 访问
http://172.16.0.221:1080/stats
8)编写启动脚本
[root@localhost ~] vim /etc/rc.d/init.d/haproxy
<pre name="code" class="plain">[root@localhost ~] chmod +x <span style="font-family: Arial, Helvetica, sans-serif;">/etc/rc.d/init.d/haproxy</span>
#!/bin/bash
#
# haproxy
# chkconfig: 35 85 15
# description: HAProxy scripts
# processname: haproxy
# config: /usr/local/haproxy/conf/haproxy.cfg
# pidfile: /usr/local/haproxy/run/haproxy.pid
if [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
. /etc/rc.d/init.d/functions
else
exit 0
fi
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
config="/usr/local/haproxy/conf/haproxy.cfg"
exec="/usr/local/haproxy/sbin/haproxy"
PID="/var/run/haproxy.pid"
[ -f $config ] || exit 1
RETVAL=0
start() {
daemon $exec -c -q -f $config
if [ $? -ne 0 ]; then
echo "Errors found in configuration file."
return 1
fi
echo -n "Starting HAproxy: "
$exec -D -f $config -p $PID
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/haproxy
return $RETVAL
}
stop() {
echo -n "Shutting down HAproxy: "
killproc haproxy -USR1
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/haproxy
[ $RETVAL -eq 0 ] && rm -f $PID
return $RETVAL
}
restart() {
$exec -c -q -f $config
if [ $? -ne 0 ]; then
echo "Errors found in configuration file, check it with 'haproxy check'."
return 1
fi
stop
start
}
rhstatus() {
status haproxy
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
rhstatus
;;
*)
echo $"Usage: haproxy {start|stop|restart|status}"
RETVAL=1
esac
exit $RETVAL
[root@localhost ~] chkconfig --add haproxy
HAProxy的算法非常多,常用的算法有如下8种:
balance roundrobin,表示简单的轮询,建议关注;
balance static-rr,表示根据权重,建议关注;
balance leastconn,表示最少连接者先处理,建议关注;
balance source,表示根据请求源IP,跟Nginx的ip_hash算法相似,建议关注;
balance uri,表示根据请求的URI;
balance url_param,表示根据请求的URl参数;
balance hdr(name),表示根据HTTP请求头来锁定每一次HTTP请求;
balance rdp-cookie(name),表示根据据cookie(name)来锁定并哈希每一次TCP请求。