• 学习 Haproxy (三)


    HAProxy安装

    
    # wget http://www.haproxy.org/download/1.4/src/haproxy-1.4.24.tar.gz
    # tar xf haproxy-1.4.24.tar.gz
    # cd haproxy-1.4.24
    # make TARGET=linux2628 ARCH=x86_64  
    # 编译参数参考README,其中TARGET是指定内核版本,ARCH指定CPU架构
    # make PREFIX=/usr/local/haproxy-1.4.24 install
    # ln -s /usr/local/haproxy-1.4.24/ /usr/local/haproxy
    # 创建配置文件目录,pid目录
    # mkdir -p /usr/local/haproxy/etc  /usr/local/haproxy/var/chroot /usr/local/haproxy/var/run

    开启ipv4转发功能

    # sed -i 's#net.ipv4.ip_forward = 0#net.ipv4.ip_forward = 1#g' /etc/sysctl.conf 
    # grep ip_forward /etc/sysctl.conf 
    # net.ipv4.ip_forward = 1
    # sysctl -p
    # net.ipv4.ip_forward = 1

    HAProxy配置文件

    HAProxy配置文件分为五部分:
    
    global:全局配置参数,用来控制Haproxy启动前的进程及系统相关配置
    
    defaults:配置一些默认参数,如frontend,backend,listen等段未设置时则使用defaults段的配置
    
    listen:frontend和backend的组合
    
    frontend:用来匹配接收客户所请求的域名,uri等,并针对不同的匹配,做不同的请求处理
    
    backend:定义后端服务器群,以及对后端服务器的一些权重,队列,连接数等选项设置

    编辑配置文件

    # vim /usr/local/haproxy/etc/haproxy.cfg 

    配置内容如下

    global
      daemon  #以后台形式运行haproxy
      nbproc 1  #进程数量(可以设置多个进程提高性能) 
      pidfile logs/haproxy.pid   #haproxy的pid存放路径,启动进程的用户必须有权限访问此文件 
      maxconn 1500
      ulimit-n 82000 #设置每个进程的可用的最大文件描述符
      log 127.0.0.1 local0 debug
    
    defaults
      #mode   http #混合模式时关闭 默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
      retries 3 #三次连接失败就认为是服务器不可用,也可以通过后面设置 
      #option dontlognull #不记录健康检查的日志信息   
      option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户ip
      stats refresh 30 #统计页面刷新间隔   
      option httplog #日志类别http日志格式   
      option tcplog  #日志类别tcp日志格式
      option httpclose #每次请求完毕后主动关闭http通道 
      option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
      option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器  
      #balance roundrobin #默认的负载均衡的方式,轮询方式
      balance source #默认的负载均衡的方式,类似nginx的ip_hash
      maxconn 2000  #默认的最大连接数  
      timeout connect  5s
      timeout client  120s
      timeout server  120s
      #timeout http-keep-alive 10s #http连接生存的时间
      timeout check   10s
      timeout queue   5000ms  #在队列等待连接槽释放的超时时间
      timeout client-fin 30000ms #半关闭状态连接,server端非活动超时时间
      timeout server-fin 30000ms #半关闭状态连接,client端非活动超时时间
      timeout tunnel  1h  #客户端和服务器端通道非活动超时时间
      log 127.0.0.1 local0 info #默认日志配置
    
    
    ########统计页面配置########
    listen admin_stats  
      bind   0.0.0.0:10088 #监听端口  
      mode   http #http的7层模式  
      option httplog #采用http日志格式  
      log 127.0.0.1 local0 err     
      maxconn 10  
      stats refresh 30s #统计页面自动刷新时间  
      stats uri /stats #统计页面url,监控页面的url   
      stats realm XingCloud Haproxy #统计页面密码框上提示文本,监控页面的提示信息   
      stats auth admin:admin #统计页面用户名和密码设置,监控页面的用户和密码admin,可以设置多个用户名  
      stats auth admin1:admin1 #监控页面的用户和密码admin1   
      stats hide-version #隐藏统计页面上HAProxy的版本信息  
      stats admin if TRUE #手工启用/禁用,后端服务器(haproxy-1.4.9以后版本)  
      #errorfile 403 hostweb/error/403.http   
      #errorfile 500 hostweb/error/500.http   
      #errorfile 502 hostweb/error/502.http   
      #errorfile 503 hostweb/error/503.http   
      #errorfile 504 hostweb/error/504.http 
    
    listen WebServer  
      bind :10080 
      mode http  
      maxconn 1024  
      server web1 127.0.0.1:8080 weight 1 rise 2 fall 3
      server web2 127.0.0.1:8081 weight 1 rise 2 fall 3
      #errorfile 403 hostweb/error/403.http   
      #errorfile 500 hostweb/error/500.http   
      #errorfile 502 hostweb/error/502.http   
      #errorfile 503 hostweb/error/503.http   
      #errorfile 504 hostweb/error/504.http 
    
    listen MySQL13306
      bind    :13306
      mode    tcp
      balance roundrobin  
      server  MySQL_77_3306 127.0.0.1:3306 weight 1 check inter 1000 rise 3 fall 2 id 1 
      server  MySQL_77_3307 127.0.0.1:3307 weight 1 check inter 1000 rise 3 fall 2 id 2
    
    ########frontend配置##############  
    #listen 与 frontend配置相比,前者可读性较好,#Frontend和Backend的组合体,监控组的名称,按需自定义名称 ,后缀比较灵活
    #---------------------------------------------------------------------
    # main frontend which proxys to the backends 这里不需要动静分离,所以全部注释掉
    #---------------------------------------------------------------------
    #frontend  main *:5000
    #    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
     #   default_backend             app
    
    #---------------------------------------------------------------------
    # static backend for serving up images, stylesheets and such
    #---------------------------------------------------------------------
    #backend static
    #    balance     roundrobin
    #    server      static 127.0.0.1:4331 check
    
    #---------------------------------------------------------------------
    # round robin balancing between the various backends
    #---------------------------------------------------------------------
    #backend app
    #    balance     roundrobin
    #    server  app1 127.0.0.1:5001 check
    #    server  app2 127.0.0.1:5002 check
    #    server  app3 127.0.0.1:5003 check
    #    server  app4 127.0.0.1:5004 check
     
    #---------------------------------------------------------------------
    # round robin balancing between the various backends
    #---------------------------------------------------------------------
    #errorloc  503  http://www.osyunwei.com/404.html

    添加控制脚本

    有haproxy没有自带start,stop,restart,因此我们需要实现此功能,通过shell脚本来实现

    # vim /etc/init.d/haproxy 

    添加如下内容

    #!/bin/bash  
    BASE_DIR="/usr/local/haproxy"
    ARGV="$@"
    start()
    {
    echo "START HAPoxy SERVERS"  
    $BASE_DIR/sbin/haproxy -f $BASE_DIR/etc/haproxy.cfg
    }
    
    stop()
    {
    echo "STOP HAPoxy Listen"  
    kill -TTOU $(cat $BASE_DIR/var/run/haproxy.pid)
    echo "STOP HAPoxy process"  
    kill -USR1 $(cat $BASE_DIR/var/run/haproxy.pid)
    }
    case $ARGV in
    
    start)
    start
    ERROR=$?
    ;;
    
    stop)
    stop
    ERROR=$?
    ;;
    
    restart)
    stop
    start
    ERROR=$?
    ;;
    
    *)
    echo "haproxy [start|restart|stop]"  
    esac
    exit $ERROR

    赋予执行权限

    # chmod +x /etc/init.d/haproxy

    验证服务

    然后执行

    # service haproxy start
    # lsof -i :10080
    COMMAND  PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
    haproxy 9692 root    4u  IPv4 1438043      0t0  TCP 10.10.180.50:http (LISTEN)

    http://10.10.180.50/stats查看

     


      

  • 相关阅读:
    CMDB 理论
    分布式
    闲着无聊 一个python的,三级菜单。装逼版。
    献上一段,派遣网易云音乐,音频的代码。
    redis 安装
    selenium之 chromedriver与chrome版本映射表(更新至v2.46)
    简单的爬虫
    anaconda使用方法
    crm开发之用户重置密码
    模块和包,logging模块
  • 原文地址:https://www.cnblogs.com/wuhg/p/9935712.html
Copyright © 2020-2023  润新知