zoukankan      html  css  js  c++  java
  • HAProxy的调度算法

      HAProxy通过固定参数balance指明对后端服务器的调度算法,该参数可以配置在listen或backend选项中。

      HAProxy的调度算法分为静态和动态调度算法,但是有些算法可以根据参数在静态和动态算法中相互转换。
    官方文档:https://cbonte.github.io/haproxy-dconv/2.0/configuration.html#4

    静态算法:

      静态算法按照事先定义好的规则轮询公平调度,不关心后端服务器的当前负载、链接数和相应速度等,且无法实时修改权重,只能靠重启HAProxy生效。

    static-rr(静态权重轮询)
    static-rr:基于权重的轮询调度,不支持权重的运行时调整及后端服务器慢启动,其后端主机数量没有限制。

    配置举例:

    listen web_host
    bind 192.168.20.101:80
    mode http
    log global
    balance static-rr
    server web1 192.168.20.17:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.20.27:80 weight 2 check inter 3000 fall 2 rise 5
    

    first(按后端服务器连接数达到最大上限后,逐个顺序调度)
    first:根据服务器在列表中的位置,自上而下进行调度,但是其只会当第一台服务器的连接数达到上限,新请求才会分配给下一台服务,因此会忽略服务器的权重设置。

    配置举例:

    listen web_host
    bind 192.168.20.101:80
    mode http
    log global
    balance first
    server web1 192.168.7.17:80 maxconn 1000 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.7.27:80 maxconn 1000 weight 1 check inter 3000 fall 2 rise 5
    

    动态算法:

      动态算法可基于后端服务器状态进行调度适当调整,比如优先调度至当前负载较低的服务器,且权重可以在haproxy运行时动态调整无需重启。

    HAproxy动态调整权重方法:

    #安装socat命令
    [root@centos7 ~]# yum -y install socat
    [root@centos7 ~]# echo "show info" | socat stdio /var/lib/haproxy/haproxy.sock
    [root@centos7 ~]# echo "get weight web_host/web1" | socat stdio /var/lib/haproxy/haproxy.sock 1 (initial 1)
    [root@centos7 ~]# echo "set weight web_host/web1 2" | socat stdio /var/lib/haproxy/haproxy.sock
    Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.
    
    [root@centos7 ~]# echo "get weight web_host/web1" | socat stdio /var/lib/haproxy/haproxy.sock 1 (initial 1)
    [root@centos7 ~]# echo "set weight web_host/web1 3" | socat stdio /var/lib/haproxy/haproxy.sock
    [root@centos7 ~]# echo "get weight web_host/web1" | socat stdio /var/lib/haproxy/haproxy.sock 3 (initial 1)
    

    roundrobin(动态权重轮询)
    roundrobin:基于权重的轮询动态调度算法,支持权重的运行时调整,不完全等于lvs中的rr轮训模式,HAProxy中的roundrobin支持慢启动(新加的服务器会逐渐增加转发数),其每个后端backend中最多支持4095个real server,roundrobin为默认调度算法,且支持对real server权重动态调整。

    配置举例:

    listen web_host
    bind 192.168.20.101:80
    mode http
    log global
    balance roundrobin
    server web1 192.168.20.17:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.20.27:80 weight 2 check inter 3000 fall 2 rise 5
    

    leastconn (动态加权的最少连接数)
    leastconn动态加权的最少连接数,支持权重的运行时调整和慢启动,即当前后端服务器连接最少的优先调度(新客户端连接),比较适合长连接的场景使用,比如MySQL等场景。

    配置举例:

    listen web_host
    bind 192.168.20.101:80
    mode http
    log global
    balance leastconn
    server web1 192.168.20.17:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.20.27:80 weight 1 check inter 3000 fall 2 rise 5
    

    其他算法:

      以下几种算法,根据配置不同即可作为静态算法,又可以通过选项成为动态算法。

    source (源地址hash)
      源地址hash,基于用户源地址hash并将请求转发到后端服务器,默认为静态即取模方式,但是可以通过hash-type支持的选项更改,后续同一个源地址请求将被转发至同一个后端web服务器,比较适用于session保持/缓存业务等场景。

    源地址有两种转发客户端请求到后端服务器的服务器选取计算方式,分别是取模法和一致性hash。

    1、map-based取模法
      基于服务器总权重的hash数组取模,该hash是静态的即不支持在线调整权重,不支持慢启动,其对后端服务器调度均衡,缺点是当服务器的总权重发生变化时,即有服务器上线或下线,都会因权重发生变化而导致调度结果整体改变。所谓取模运算,就是计算两个数相除之后的余数,10%7=3, 7%4=3,(2^32-1)%(1+1+2)

    source map-based取模法配置举例:

    listen web_host
    bind 192.168.20.101:80
    mode tcp
    log global
    balance source
    server web1 192.168.20.103:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.20.104:80 weight 1 check inter 3000 fall 2 rise 5
    

    2、一致性hash:
      一致性哈希,该hash是动态的,支持在线调整权重,支持慢启动,优点在于当服务器的总权重发生变化时,对调度结果影响是局部的,不会引起大的变动,hash(o)mod n 。

    source一致性hash配置举例:

    listen web_host
    bind 192.168.20.101:80,:8801-8810,192.168.7.101:9001-9010
    mode tcp
    log global
    balance source
    hash-type consistent
    server web1 192.168.20.103:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.20.104:80 weight 1 check inter 3000 fall 2 rise 5
    

    uri (基于户请求的uri做hash)
      基于对用户请求的uri做hash并将请求转发到后端指定服务器,也可以通过map-based和consistent定义使用取模法还是一致性hash。

    uri 取模法配置示例:

    listen web_host
    bind 192.168.20.101:80
    mode http
    log global
    balance uri
    server web1 192.168.20.103:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.20.104:80 weight 1 check inter 3000 fall 2 rise 5
    

    uri 一致性hash配置示例:

    listen web_host
    bind 192.168.20.101:80
    mode http
    log global
    balance uri
    hash-type consistent
    server web1 192.168.20.103:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.20.104:80 weight 1 check inter 3000 fall 2 rise 5
    

    url_param (基于用户请求url中params部分参数name做hash)
      url_param对用户请求的url中的 params 部分中的参数name作hash计算,并由服务器总权重相除以后派发至某挑出的服务器;通常用于追踪用户,以确保来自同一个用户的请求始终发往同一个real server。

    假设url = http://www.baidu.com/foo/bar/index.php?k1=v1&k2=v2
    则:
    host = "www.baidu.com"
    url_param = "k1=v1&k2=v2"
    

    url_param取模法配置示例:

    listen web_host
    bind 192.168.20.101:80
    mode http
    log global
    balance url_param name,age #支持对单个及多个url_param 值hash
    server web1 192.168.20.103:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.20.104:80 weight 1 check inter 3000 fall 2 rise 5
    

    url_param一致性hash配置示例:

    listen web_host
    bind 192.168.20.101:80
    mode http
    log global
    balance url_param name,age #支持对单个及多个url_param 值hash
    hash-type consistent
    server web1 192.168.20.103:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.20.104:80 weight 1 check inter 3000 fall 2 rise 5
    

    hdr (针对每个http请求头部header中的指定信息做hash)
      针对用户每个http头部(header)请求中的指定信息做hash,此处由 name 指定的http首部将会被取出并做hash计算,然后由服务器总权重相除以后派发至某挑出的服务器,假如无有效的值,则会使用默认的轮询调度。

    hdr取模法配置示例:

    listen web_host
    bind 192.168.20.101:80
    mode http
    log global
    balance hdr(User-Agent)
    server web1 192.168.20.103:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.20.104:80 weight 1 check inter 3000 fall 2 rise 5
    

    hdr一致性hash配置示例:

    listen web_host
    bind 192.168.20.101:80
    mode http
    log global
    balance hdr(User-Agent)
    hash-type consistent
    server web1 192.168.20.103:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.20.104:80 weight 1 check inter 3000 fall 2 rise 5
    

    rdp-cookie
      rdp-cookie对远程桌面的负载,使用cookie保持会话。

    基于iptables实现RDP端口转发:

    net.ipv4.ip_forward = 1
    iptables -t nat -A PREROUTING -d 192.168.20.101 -p tcp --dport 3389 -j DNAT --to-destination 172.18.139.20:3389
    iptables -t nat -A PREROUTING -d 192.168.20.101 -p tcp --dport 3389 -j DNAT --to-destination 172.18.139.21:3389
    iptables -t nat -A POSTROUTING -s 192.168.20.0/24 -j SNAT --to-source 192.168.20.101
    

    rdp-cookie取模法配置示例:

    listen RDP
    bind 192.168.20.101:3389
    balance rdp-cookie
    mode tcp
    server rdp0 172.18.132.20:3389 check fall 3 rise 5 inter 2000 weight 1
    server rdp0 172.18.132.21:3389 check fall 3 rise 5 inter 2000 weight 1
    

    rdp-cookie一致性hash配置示例:

    listen RDP
    bind 192.168.20.101:3389
    balance rdp-cookie
    hash-type consistent
    mode tcp
    server rdp0 172.18.132.20:3389 check fall 3 rise 5 inter 2000 weight 1
    server rdp0 172.18.132.21:3389 check fall 3 rise 5 inter 2000 weight 1
    

    random (随机负载平衡算法)
    在1.9版本开始增加一个叫做random的负载平衡算法,其基于一个随机数作为一致性hash的key,随机负载平衡对于大型服务器或经常添加或删除服务器非常有用,因为它可以避免在这种情况下由roundrobin或leastconn导致的锤击效应。

    random配置实例:

    listen web_host
    bind 192.168.20.101:80
    mode http
    log global
    balance random
    server web1 192.168.20.103:80 weight 1 check inter 3000 fall 2 rise 5
    server web2 192.168.20.104:80 weight 1 check inter 3000 fall 2 rise 5
    

    HAProxy各种算法总结

    static-rr--------->tcp/http 静态
    first------------->tcp/http 静态
    roundrobin-------->tcp/http 动态
    leastconn--------->tcp/http 动态
    random------------>tcp/http 动态
    source------------>tcp/http
    uri--------------->http
    url_param--------->http 取决于hash_type是否consistent
    hdr--------------->http
    rdp-cookie-------->tcp
    

    各算法使用场景

    static-rr               #做了session共享的web集群
    first                   #使用较少
    roundrobin              (HAProxy默认的调度算法)
    random
    leastconn               #数据库
    source                  #基于客户端公网IP的会话保持
    uri--------------->http #缓存服务器,CDN服务商
    url_param--------->http
    hdr                     #基于客户端请求报文头部做下一步处理
    rdp-cookie              #很少使用
    
  • 相关阅读:
    学生党可以申请的免费学习资源
    ms16-032漏洞复现过程
    宽字节注入浅浅谈!
    access数据库之cookie注入
    web渗透学习方向
    最简单的注入
    snapde的批量数据运算公式
    snapde的批量文件数据过滤保存功能
    五、Snapman多人协作电子表格之——Python脚本
    超大文本文件浏览器Snaptext,支持不限制大小的文本文件浏览
  • 原文地址:https://www.cnblogs.com/eddie1127/p/11808262.html
Copyright © 2011-2022 走看看