haproxy负载均衡器的安装与使用步骤

发表时间:2014-08-26  分类:Linux  浏览次数:10376

    HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代 理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。

    其它详细介绍 ,就不在这里介绍了。常用负载均衡器有Lvs,nginx,haproxy,至于它们的优缺点,可以谷歌下。

    1,安装

    可以从官方下载 http://www.haproxy.org/#down 

    tar zcvf haproxy-1.5.3.tar.gz
cd haproxy-1.5.3
make TARGET=linux26 PREFIX=/usr/local/haproxy    #将haproxy安装到/usr/local/haproxy
make install PREFIX=/usr/local/haproxy

    2.服务启动与停止

    启动服务:
# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg
重启服务:
# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg -st `cat /usr/local/haproxy/logs/haproxy.pid` (没有换行)
停止服务:
# killall haproxy

    haproxy.cfg 在安装包下,是例子配置文件。

    下面贴上我的测试haproxy.cfg文件

    global
log 127.0.0.1   local0  
log 127.0.0.1   local1 notice
maxconn 4096
chroot /usr/share/haproxy
uid 99 
gid 99 
daemon  
#debug  
#quiet  
 
defaults
log     global  
mode    http    
option  httplog 
option  dontlognull
retries 3
#redispatch
maxconn 2000
option redispatch
#contimeout     5000
timeout connect 5000
#clitimeout     50000
timeout client 50000
timeout server 50000
#srvtimeout     50000
stats   uri     /haproxy
stats auth  cnlvzi:cnlvzi   #设置监控页面的用户和密码:cnlvzi
 
listen  appli1-rewrite 192.168.2.128:80
cookie  SERVERID rewrite 
mode http
balance roundrobin
option httpchk HEAD /index.html HTTP/1.0
server  app1_1 192.168.2.130:80 cookie app1inst1 weight 5 check inter 2000 rise 2 fall 5
server  app1_2 192.168.2.129:80 cookie app1inst2 weight 3 check inter 2000 rise 2 fall 5

    3,有关错误

    启动haproxy

    出现下面错误,一般只是参数过期,按提示改正就可

    [WARNING] 236/093506 (7418) : parsing [/usr/local/haproxy/haproxy.cfg:21]: keyword 'redispatch' is deprecated in favor of 'option redispatch', and will not be supported by future versions.
[WARNING] 236/093506 (7418) : parsing [/usr/local/haproxy/haproxy.cfg:23] : the 'contimeout' directive is now deprecated in favor of 'timeout connect', and will not be supported in future versions.
[WARNING] 236/093506 (7418) : parsing [/usr/local/haproxy/haproxy.cfg:24] : the 'clitimeout' directive is now deprecated in favor of 'timeout client', and will not be supported in future versions.
[WARNING] 236/093506 (7418) : parsing [/usr/local/haproxy/haproxy.cfg:25] : the 'srvtimeout' directive is now deprecated in favor of 'timeout server', and will not be supported in future versions.

    4.加上日志支持

    # vim /etc/syslog.conf

    在最下边增加

    local3.*         /var/log/haproxy.log
local0.*         /var/log/haproxy.log

    修改syslog

    #vim /etc/sysconfig/syslog

    修改: SYSLOGD_OPTIONS="-r -m 0"

    重启日志服务

    service syslog restart

    5.启动脚本

    当然,为了方便系统在开机时加载,还可以创建启动脚本:

    # vim /etc/rc.d/init.d/haproxy 内容如下:

    #! /bin/sh
set -e
 
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/haproxy/sbin
PROGDIR=/usr/local/haproxy
PROGNAME=haproxy
DAEMON=$PROGDIR/sbin/$PROGNAME
CONFIG=$PROGDIR/conf/$PROGNAME.conf
PIDFILE=$PROGDIR/run/$PROGNAME.pid
DESC=”HAProxy daemon”
SCRIPTNAME=/etc/init.d/$PROGNAME
 
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
 
start()
{
echo -n “Starting $DESC: $PROGNAME”
$DAEMON -f $CONFIG
echo “.”
}
 
stop()
{
echo -n “Stopping $DESC: $PROGNAME”
haproxy_pid=cat $PIDFILE
kill $haproxy_pid
echo “.”
}
 
restart()
{
echo -n “Restarting $DESC: $PROGNAME”
$DAEMON -f $CONFIG -p $PIDFILE -sf $(cat $PIDFILE)
echo “.”
}
 
case “$1″ in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo “Usage: $SCRIPTNAME {start|stop|restart}” >&2
exit 1
;;
esac
exit 0

    保存后赐予可执行权限

    # chmod +x /etc/rc.d/init.d/haproxy

    就可以使用 service haproxy start|stop|restart 来控制服务的启动停止跟重启。

    并通过以下命令加载到开机服务启动列表

    # chkconfig –add haproxy

    

    相关介绍,到此,可以正常使用 。


相关文章列表

haproxy session共享

评论列表

  • 网友   2015-06-02 07:27
    可以更详情点吗?
  • 1 篇文章 1/1 页

提交评论