CentOS下HAProxy配置步骤

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
#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访问:8899会发现调度到两个不同的realserver中。用: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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/98b6fba6f933020e63949ecb69a70140.html