zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Haproxy ACL规则实现智能负载均衡

规则负载智能 实现 均衡 ACL haproxy
2023-09-14 09:15:16 时间

 

HAProxy的ACL 是什么?


由于HAProy可以工作在七层模型下,因此,要实现 HAProxy的强大功能,一定要使用强大灵活的ACL规则,通过ACL规则可以实现基于HAProy的智能负载均衡功能。HARroxy 通过ACL 规则完成两种主要的功能,分别是∶

1)通过设置的ACL规则检查客户端请求是否合法。如果符合ACL规则要求,那么就将放行,反正,如果不符合规则,则直接中断请求。

2)符合ACL 规则要求的请求将被提交到后端的backend 服务器集群,进而实现基于ACL 规则的负载均衡

 

ACL规则语法


HAProxy中的ACL规则经常使用在frontend 段中,使用方法如下∶

acl语法

#acl 参数
acl(关键字) 定义acl(名称)  方法(criterion)    -i (flags)  [匹配的路径或文件]
                           hdr_beg(host)
                           hdr_reg(host)
                           path_beg
                           path_end

acl∶是一个关键字,表示定义ACL规则的开始。后面需要跟上自定义的ACL名称

acl方法这个字段用来定义实现ACL的方法,HAproxy定义了很多ACL方法,经常使用的方法也就那么些,比如有 hdr_reg(host)、hr_dom(host)、hdr_beg(host)、url_sub、url_dir、path_beg、path_end等.

 

ACL常用方法


常用的方法:(-i∶表示忽略大小写,后面需要跟上匹配的路径或文件或正则表达式。) 

hdr_beg(host):精确匹配主机,表示以什么开头的域名

hdr_reg(host):正则匹配
例子:acl bbs hdr_reg(host) -i ^(bbs.test.com|shequ.test.com|forum)
例子:acl  www_polilcy  hdr_reg(host)  -i  ^(www.z.cn|z.cn)
path_beg: 用于测试请求的URL是否以指定的模式开头(匹配路径,表示以什么路径开头)
例子:acl url_static path_beg -i /static /iilannis /javascript /stylesheets
用于测试URL是否以/static、/iilannis、/javascript或/stylesheets开头。

path_end:用于测试请求的URL是否以指定的模式结尾(匹配路径结尾,表示以什么路径结尾)
例子:acl url_static path_end -i .jpg .gif .png .css .js
测试URL是否以.jpg、.gif、.png、.css或.js结尾。(这些都是静态资源,以静态资源结尾)

url_sub:表示请求url中包含什么字符串
url_dir:表示请求url中存在哪些字符串作为部分地址路径

url_sub 表示请求url中包含什么字符串
例如: acl  file_req  url_sub  -i  killall= 表示在请求url当中包含killall=,则此控制策略返回true

url_dir∶表示请求url中存在哪些字符串作为部分地址路径
例如: acl  dir_eq  url_dir  -i   allow,表示在请求url中存在allow作为部分地址路径。则此控制策略返回 true,否则返回false

以上这三种ACL规则可以满足企业当中绝大部分场景。 

与ACL 规则一起使用的HAProy参数还有use_backend,use_badend后面需要跟上一个 backend实例名,表示在满足 ACL规则后去请求哪个backend实例,与use_badend对应的还有default_backend参数,它表示在没有满足ACL条件的时候默认使用哪个后端 backend。

下面列举几个常见的ACL规则例子∶

#reg说明是可以使用正则,如果我们的域名是以www.z.cn或者以z.cn开头的就满足这样一个策略
acl  www_polilcy  hdr_reg(host)  -i  ^(www.z.cn|z.cn)

#精确匹配,只有域名为bbs.z.cn才满足这个ACL规则
acl  bbs_policy   hdr_dom(host)  -i  bbs.z.cn

#如果请求的URL当中有buy_sid=字符串的时候就满足该ACL规则
acl  url_policy   url_sub        -i  buy_sid=


#当满足ACL规则时候会使用use_backend来接上上面定义的ACL规则

#当ACL规则满足www_polioy会将请求转到server_www后端真实服务器上,如果都不满足请求转发到server_cache实例上
use_backend  server_www    if  www_polioy
use_backend  server_app     if  url_pollcy
use_backend  endserver_bbs  if bbs_policy

default  backend  server_cache



#上面的ACL规则从上到下依次执行
acl  html  url_reg  -i  \.html$  #访问控制列表名称html。规则要求访问以html结尾的url(可选)

use_backend  httpservers  if  html
#如果满足acl html规则,则推送给后端服务器httpservers 

default_backend    httpservers   
#默认使用的服务器组


backend httpservers    #名字要与上面的名字必须一样

    balance     roundrobin  #负载均衡的方式
    server  http1 192.168.13.133:80 maxconn 2000 weight 1  check inter 1s rise 2 fall 2
    server  http2 192.168.13.137:80 maxconn 2000 weight 1  check inter 1s rise 2 fall 2

 

 

ACL规则举例说明


在你本地host里面配置本地域名解析规则,该IP地址为你的Haproxy物理地址,即192.168.179.101(这里没有配置keepalived,所以并没有VIP地址,这里是物理网卡地址) 

ACL规则放在fronted的字段 ,所有配置如下

[root@master ~]# cat /etc/haproxy/haproxy.cfg
global
    log         127.0.0.1 local2 info
    pidfile     /var/run/haproxy.pid
    maxconn     4096  
    user        haproxy
    group       haproxy
    daemon               
    nbproc 1         
defaults
    mode                    http   
    log                     global
    retries                 3  
    option                  redispatch 
    maxconn                 4000 
    timeout connect         5000 
    timeout client          5000  
    timeout server          5000
    timeout check           5000

frontend  web
    mode                    http  
    bind                            *:80  
    option                  httplog     
    option                  forwardfor
    option                  httpclose
    log                     global
    
    acl a-server base_dom -i  www.test.com
    use_backend httpservers1   if a-server
    
    default_backend    httpservers2   

backend httpservers1   
    option      redispatch
    option      abortonclose
    cookie      SERVERID insert indirect nocache
    server  http1 192.168.179.102:80 maxconn 2000 cookie server1 weight 1  check inter 1s rise 2 fall 2
    server  http2 192.168.179.103:80 maxconn 2000 cookie server2 weight 1  check inter 1s rise 2 fall 2

backend httpservers2
    option      redispatch
    option      abortonclose
    cookie      SERVERID  insert indirect nocache
    server  http3 192.168.179.102:8080 maxconn 2000 cookie server3 weight 1  check inter 1s rise 2 fall 2

listen stats
    bind            *:81
    stats                       enable
    stats uri               /haproxy  
    stats auth              lulei:123 
     log         127.0.0.1 local2 err
     stats                  admin if TRUE
     stats                  hide-version
     stats                  refresh 30s

当访问的域名不一样,访问的后端服务器群组是不一样的,根据ACL规则如果访问域名为www.test.com则跳转到后端的 backend httpservers1 上面,对应Nginx服务器。其余的域名使用
backend httpservers2对应于后端的Tomcat服务器

满足ACL规则:

不满足ACL规则: 缺省 default_backend    httpservers2

一般后端一个节点做会话保持也没有太多的意义,因为请求只会放到这个节点如果有很多节点才做会话保持 ,所以这里只是演示一下

直接访问IP也不满足ACL规则