Rootop 服务器运维与web架构

haproxy安装配置及测试

系统版本:centos6.5_x64
软件版本:haproxy-1.5.9.tar.gz
后端服务器IP:10.10.10.5、10.10.10.6

这里提供haproxy的本站下载地址:wget -c https://www.rootop.org/rs/haproxy-1.5.9.tar.gz

安装:

[root@rootop-haproxy haproxy-1.5.9]# make TARGET=linux26 PREFIX=/usr/local/haproxy install

这里为什么是linux26,可以去看下README文档。

创建配置文件:
[root@rootop-haproxy ~]# mkdir /usr/local/haproxy/conf
[root@rootop-haproxy ~]# vi /usr/local/haproxy/conf/haproxy.cfg

global
    log 127.0.0.1 local0 info
    maxconn 1000
    chroot /usr/local/haproxy
    uid nobody
    gid nobody
    daemon
    nbproc 1
    pidfile /tmp/haproxy.pid

defaults
    log global
    mode http
    option httplog
    retries 3
    option httpclose
    option dontlognull
    option forwardfor
    option redispatch
    log 127.0.0.1 local3
    balance roundrobin
    maxconn 20480
    timeout connect 5000
    timeout client 50000
    timeout server 50000
    timeout check 2000

    stats enable
    stats refresh 30s
    stats uri /ha_check
    stats hide-version
    stats auth admin:admin

frontend http-in
    bind *:80
    mode http
    option httplog
    log global
    default_backend http_pool

backend http_pool
    balance roundrobin
    option httpchk HEAD /index.html HTTP/1.0
    cookie SERVERID insert indirect
    server WEBSRV1 10.10.10.5:80 maxconn 1500 cookie SRV1 check inter 2000 rise 2 fall 3 weight 1
    server WEBSRV2 10.10.10.6:80 maxconn 1500 cookie SRV2 check inter 2000 rise 2 fall 3 weight 1

启动服务:
[root@rootop-haproxy ~]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg

stats uri /status #监控页面
stats auth admin:admin #监控页身份认证

提供一个管理脚本:

#!/bin/bash
PROXY_CONF=/usr/local/haproxy/conf/haproxy.cfg
PROXY_BIN=/usr/local/haproxy/sbin/haproxy
CHECK_PROXY=$(ps aux | grep "haproxy -f" | grep -v grep | wc -l)

case $1 in

        start)
        if [ $CHECK_PROXY -ge 1 ]; then
                echo "already running , exit"
                exit
        else
        $PROXY_BIN -f $PROXY_CONF
                if [ $? == 0 ]; then
                        echo "start ok"
                else
                        echo "start failed"
                fi
        fi
        ;;

        stop)
        kill -9 $(cat /tmp/haproxy.pid)
                if [ $? == 0 ]; then
                        rm -f /tmp/haproxy.pid && echo "stop ok"
                else
                        echo "stop failed"
                fi
        ;;

        restart)
        kill -9 $(cat /tmp/haproxy.pid)
                if [ $? == 0 ]; then
                        rm -f /tmp/haproxy.pid && echo "stop ok"
                else
                        echo "stop failed"
                fi

        $PROXY_BIN -f $PROXY_CONF
                if [ $? == 0 ]; then
                        echo "start ok"
                else
                        echo "start failed"
                fi
        ;;

        *)
        echo "only start/stop/restart"
esac

exit

然后通过多个客户端访问haproxy的ip地址可以看到后端不同服务器提供的内容。

cookie SERVERID insert indirect  其中这句设置了会话保持,通过插入cookie方式,测试效果。

访问到服务器1:

访问到服务器2:

浏览器关闭后,会话断开。

原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/3192.html

作者:Venus

专注于 服务器运维与web架构 E-mail:venus#rootop.org

评论已关闭。