RedHat下安装HAProxy

RedHat下安装HAProxy

首先查看系统内核版本号及系统名称

uname -a
Linux rh64pfcrm01kf 2.6.32-358.el6.x86_64 #1 SMP Tue Jan 29 11:47:41 EST 2013 x86_64 x86_64 x86_64 GNU/Linux
HAProxy安装
  1. HAProxy官网下载源码安装包http://www.haproxy.org/download/1.7/src/haproxy-1.7.5.tar.gz, 并将源码包拷贝到linux某个目录下

  2. 执行以下命令安装haproxy

    tar –xzvf haproxy-1.7.5.tar.gz  #解压文件
    cd haproxy-1.7.5
    make TARGET=linux2628
    make install PREFIX=/usr/local/haproxy

    遇到问题: 执行make TARGET=linux2628 报错: make: gcc: Command not found
    解决:gcc未安装,yum install gcc

  3. 配置haproxy.cfg
    安装包中有例子:examples/option-http_proxy.cfg
    我的最终配置:

    global
        log 127.0.0.1 local3 info
        chroot /usr/local/haproxy
        pidfile     /var/run/haproxy.pid 
        user haproxy
        group haproxy
        daemon
        stats socket /var/lib/haproxy/stats level admin
        maxconn 1024
    
    defaults
        log global
        mode tcp
        option tcplog
        timeout connect 5000
        timeout client 50000
        timeout server 50000
        balance roundrobin
        retries 3
    
    listen mysql_proxy_1
        bind ip.5451  #ip
        mode tcp
        server plfA ip1:3306 check weight 5 #mysql数据库服务器
        server plfB ip2:3306 check weight 5
    
    listen stats
        bind 0.0.0.0:1080
        mode http
        maxconn 10 
        stats refresh 30s 
        stats uri /stats 
        stats realm HAPCloud\ Haproxy 
        stats auth admin:** # 监控的用户名和密码
        stats hide-version 
        stats admin if TRUE
  4. 配置脚本启动与关闭haproxy
    安装包中同样有例子:

    cp -p examples/haproxy.init /etc/rc.d/init.d/haproxy
    chmod +x /etc/rc.d/init.d/haproxy
    chkconfig –-add haproxy
    vi /etc/rc.d/init.d/haproxy  #这里需要把BIN的值替换为BIN=haproxy安装目录/sbin/$BASENAME

    启动 service haproxy start
    关闭 service haproxy stop
    重启 service haproxy restart

    启动后可通过http://ip.1080/stats页面查看

  5. 常用指令
    动态设置权重:权重值为0-256

    echo "set weight mysql_proxy_1/plfA 0" |socat stdio /var/lib/haproxy/stats set weight <backend>/<server> <weight>[%]

    动态关闭和开启后台服务

    echo "disable server mysql_proxy_1/plfA" | socat stdio /var/lib/haproxy/stats disable server <backend>/<server>
    echo "enable server mysql_proxy_1/plfA" | socat stdio /var/lib/haproxy/stats enable server <backend>/<server>

    mysql测试

    mysql -uhaproxy -phaproxy123456 -h*.*.*.* -P5451 -D test -e "select count(1) from test;"

后续研究如何结合keepalived。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值