HAproxy 简单测试(http与tcp代理)

一:环境
     服务器两台;
     192.168.2.165   HAproxy
     192.168.2.163   nginx (已有服务,此次配置不涉及)
     服务器系统  centos6.5  64位
二:安装:
     下载地址:百度云   http://pan.baidu.com/s/1i3jLnIt
     cd /usr/local/src
     tar xzvf haproxy-1.4.24.tar.gz
     cd haproxy-1.4.24
     make TARGET=linux26 PREFIX=/usr/local/haproxy-1.4.24
     make install PREFIX=/usr/local/haproxy-1.4.24
     cd /usr/local
     ln -s  /usr/local/haproxy-1.4.24 ./haproxy
     groupadd haproxy
     useradd -g haproxy haproxy

三:配置
将以下启动脚本放到/etc/init.d/haproxy  下,并赋予执行权限
#!/bin/bash
#
# haproxy
#
# chkconfig: 35 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.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

config="/etc/haproxy.cfg"
exec="/usr/local/haproxy/sbin/haproxy"
prog=$(basename $exec)

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

lockfile=/var/lock/subsys/haproxy

check() {
    $exec -c -V -f $config
}

start() {
    $exec -c -q -f $config
    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 $config -p /var/run/$prog.pid
    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 $config
    if [ $? -ne 0 ]; then
        echo "Errors in configuration file, check with $prog check."
        return 1
    fi
    stop
    start
}

reload() {
    $exec -c -q -f $config
    if [ $? -ne 0 ]; then
        echo "Errors in configuration file, check with $prog check."
        return 1
    fi
    echo -n $"Reloading $prog: "
    $exec -D -f $config -p /var/run/$prog.pid -sf $(cat /var/run/$prog.pid)
    retval=$?
    echo
    return $retval
}

force_reload() {
    restart
}

fdr_status() {
    status $prog
}

case "$1" in
    start|stop|restart|reload)
        $1
        ;;
    force-reload)
        force_reload
        ;;
    checkconfig)
        check
        ;;
    status)
        fdr_status
        ;;
    condrestart|try-restart)
      [ ! -f $lockfile ] || restart
    ;;
    *)
        echo $"Usage: $0 {start|stop|status|checkconfig|restart|try-restart|reload|force-reload}"
        exit 2
esac


HAproxy配置文件:/etc/haproxy.cfg
global
    log 127.0.0.1 local3
    maxconn 20480
    chroot /usr/local/haproxy
    group haproxy  
    user haproxy
    daemon
    quiet
    nbproc 1
    pidfile /var/run/haproxy.pid
defaults
    log global
    mode http
    maxconn 20480
    option httplog
    option httpclose
    option forwardfor
    option dontlognull
    option redispatch
    retries 3
    balance roundrobin
    contimeout 5000
    clitimeout 50000
    srvtimeout 50000


frontend http-in                        #前台
       bind *:81
       mode    http
       option  httplog
       log     global
       default_backend htmlpool       #静态服务器池

backend htmlpool                    #后台
    #balance leastconn#负载均衡算法
    #option  httpchk HEAD /index.htm HTTP/1.0       #健康检查
    server web1 192.168.2.163:80  check port 80  inter 1000 fall 3 weight 20

frontend test-mysql   
    bind *:65224
    mode tcp
    log global
    option tcplog
    timeout client 3600s
    backlog 4096
    maxconn 50000
    default_backend test-mysql-server

backend test-mysql-server
   mode tcp
   option log-health-checks
   option redispatch
   option tcplog
   balance roundrobin
   timeout connect 1s
   timeout queue 5s
   timeout server 3600s
   server chaungshang-mysql-68 192.168.68.7:3306 check


四:启动
/etc/init.d/haproxy start

访问http://192.168.2.165:81  看到了163的80页面 
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值