你好,游客 登录 注册 搜索
背景:
阅读新闻

CentOS下HAProxy配置步骤

[日期:2012-04-23] 来源:Linux社区  作者:gaby [字体: ]
haproxy凭借自身的优势,常在生产环境中用于web和数据库的负载,下面是个人对haproxy的了解。
准备:3台CentOS系统,1台做haproxy,2台做realserver(已经配置好用apache提供web服务),yum源,编译环境
配置步骤:
添加haproxy用户和组,用于运行haproxy,下载源码包,编译安装,haproxy的配置文件需要自己手动创建,最后使用haproxy自带脚本启动。
#groupadd -r haproxy
#useradd -r -M -g haproxy -s /sbin/nologin haproxy
#wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.20.tar.gz
#tar xf haproxy-1.4.20.tar.gz
#cd haproxy-1.4.20
#make TARGET=linux26 PREFIX=/usr/local/haproxy install
# cd /usr/local/
# chown -R haproxy. haproxy/
#cd /usr/local/haproxy/
#vim haproxy.cfg
global
     log 127.0.0.1 local0 info #[err warning info debug]
     maxconn 4096
     user haproxy
     group haproxy
     daemon
     nbproc 1
     pidfile /var/run/haproxy.pid
defaults
     maxconn 2000
     contimeout 5000
     clitimeout 30000
     srvtimeout 30000
listen    admin_status
     mode    http
     bind 192.168.1.104:8899
     option httplog
     log global
     stats enable
     stats refresh 10s
     stats hide-version
     stats realm Haproxy\ Statistics
     stats uri    /admin_status
     stats auth    haproxy :gaby
     stats admin if TRUE
     server app1_1 192.168.1.103:80 cookie app1inst1 check inter 2000 rise 2 fall 5
     server app1_2 192.168.1.104:80 cookie app1inst2 check inter 2000 rise 2 fall 5

#chown haproxy. haproxy.cfg
# cd /usr/local/haproxy/sbin/
# ./haproxy -f ../haproxy.cfg
此刻用ie访问http://192.168.1.104:8899会发现调度到两个不同的realserver中。用http://192.168.1.104:8899/admin-status输入账号密码(由 stats auth  haproxy :gaby
定义)后可以查看当前haproxy的工作状态。
很多朋友喜欢sv脚本,写个呗!
# cd /etc/init.d/
#vim haproxy
#!/bin/bash
#
#haproxy        Startup script for the haproxy Server,write by gabylinux
#
# chkconfig: - 86 14
# description: haproxy
# processname: haproxy
# config: /usr/local/haproxy/haproxy.cfg
# pidfile: /usr/local/haproxy/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
HAPROXY_CONF_FILE=/usr/local/haproxy/haproxy.cfg
haproxy="/usr/local/haproxy/sbin/haproxy"
prog=$(basename $haproxy)
lockfile=/var/lock/subsys/haproxy
start() {
    [ -x $haproxy ] || exit 5
    [ -f $HAPROXY_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $haproxy -f $HAPROXY_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
    echo "ok"
}
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -9
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
restart() {
    #configtest || return $?
    stop
    sleep 1
    start
}
reload() {
    #configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $haproxy -HUP
    RETVAL=$?
    echo
}
rh_status() {
    status $prog
}
rh_status_q() {
    rh_status >/dev/null 2>&1
}
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

linux
相关资讯       CentOS安装  HAProxy 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款