haproxy 负载均衡

1、HAProxy 简介

HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在时下的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。

HAProxy实现了一种事件驱动、单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户端(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。

2、HAProxy 安装

yum install haproxy -y

3、配置HAProxy

  1. 配置文件格式
    • HAProxy的配置处理3类来主要参数来源:
      • ——最优先处理的命令行参数,
      • ——“global”配置段,用于设定全局配置参数;
      • ——proxy相关配置段,如“defaults”、“listen”、“frontend”和“backend”;
  2. 时间格式(一些包含了值的参数表示时间,如超时时长。这些值一般以毫秒为单位,但也可以使用其它的时间单位后缀)
    • us: 微秒(microseconds),即1/1000000秒;

    • ms: 毫秒(milliseconds),即1/1000秒;

    • s: 秒(seconds);

    • m: 分钟(minutes);

    • h:小时(hours);

    • d: 天(days);

  3. 例子
    • 下面的例子配置了一个监听在所有接口的80端口上HTTP proxy服务,它转发所有的请求至后端监听在127.0.0.1:8000上的"server"
    •  1   global
       2         daemon
       3         maxconn 25600
       4 
       5     defaults
       6         mode http
       7         timeout connect 5000ms
       8         timeout client 50000ms
       9         timeout server 50000ms
      10 
      11     frontend http-in
      12         bind *:80
      13         default_backend servers
      14 
      15     backend servers
      16         server server1 127.0.0.1:8080 maxconn 32
      View Code
  4. 全局配置参数
    •  1 “global”配置中的参数为进程级别的参数,且通常与其运行的OS相关。
       2 
       3  * 进程管理及安全相关的参数
       4    - chroot <jail dir>:修改haproxy的工作目录至指定的目录并在放弃权限之前执行chroot()操作,可以提升haproxy的安全级别,不过需要注意的是要确保指定的目录为空目录且任何用户均不能有写权限;
       5    - daemon:让haproxy以守护进程的方式工作于后台,其等同于“-D”选项的功能,当然,也可以在命令行中以“-db”选项将其禁用;
       6    - gid <number>:以指定的GID运行haproxy,建议使用专用于运行haproxy的GID,以免因权限问题带来风险;
       7    - group <group name>:同gid,不过指定的组名;
       8    - log  <address> <facility> [max level [min level]]:定义全局的syslog服务器,最多可以定义两个;
       9    - log-send-hostname [<string>]:在syslog信息的首部添加当前主机名,可以为“string”指定的名称,也可以缺省使用当前主机名;
      10    - nbproc <number>:指定启动的haproxy进程的个数,只能用于守护进程模式的haproxy;默认只启动一个进程,鉴于调试困难等多方面的原因,一般只在单进程仅能打开少数文件描述符的场景中才使用多进程模式;
      11    - pidfile:
      12    - uid:以指定的UID身份运行haproxy进程;
      13    - ulimit-n:设定每进程所能够打开的最大文件描述符数目,默认情况下其会自动进行计算,因此不推荐修改此选项;
      14    - user:同uid,但使用的是用户名;
      15    - stats:
      16    - node:定义当前节点的名称,用于HA场景中多haproxy进程共享同一个IP地址时;
      17    - description:当前实例的描述信息;
      18 
      19  * 性能调整相关的参数
      20    - maxconn <number>:设定每个haproxy进程所接受的最大并发连接数,其等同于命令行选项“-n”;“ulimit -n”自动计算的结果正是参照此参数设定的;
      21    - maxpipes <number>:haproxy使用pipe完成基于内核的tcp报文重组,此选项则用于设定每进程所允许使用的最大pipe个数;每个pipe会打开两个文件描述符,因此,“ulimit -n”自动计算时会根据需要调大此值;默认为maxconn/4,其通常会显得过大;
      22    - noepoll:在Linux系统上禁用epoll机制;
      23    - nokqueue:在BSE系统上禁用kqueue机制;
      24    - nopoll:禁用poll机制;
      25    - nosepoll:在Linux禁用启发式epoll机制;
      26    - nosplice:禁止在Linux套接字上使用内核tcp重组,这会导致更多的recv/send系统调用;不过,在Linux 2.6.25-28系列的内核上,tcp重组功能有bug存在;
      27    - spread-checks <0..50, in percent>:在haproxy后端有着众多服务器的场景中,在精确的时间间隔后统一对众服务器进行健康状况检查可能会带来意外问题;此选项用于将其检查的时间间隔长度上增加或减小一定的随机时长;
      28    - tune.bufsize <number>:设定buffer的大小,同样的内存条件小,较小的值可以让haproxy有能力接受更多的并发连接,较大的值可以让某些应用程序使用较大的cookie信息;默认为16384,其可以在编译时修改,不过强烈建议使用默认值;
      29    - tune.chksize <number>:设定检查缓冲区的大小,单位为字节;更大的值有助于在较大的页面中完成基于字符串或模式的文本查找,但也会占用更多的系统资源;不建议修改;
      30    - tune.maxaccept <number>:设定haproxy进程内核调度运行时一次性可以接受的连接的个数,较大的值可以带来较大的吞吐率,默认在单进程模式下为100,多进程模式下为8,设定为-1可以禁止此限制;一般不建议修改;
      31    - tune.maxpollevents  <number>:设定一次系统调用可以处理的事件最大数,默认值取决于OS;其值小于200时可节约带宽,但会略微增大网络延迟,而大于200时会降低延迟,但会稍稍增加网络带宽的占用量;
      32    - tune.maxrewrite <number>:设定为首部重写或追加而预留的缓冲空间,建议使用1024左右的大小;在需要使用更大的空间时,haproxy会自动增加其值;
      33    - tune.rcvbuf.client <number>34    - tune.rcvbuf.server <number>:设定内核套接字中服务端或客户端接收缓冲的大小,单位为字节;强烈推荐使用默认值;
      35    - tune.sndbuf.client:
      36    - tune.sndbuf.server:
      37 
      38  * Debug相关的参数
      39    - debug
      40    - quiet
      View Code
  5. 代理
    •  1 代理相关的配置可以如下配置段中。
       2 
       3  - defaults <name>
       4  - frontend <name>
       5  - backend  <name>
       6  - listen   <name>
       7 
       8  “defaults”段用于为所有其它配置段提供默认参数,这配置默认配置参数可由下一个“defaults”所重新设定。
       9 
      10  “frontend”段用于定义一系列监听的套接字,这些套接字可接受客户端请求并与之建立连接。
      11 
      12  “backend”段用于定义一系列“后端”服务器,代理将会将对应客户端的请求转发至这些服务器。
      13 
      14  “listen”段通过关联“前端”和“后端”定义了一个完整的代理,通常只对TCP流量有用。
      15 
      16  所有代理的名称只能使用大写字母、小写字母、数字、-(中线)、_(下划线)、.(点号)和:(冒号)。此外,ACL名称会区分字母大小写。
      View Code
  6. http服务器配置示例

    •  1 #---------------------------------------------------------------------
       2 # Global settings
       3 #---------------------------------------------------------------------
       4 global
       5     # to have these messages end up in /var/log/haproxy.log you will
       6     # need to:
       7     #
       8     # 1) configure syslog to accept network log events.  This is done
       9     #    by adding the '-r' option to the SYSLOGD_OPTIONS in
      10     #    /etc/sysconfig/syslog
      11     #
      12     # 2) configure local2 events to go to the /var/log/haproxy.log
      13     #   file. A line like the following can be added to
      14     #   /etc/sysconfig/syslog
      15     #
      16     #    local2.*                       /var/log/haproxy.log
      17     #
      18     log         127.0.0.1 local2
      19 
      20     chroot      /var/lib/haproxy
      21     pidfile     /var/run/haproxy.pid
      22     maxconn     4000
      23     user        haproxy
      24     group       haproxy
      25     daemon
      26 
      27 defaults
      28     mode                    http
      29     log                     global
      30     option                  httplog
      31     option                  dontlognull
      32     option http-server-close
      33     option forwardfor       except 127.0.0.0/8
      34     option                  redispatch
      35     retries                 3
      36     timeout http-request    10s
      37     timeout queue           1m
      38     timeout connect         10s
      39     timeout client          1m
      40     timeout server          1m
      41     timeout http-keep-alive 10s
      42     timeout check           10s
      43     maxconn                 30000
      44 
      45 listen stats
      46     mode http
      47     bind 0.0.0.0:1080
      48     stats enable
      49     stats hide-version
      50     stats uri     /haproxyadmin?stats
      51     stats realm   Haproxy\ Statistics
      52     stats auth    admin:admin
      53     stats admin if TRUE
      54 
      55 
      56 frontend http-in
      57     bind *:80
      58     mode http
      59     log global
      60     option httpclose
      61     option logasap
      62     option dontlognull
      63     capture request  header Host len 20
      64     capture request  header Referer len 60
      65     default_backend servers
      66 
      67 frontend healthcheck
      68     bind :1099
      69     mode http
      70     option httpclose
      71     option forwardfor
      72     default_backend servers
      73 
      74 backend servers
      75   balance roundrobin
      76     server websrv1 192.168.10.11:80 check maxconn 2000
      77     server websrv2 192.168.10.12:80 check maxconn 2000
      View Code
  7. 负载均衡MySQL服务的配置示例
    •  1 #---------------------------------------------------------------------
       2 # Global settings
       3 #---------------------------------------------------------------------
       4 global
       5     # to have these messages end up in /var/log/haproxy.log you will
       6     # need to:
       7     #
       8     # 1) configure syslog to accept network log events.  This is done
       9     #    by adding the '-r' option to the SYSLOGD_OPTIONS in
      10     #    /etc/sysconfig/syslog
      11     #
      12     # 2) configure local2 events to go to the /var/log/haproxy.log
      13     #   file. A line like the following can be added to
      14     #   /etc/sysconfig/syslog
      15     #
      16     #    local2.*                       /var/log/haproxy.log
      17     #
      18     log         127.0.0.1 local2
      19 
      20     chroot      /var/lib/haproxy
      21     pidfile     /var/run/haproxy.pid
      22     maxconn     4000
      23     user        haproxy
      24     group       haproxy
      25     daemon
      26 
      27 defaults
      28     mode                    tcp
      29     log                     global
      30     option                  httplog
      31     option                  dontlognull
      32     retries                 3
      33     timeout http-request    10s
      34     timeout queue           1m
      35     timeout connect         10s
      36     timeout client          1m
      37     timeout server          1m
      38     timeout http-keep-alive 10s
      39     timeout check           10s
      40     maxconn                 600
      41 
      42 listen stats
      43     mode http
      44     bind 0.0.0.0:1080
      45     stats enable
      46     stats hide-version
      47     stats uri     /haproxyadmin?stats
      48     stats realm   Haproxy\ Statistics
      49     stats auth    admin:admin
      50     stats admin if TRUE
      51 
      52 
      53 frontend mysql
      54     bind *:3306
      55     mode tcp
      56     log global
      57     default_backend mysqlservers
      58 
      59 backend mysqlservers
      60     balance leastconn
      61     server dbsrv1 192.168.10.11:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300
      62     server dbsrv2 192.168.10.12:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300
      View Code
  8. 动静分离示例
    •  1 global
       2     log         127.0.0.1 local2
       3 
       4     chroot      /var/lib/haproxy
       5     pidfile     /var/run/haproxy.pid
       6     maxconn     4000
       7     user        haproxy
       8     group       haproxy
       9     daemon
      10 
      11     # turn on stats unix socket
      12     stats socket /var/lib/haproxy/stats
      13 
      14 defaults
      15     mode                    http
      16     log                     global
      17     option                  httplog
      18     option                  dontlognull
      19     option http-server-close
      20     option forwardfor       except 127.0.0.0/8
      21     option                  redispatch
      22     retries                 3
      23     timeout http-request    10s
      24     timeout queue           1m
      25     timeout connect         10s
      26     timeout client          1m
      27     timeout server          1m
      28     timeout http-keep-alive 10s
      29     timeout check           10s
      30     maxconn                 30000
      31 
      32 listen stats
      33     mode http
      34     bind 0.0.0.0:1080
      35     stats enable
      36     stats hide-version
      37     stats uri     /haproxyadmin?stats
      38     stats realm   Haproxy\ Statistics
      39     stats auth    admin:admin
      40     stats admin if TRUE
      41 
      42 
      43 frontend http-in
      44     bind *:80
      45     mode http
      46     log global
      47     option httpclose
      48     option logasap
      49     option dontlognull
      50     capture request  header Host len 20
      51     capture request  header Referer len 60
      52     acl url_static       path_beg       -i /static /images /javascript /stylesheets
      53     acl url_static       path_end       -i .jpg .jpeg .gif .png .css .js
      54 
      55     use_backend static_servers          if url_static
      56     default_backend dynamic_servers
      57 
      58 backend static_servers
      59     balance roundrobin
      60     server imgsrv1 172.16.200.7:80 check maxconn 6000
      61     server imgsrv2 172.16.200.8:80 check maxconn 6000
      62 
      63 backend dynamic_servers
      64     cookie srv insert nocache
      65     balance roundrobin
      66     server websrv1 172.16.200.7:80 check maxconn 1000 cookie websrv1
      67     server websrv2 172.16.200.8:80 check maxconn 1000 cookie websrv2
      68     server websrv3 172.16.200.9:80 check maxconn 1000 cookie websrv3
      View Code

猜你喜欢

转载自www.cnblogs.com/hequan/p/9206095.html