• CentOS6—HAProxy安装与配置


    概述

    Haproxy下载地址:
    http://pkgs.fedoraproject.org/repo/pkgs/haproxy/

    关闭SElinux、配置防火墙

    1、vi /etc/selinux/config

    #SELINUX=enforcing #注释掉

    #SELINUXTYPE=targeted #注释掉

    SELINUX=disabled #增加

    :wq!  #保存退出

    setenforce 0 #使配置立即生效

    2、vi /etc/sysconfig/iptables  #编辑

    -A RH-Firewall-1-INPUT -d 224.0.0.18 -j ACCEPT  #允许组播地址通信

    -A RH-Firewall-1-INPUT -p    vrrp    -j ACCEPT  #允许VRRP(虚拟路由器冗余协)通信

    -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT  #允许80端口通过防火墙

    :wq! #保存退出

    /etc/init.d/iptables restart #重启防火墙使配置生效

    安装HAProxy

    1、创建HAProxy运行账户和组

    groupadd haproxy #添加haproxy组

    useradd -g haproxy haproxy -s /bin/false #创建nginx运行账户haproxy并加入到haproxy组,不允许haproxy用户直接登录系统

    2、安装:

    [root@A local]# yum install -y gcc
    [root@A local]# tar zxvf haproxy-1.6.9.tar.gz
    [root@A local]# cd haproxy-1.6.9
    [root@A local]# make  TARGET=linux3100 CPU=x86_64  PREFIX=/usr/local/haprpxy  #编译  uname -r #查看系统内核版本号
    [root@A local]# make install PREFIX=/usr/local/haproxy  #安装
    
    #数说明:
    #TARGET=linux3100
    #使用uname -r查看内核,如:2.6.18-371.el5,此时该参数就为linux26
    #kernel 大于2.6.28的用:TARGET=linux2628
    #CPU=x86_64   #使用uname -r查看系统信息,如x86_64 x86_64 x86_64 GNU/Linux,此时该参数就为x86_64
    #PREFIX=/usr/local/haprpxy   #/usr/local/haprpxy为haprpxy安装路径

    3、设置HAProxy

    mkdir -p  /usr/local/haproxy/conf  #创建配置文件目录

    mkdir -p /etc/haproxy  #创建配置文件目录

    touch  /usr/local/haproxy/conf/haproxy.cfg  #创建配置文件

    ln -s  /usr/local/haproxy/conf/haproxy.cfg   /etc/haproxy/haproxy.cfg  #添加配置文件软连接

    cp -r  /usr/local/src/haproxy-1.6.9/examples/errorfiles  /usr/local/haproxy/errorfiles  #拷贝错误页面

    ln -s  /usr/local/haproxy/errorfiles  /etc/haproxy/errorfiles  #添加软连接

    mkdir -p  /usr/local/haproxy/log  #创建日志文件目录

    touch  /usr/local/haproxy/log/haproxy.log  #创建日志文件

    ln -s  /usr/local/haproxy/log/haproxy.log  /var/log/haproxy.log  #添加软连接

    cp /usr/local/src/haproxy-1.6.9/examples/haproxy.init  /etc/rc.d/init.d/haproxy  #拷贝开机启动文件

    chmod +x  /etc/rc.d/init.d/haproxy  #添加脚本执行权限

    chkconfig haproxy on  #设置开机启动

    ln -s  /usr/local/haproxy/sbin/haproxy  /usr/sbin  #添加软连接

    4、配置haproxy.cfg参数

    cp  /usr/local/haproxy/conf/haproxy.cfg   /usr/local/haproxy/conf/haproxy.cfg-bak  #备份

    vi  /usr/local/haproxy/conf/haproxy.cfg  #编辑,修改

    #---------------------------------------------------------------------
    # Global settings
    #---------------------------------------------------------------------
    global
        log    127.0.0.1 local2          ###[err warning info debug] 
        chroot  /usr/local/haproxy
        pidfile  /var/run/haproxy.pid   ###haproxy的pid存放路径,启动进程的用户必须有权限访问此文件 
        maxconn  4000                   ###最大连接数,默认4000
        user   haproxy
        group   haproxy
        daemon                          ###创建1个进程进入deamon模式运行。此参数要求将运行模式设置为"daemon"
     
    #---------------------------------------------------------------------
    # common defaults that all the 'listen' and 'backend' sections will 
    # use if not designated in their block
    #---------------------------------------------------------------------
    defaults
        mode   http             ###默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
        log    global           ###采用全局定义的日志
        option  dontlognull     ###不记录健康检查的日志信息
        option  httpclose       ###每次请求完毕后主动关闭http通道 
        option  httplog         ###日志类别http日志格式 
        option  forwardfor      ###如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip  
        option  redispatch      ###serverId对应的服务器挂掉后,强制定向到其他健康的服务器
        timeout connect 10000   #default 10 second timeout if a backend is not found
        timeout client 300000   ###客户端连接超时
        timeout server 300000   ###服务器连接超时
        maxconn     60000       ###最大连接数
        retries     3           ###3次连接失败就认为服务不可用,也可以通过后面设置 
    ####################################################################
    listen stats
            bind 0.0.0.0:1080           #监听端口  
            stats refresh 30s           #统计页面自动刷新时间  
            stats uri /stats            #统计页面url  
            stats realm Haproxy Manager #统计页面密码框上提示文本  
            stats auth admin:admin      #统计页面用户名和密码设置  
            #stats hide-version         #隐藏统计页面上HAProxy的版本信息
    #---------------------------------------------------------------------
    # main frontend which proxys to the backends
    #---------------------------------------------------------------------
    frontend main
        bind 0.0.0.0:80
        acl url_static path_beg    -i /static /images /javascript /stylesheets
        acl url_static path_end    -i .jpg .gif .png .css .js
     
        use_backend static if url_static     ###满足策略要求,则响应策略定义的backend页面
        default_backend   dynamic            ###不满足则响应backend的默认页面
     
    #---------------------------------------------------------------------
    # static backend for serving up images, stylesheets and such
    #---------------------------------------------------------------------
     
    backend static
        balance     roundrobin                 ###负载均衡模式轮询
        server      static 127.0.0.1:80 check ###后端服务器定义
         
    backend dynamic
        balance    roundrobin
        server         websrv1 10.252.97.106:80 check maxconn 2000
        server         websrv2 10.117.8.20:80 check maxconn 2000
     
    #---------------------------------------------------------------------
    # round robin balancing between the various backends
    #---------------------------------------------------------------------

    #errorloc  503  http://www.osyunwei.com/404.html

    errorfile 403 /etc/haproxy/errorfiles/403.http

    errorfile 500 /etc/haproxy/errorfiles/500.http

    errorfile 502 /etc/haproxy/errorfiles/502.http

    errorfile 503 /etc/haproxy/errorfiles/503.http

    errorfile 504 /etc/haproxy/errorfiles/504.http

    :wq! #保存退出

    service haproxy start #启动

    service haproxy stop  #关闭

    service haproxy restart  #重启

    5、设置HAProxy日志

    vi  /etc/syslog.conf  #编辑,在最下边增加

    # haproxy.log

    local0.*          /var/log/haproxy.log

    local3.*          /var/log/haproxy.log

    :wq! #保存退出

    vi  /etc/sysconfig/syslog   #编辑修改

    SYSLOGD_OPTIONS="-r -m 0"   #接收远程服务器日志

    :wq! #保存退出

    service syslog restart  #重启syslog

    5.浏览器打开haproxy的监控页面

    如下:http://120.55.95.103:1080/stats  //说明:1080即haproxy配置文件中监听端口,stats 即haproxy配置文件中的监听名称

    参考博客

    http://www.osyunwei.com/archives/7512.html

    http://www.cnblogs.com/kgdxpr/p/3272861.html

    http://www.cnblogs.com/MacoLee/p/5853413.html

  • 相关阅读:
    函数式编程(二):curry
    函数式编程(一):纯函数
    用 gulp 建一个服务器
    深度学习-Tensorflow2.2-预训练网络{7}-迁移学习基础针对小数据集-19
    深度学习-Tensorflow2.2-自定义训练综合实例与图片增强{6}-猫狗数据集实例-18
    深度学习-Tensorflow2.2-Tensorboard可视化{5}-可视化基础-17
    深度学习-Tensorflow2.2-Eager模式与自定义训练{4}-微分运算训练练习-16
    深度学习-Tensorflow2.2-卷积神经网络{3}-电影评论数据分类/猫狗数据集实例-15
    深度学习-Tensorflow2.2-批标准化简介-14
    深度学习-Tensorflow2.2-卷积神经网络{3}-卫星图像识别卷积综合实例(二分类)-13
  • 原文地址:https://www.cnblogs.com/xibei666/p/5877548.html
Copyright © 2020-2023  润新知