Haproxy学习笔记整理

Haproxy学习笔记整理

1 负载均衡

什么是负载均衡

负载均衡是一种在多个服务器或资源之间分配网络流量或请求的技术,目的是优化资源的利用、提高应用程序的可用性和响应速度,同时避免任何单点的过载。

负载均衡:Load Balance,简称LB,是一种服务或基于硬件设备等实现的高可用反向代理技术,负载均衡将特定的业务(web服务、网络流量等)分担给指定的一个或多个后端特定的服务器或设备,从而提高了公司业务的并发处理能力、保证了业务的高可用性、方便了业务后期的水平动态扩展。

四层负载均衡

是指在OSI模型的第四层,即传输层上进行的负载均衡。这一层主要处理如TCP和UDP这样的传输协议。四层负载均衡器能够理解并处理来自客户端的TCP或UDP连接请求,并根据特定的算法和规则将这些连接分发到后端的多个服务器上。

1.通过ip+port决定负载均衡的去向。

2.对流量请求进行NAT处理,转发至后台服务器。

3.记录tcp、udp流量分别是由哪台服务器处理,后续该请求连接的流量都通过该服务器处理。

4.支持四层的软件

  • Ivs:重量级四层负载均衡器。
  • Nginx:轻量级四层负载均衡器,可缓存。(nginx四层是通过upstream模块)
  • Haproxy:模拟四层转发。

七层负载均衡

1.通过虚拟ur|或主机ip进行流量识别,根据应用层信息进行解析,决定是否需要进行负载均衡。

2.代理后台服务器与客户端建立连接,如nginx可代理前后端,与前端客户端tcp连接,与后端服务器建立tcp连接。

3.支持7层代理的软件:

  • Nginx:基于http协议(nginx七层是通过proxy_pass)
  • Haproxy:七层代理,会话保持、标记、路径转移等

四层和七层的区别

  • 工作层次:四层在传输层,七层在应用层。
  • 协议处理:四层主要处理TCP/UDP,七层处理应用层协议如HTTP/HTTPS。
  • 流量分发依据:四层基于IP地址和端口,七层基于应用层内容。
  • 性能:四层通常性能更高,七层可能稍低但提供更精细的控制。
  • 应用场景:四层适用于高性能场景,七层适用于需要内容识别的场景。
  • 内容解析:四层不解析应用层内容,七层能够解析和理解应用层数据。
  • 安全性:四层负载均衡无法识别DDoS攻击;七层可防御SYN Cookie/Flood攻击

2 haproxy简介

企业版网站:https://www.haproxy.com

社区版网站:http://www.haproxy.org

github: https://github.com/haprox

HAProxy(High Availability Proxy)是一个开源的高性能TCP/HTTP负载均衡器和代理服务器。它被设计用于处理大量的并发连接和高吞吐量,同时提供高可用性、负载均衡和代理功能。HAProxy广泛应用于Web服务器的负载均衡、API代理、SSL/TLS终端点、连接路由、内容路由等场景。

主要特点:

  1. 高性能:HAProxy使用C语言编写,能够处理数以百万计的并发连接。
  2. 高可用性:支持热备份,可以在主服务器故障时自动切换到备份服务器。
  3. 灵活性:支持多种负载均衡算法,如轮询、最少连接、源地址哈希等。
  4. 安全性:支持SSL/TLS加密,可以作为SSL终端点或SSL代理。
  5. 配置简单:通过简单的配置文件进行设置,易于理解和维护。
  6. 监控与日志:提供实时监控和详细的日志记录功能,方便系统管理员进行故障排查和性能分析。

使用场景:

  • Web服务器负载均衡:将用户请求分发到多个后端服务器,提高网站访问速度和可靠性。
  • API网关:作为API的入口点,进行请求路由、认证、限流等操作。
  • SSL终端:处理SSL加密和解密,减轻后端服务器的计算负担。
  • 连接路由:根据请求的不同部分(如HTTP头、URL等)将连接路由到不同的后端服务器。
  • 内容缓存:缓存静态内容,减少后端服务器的负载。

3 haproxy的安装和服务信息

配置三台虚拟机,haproxy为172.25.254.100,webserver1为172.25.254.10,webserver2为172.25.254.20

安装配置nginx

[root@webserver1 ~]# dnf install nginx -y
[root@webserver2 ~]# dnf install nginx -y

[root@webserver1 ~]# echo webserver1 - 172.25.254.10 > /usr/share/nginx/html/index.html
[root@webserver2 ~]# echo webserver1 - 172.25.254.10 > /usr/share/nginx/html/index.html

[root@webserver1 ~]# systemctl enable --now nginx
[root@webserver2 ~]# systemctl enable --now nginx

实验环境

[root@haproxy ~]# curl 172.25.254.10
webserver1 - 172.25.254.10
[root@haproxy ~]# curl 172.25.254.20
webserver2 - 172.25.254.20

安装配置haproxy

[root@haproxy ~]# dnf install haproxy -y

haproxy基本配置信息

HAProxy的配置文件haproxy.cfg由两大部分组成,分别是:

  • global:全局配置段
  • 进程及安全配置相关的参数
  • 性能调整相关参数
  • Debug参数

proxies:代理配置段

  • defaults:为frontend, backend, listen提供默认配置
  • frontend:前端,相当于nginx中的server {}
  • backend:后端,相当于nginx中的upstream{}
  • listen:同时拥有前端和后端配置,配置简单,生产推荐使用

查找配置文件

[root@haproxy ~]# rpm -qc haproxy 
/etc/haproxy/haproxy.cfg
/etc/logrotate.d/haproxy
/etc/sysconfig/haproxy

3.1 haproxy基本部署方法实现负载均衡

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster
    bind *:80   # 监听所有80端口
    mode http
    use_backend webcluster-host         # 使用哪个后端

backend webcluster-host
    balance roundrobin
    server web1 172.25.254.10:80
    server web2 172.25.254.20:80

    
    
[root@haproxy ~]# systemctl restart haproxy.service 
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@haproxy ~]# curl 172.25.254.100
webserver1 - 172.25.254.10

也可以合并写法

listen webcluster
    bind *:80
    mode http
    balance roundrobin
    server web1 172.25.254.10:80
    server web2 172.25.254.20:80

3.2 haproxy全局配置参数

多进程和线程

haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
log 127.0.0.1 loca12
/var/lib/haproxy
/var/run/haproxy.pid
100000 maxconn
haproxy
haproxy
chroot
pidfile
user
group
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/haproxy.sock1 mode 600 level admin process 1
#启用多个sock文件
stats socket /var/lib/haproxy/haproxy.sock2 mode 600 level admin process 2
nbproc 2 #启用多进程
cpu-map 1 0 #进程和cpu核心绑定防止cpu抖动从而减少系统资源消耗
cpu-map 2 1 #2 表示第二个进程,1表示第二个cpu核心

查看多进程信息

[root@haproxy ~]# pstree -p | grep haproxy
           |-haproxy(33088)-+-haproxy(33090)
           |                `-haproxy(33091)

多线程对比

[root@haproxy ~]# cat /proc/33090/status | grep thread
Speculation_Store_Bypass:	thread vulnerable
[root@haproxy ~]# cat /proc/33090/status | grep -i thread
Threads:	1
Speculation_Store_Bypass:	thread vulnerable

[root@haproxy ~]# pstree -p | grep haproxy
           |-haproxy(33114)---haproxy(33116)---{haproxy}(33117)
[root@haproxy ~]# cat /proc/33116/status | grep -i thread
Threads:	2
Speculation_Store_Bypass:	thread vulnerable

3.3 proxies中的常用配置参数

frontend配置示例

backup

# 下载apache之后,将默认端口改为8080,当作sorry——server端口
[root@haproxy ~]# echo sorry 下班了 > /var/www/html/index.html

# 修改的配置文件
listen webcluster
    bind *:80
    mode http
    balance roundrobin
    server web1 172.25.254.10:80 
    server web2 172.25.254.20:80
    server web_sorry 172.25.254.100:8080 backup

在这里插入图片描述

disabled

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
    bind *:80
    mode http
    balance roundrobin
    server web1 172.25.254.10:80 disabled
    server web2 172.25.254.20:80
    server web_sorry 172.25.254.100:8080 backup

[root@haproxy ~]# systemctl restart haproxy.service

下线web1,将访问不到web1

redirect临时重定向

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
    bind *:80
    mode http
    balance roundrobin
    redirect prefix http://www.baidu.com
    server web1 172.25.254.10:80 weight 2
    server web2 172.25.254.20:80 weight 1
    server web_sorry 172.25.254.100:8080 backup

[root@haproxy ~]# systemctl restart haproxy.service

当访问172.25.254.100时,将重定向到百度页面

3.4 socat工具

对服务器动态权重和其它状态可以利用 socat工具进行调整,Socat是Linux下的一个多功能的网络工具,名字来由是Socket CAT,相当于netCAT的增强版.Socat的主要特点就是在两个数据流之间建立双向通道,且支持众多协议和链接方式。如 IP、TCP、UDP、IPv6、Socket文件等

范例:利用工具socat 对服务器动态权重调整

[root@haproxy ~]# dnf install socat -y

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats mode 600 level admin

# 查看haproxy状态
[root@haproxy ~]# echo "show servers state" | socat stdio /var/lib/haproxy/stats    
# 查看权重
[root@haproxy ~]# echo get weight webcluster/web1 | socat stdio /var/lib/haproxy/stats 
# 修改权重
[root@haproxy ~]# echo "set weight webcluster/web1 1" | socat stdio /var/lib/haproxy/haproxy.sock  
[root@haproxy ~]# echo get weight webcluster/web1 | socat stdio /var/lib/haproxy/stats
1 (initial 2)                    

# web1下线
[root@haproxy ~]# echo "disable server webcluster/web1 1" | socat stdio /var/lib/haproxy/haproxy.sock   
# web2下线
[root@haproxy ~]# echo "enable server webcluster/web1 1" | socat stdio /var/lib/haproxy/haproxy.sock    

设置多进程的热处理

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats1 mode 600 level admin process 1
    stats socket /var/lib/haproxy/stats2 mode 600 level admin process 2

    # utilize system-wide crypto-policies
    ssl-default-bind-ciphers PROFILE=SYSTEM
    ssl-default-server-ciphers PROFILE=SYSTEM
    nbproc 2
    cpu-map 1 0
    cpu-map 2 1


[root@haproxy ~]# systemctl restart haproxy.service

在这里插入图片描述

4. haproxy算法

HAProxy通过固定参数 balance指明对后端服务器的调度算法

balance参数可以配置在listen或backend选项中。

HAProxy的调度算法分为静态和动态调度算法

有些算法可以根据参数在静态和动态算法中相互转换。

4.1 静态算法

static-rr:基于权重的轮询调度

  • 不支持运行时利用socat进行权重的动态调整(只支持0和1,不支持其它值)
  • 不支持端服务器慢启动
  • 其后端主机数量没有限制,相当于LVS中的 wrr
listen webcluster
    bind *:80
    mode http
    balance static-rr #
    redirect prefix http://www.baidu.com
    server web1 172.25.254.10:80 weight 2
    server web2 172.25.254.20:80 weight 1
    server web_sorry 172.25.254.100:8080 backup

first

  • 根据服务器在列表中的位置,自上而下进行调度
  • 其只会当第一台服务器的连接数达到上限,新请求才会分配给下一台服务
  • 其会忽略服务器的权重设置
  • 不支持用socat进行动态修改权重,可以设置0和1,可以设置其它值但无效
listen webcluster
    bind *:80
    mode http
    #balance static-rr
    balance first
    redirect prefix http://www.baidu.com
    server web1 172.25.254.10:80 weight 2
    server web2 172.25.254.20:80 weight 1
    server web_sorry 172.25.254.100:8080 backup

4.2 动态算法

roundrobin

1.基于权重的轮询动态调度算法,

2.支持权重的运行时调整,不同于Ivs中的rr轮训模式

3.HAProxy中的roundrobin支持慢启动(新加的服务器会逐渐增加转发数),

4.其每个后端backend中最多支持4095个real server,

5.支持对real server权重动态调整,

6.roundrobin为默认调度算法,此算法使用广泛

listen webcluster
    bind *:80
    mode http
    #balance static-rr
    #balance first
    balance roundrobin
    redirect prefix http://www.baidu.com
    server web1 172.25.254.10:80 weight 2
    server web2 172.25.254.20:80 weight 1
    server web_sorry 172.25.254.100:8080 backup

动态调整

[root@haproxy ~]# echo "set weight webserver_80/webserver1 2"| socat stdio /var/lib/haproxy/haproxy.sock

leastconn

  • leastconn加权的最少连接的动态
  • 支持权重的运行时调整和慢启动,即:根据当前连接最少的后端服务器而非权重进行优先调度(新客户端连接)
  • 比较适合长连接的场景使用,比如:MySQL等场景。
listen webcluster
    bind *:80
    mode http
    #balance static-rr
    #balance first
    #balance roundrobin
    balance leastconn
    server web1 172.25.254.10:80  weight 1 check inter3s fal1 3 rise 5
    server web2 172.25.254.20:80  weight 1 check inter3s fal1 3 rise 5
    

4.3 其他算法

source

源地址hash,基于用户源地址hash并将请求转发到后端服务器,后续同一个源地址请求将被转发至同一个后端web服务器。此方式当后端服务器数据量发生变化时,会导致很多用户的请求转发至新的后端服务器,默认为静态方式,但是可以通过hash-type支持的选项更改这个算法一般是在不插入Cookie的TCP模式下使用,也可给拒绝会话cookie的客户提供最好的会话粘性,适用于session会话保持但不支持cookie和缓存的场景源地址有两种转发客户端请求到后端服务器的服务器选取计算方式,分别是取模法和一致性hash

如果访问客户端时一个家庭,那么所有的家庭的访问流量都会被定向到一台服务器,这时source算法的缺陷

map-base 取模法

map-based:取模法,对source地址进行hash计算,再基于服务器总权重的取模,最终结果决定将此请求转发至对应的后端服务器。

此方法是静态的,即不支持在线调整权重,不支持慢启动,可实现对后端服务器均衡调度

缺点是当服务器的总权重发生变化时,即有服务器上线或下线,都会因总权重发生变化而导致调度结果整体改变,hash-type 指定的默认值为此算法

所谓取模运算,就是计算两个数相除之后的余数,10%7=3,7%4=3

map-based算法:基于权重取模,hash(source_ip)??有后端服务器相加的总权重

一致性hash

一致性哈希,当服务器的总权重发生变化时,对调度结果影响是局部的,不会引起大的变动hash(o) mod n

该hash算法是动态的,支持使用 socat等工具进行在线权重调整,支持慢启动

1、后端服务器哈希环点keyA=hash(后端服务器虚拟ip)%(2^32)

2、客户机哈希环点key1=hash(client_ip)%(2^32) 得到的值在[0—4294967295]之间

3、将keyA和key1都放在hash环上,将用户请求调度到离key1最近的keyA对应的后端服务器

hash环偏斜问题
增加虚拟服务器IP数量,比如:一个后端服务器根据权重为1生成1000个虚拟IP,再hash。而后端服务器权重为2则生成2000的虚拟IP,再bash,最终在hash环上生成3000个节点,从而解决hash环偏斜问题

listen webcluster
    bind *:80
    mode http
    #balance static-rr
    #balance first
    #balance roundrobin
    balance source
    hash-type consisten
    redirect prefix http://www.baidu.com
    server web1 172.25.254.10:80 weight 2
    server web2 172.25.254.20:80 weight 1
    server web_sorry 172.25.254.100:8080 backup

uri

基于对用户请求的URI的左半部分或整个uri做hash,再将hash结果对总权重进行取模后根据最终结果将请求转发到后端指定服务器

适用于后端是缓存服务器场景

注意:此算法基于应用层,所以只支持mode http,不支持 modetcp

uri 取模法配置示例

listen webserver_80
    bind 172.25.254.100:80
    mode http
    balance uri
    server web1 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5
    server web2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5

uri一致性hash配置示例

listen webserver_80
    bind 172.25.254.100:80
    mode http
    balance uri
    hash-type consistent
    server web1 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5
    server web2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5

url_param

url_param对用户请求的url中的 params部分中的一个参数key对应的value值作hash计算,并由服务器总权重相除以后派发至某挑出的服务器,后端搜索同一个数据会被调度到同一个服务器,多用与电商通常用于追踪用户,以确保来自同一个用户的请求始终发往同一个real server

如果无没key,将按roundrobin算法

listen webserver_80
    bind 172.25.254.100:80
    mode http
    balance url_param name,userid #支持对多个url_param hash
    hash-type consistent
    server web1 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5
    server web2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5

hdr

针对用户每个http头部(header)请求中的指定信息做hash,

此处由 name指定的http首部将会被取出并做hash计算,

然后由服务器总权重取模以后派发至某挑出的服务器,如果无有效值,则会使用默认的轮询调度。

 listen webserver_80
    bind 172.25.254.100:80
    mode http
    balance hdr(User-Agent)
    server web1 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5
    server web2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5

一致性hash配置示例

 listen webserver_80
    bind 172.25.254.100:80
    mode http
    balance hdr(User-Agent)
    hash-type consistent
    server web1 172.25.254.10:80 weight 1 check inter 3s fall 3 rise 5
    server web2 172.25.254.20:80 weight 1 check inter 3s fall 3 rise 5

4.4 算法总结

#静态
static-rr--------->tcp/http
first------------->tcp/http
#动态
roundrobin-------->tcp/http
leastconn--------->tcp/http
random------------>tcp/http
#以下静态和动态取决于hash_type是否consistent
source------------>tcp/http
Uri--------------->http
url_param--------->http
hdr--------------->http

各算法使用场景

first #使用较少
static-rr #做了session共享的web集群
roundrobin
random
leastconn #数据库
source
#基于客户端公网IP的会话保持
Uri--------------->http
url_param--------->http
hdr
#缓存服务器,CDN服务商,蓝汛、百度、阿里云、腾讯
#可以实现session保持
#基于客户端请求报文头部做下一步处理

5. haproxy状态页

stats enable                        #基于默认的参数启用stats page
stats hide-version               #将状态页中haproxy版本隐藏
stats refresh <delay>          #设定自动刷新时间间隔,默认不自动刷新
stats uri <prefix>                #自定义stats page uri,默认值:/haproxy?stats                                stats auth <user>:<passwd>        #认证时的账号和密码,可定义多个用户,每行指定一个用户
stats admin {iflunless }<cond>      #启用stats page中的管理功能
listen webcluster
    bind *:80
    mode http
    #balance static-rr
    #balance first
    #balance roundrobin
    balance source
    hash-type consisten
    redirect prefix http://www.baidu.com
    server web1 172.25.254.10:80 weight 2
    server web2 172.25.254.20:80 weight 1
    server web_sorry 172.25.254.100:8080 backup

listen stats
    mode http
    bind *:3566
    stats enable
    stats uri /status
    stats auth pc:pz

在这里插入图片描述

6. 基于cookie的会话保持

cookie value:为当前server指定cookie值,实现基于cookie的会话黏性,相对于基于 source地址hash 调度算法对客户端的粒度更精准,但同时也加大了haproxy负载,目前此模式使用较少,已经被session共享服务器代替

配置

listen webcluster
    bind *:80
    mode http
    balance roundrobin
    cookie WEBCOOKIE insert nocache indirect
    server web1 172.25.254.10:80 cookie web1 weight 2
    server web2 172.25.254.20:80 cookie web2 weight 1
    #server web_sorry 172.25.254.100:8080 backup

[C:\~]$ curl -i 172.25.254.100
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    27  100    27    0     0  17821      0 --:--:-- --:--:-- --:--:-- 27000
HTTP/1.1 200 OK
server: nginx/1.20.1
date: Sun, 11 Aug 2024 19:49:53 GMT
content-type: text/html
content-length: 27
last-modified: Sun, 11 Aug 2024 16:30:23 GMT
etag: "66b8e71f-1b"
accept-ranges: bytes
set-cookie: WEBCOOKIE=web1; path=/
cache-control: private

webserver1 - 172.25.254.10

7. IP透传

web服务器中需要记录客户端的真实IP地址,用于做访问统计、安全防护、行为分析、区域排行等场景。

layer 4与 layer 7

四层:IP+PORT转发

七层:协议+内容交换

在这里插入图片描述

httpd.conf主配置文件,添加参数

在这里插入图片描述

在四层负载设备中,把client发送的报文目标地址(原来是负载均衡设备的IP地址),根据均衡设备设置的选择web服务器的规则选择对应的web服务器IP地址,这样client就可以直接跟此服务器建立TCP连接并发送数据,而四层负载自身不参与建立连接,而和LVS不同,haproxy是伪四层负载均衡,因为haproxy需要分别和前端客户端及后端服务器建立连接

mode http

​ 七层负载均衡服务器起了一个反向代理服务器的作用,服务器建立一-次TCP连接要三次握手,而client要访问Web Server要先与七层负载设备进行三次握手后建立TCP连接,把要访问的报文信息发送给七层负载均衡;然后七层负载均衡再根据设置的均衡规则选择特定的 Web Server,然后通过三次握手与此台Web Server建立TCP连接,然后Web Server把需要的数据发送给七层负载均衡设备,负载均衡设备再把数据发送给client;所以,七层负载均衡设备起到了代理服务器的作用,七层代理需要和Client和后端服务器分别建立连接

8. ACL

它可以根据设定的条件对经过服务器传输的数据包进行过滤(条件匹配)即对接收到的报文进行匹配和过滤,基于请求报文头部中的源地址、源端口、目标地址、目标端口、请求方法、URL、文件后缀等信息内容进行匹配并执行进一步操作,比如允许其通过或丢弃。

ACL-flags 匹配模式

-i 不区分大小写
-m 使用指定的正则表达式匹配方法
-n 不做DNS解析
-u 禁止acl重名,否则多个同名ACL匹配或关系
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster
    bind *:80
    mode http
    acl test hdr_dom(host) -i www.pzc.org
    use_backend webcluster-host if test
    default_backend default-host

backend webcluster-host
    mode http
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5

backend default-host
    mode http
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5


# 改电脑的解析

在这里插入图片描述

基于域名的访问(有域名解析)

ACL-operator 具体操作符

整数比较:eq、ge、gt、le、1t
字符比较:
- exact match (-m str):字符串必须完全匹配模式
- substring match(-m sub):在提取的字符串中查找模式,如果其中任何一个被发现,ACL将匹配
- prefix match (-m beg):在提取的字符串首部中查找模式,如果其中任何一个被发现,ACL将匹配
- suffix match(-m end):将模式与提取字符串的尾部进行比较,如果其中任何一个匹配,则ACL进
行匹配
- subdir match (-m dir):查看提取出来的用斜线分隔("/")的字符串,如其中任一个匹配,则
ACL进行匹配
- domain match (-m dom):查找提取的用点(".")分隔字符串,如果其中任何一个匹配,则ACL进
行匹配

ACL-value操作对象

The ACL engine can match these types against patterns of the following types :
- Boolean #布尔值
- integer or integer range #整数或整数范围,比如用于匹配端口范围
- IP address / network #IP地址或IP范围,192.168.0.1 ,192.168.0.1/24
- string--> www.timinglee.org
exact #精确比较
substring #子串
suffix #后缀比较
prefix #前缀比较
subdin #路径,/wp-includes/js/jquery/jquery.js
domain #域名,www.timinglee.org
- regular expression #正则表达式
- hex block #16进制

多个ACL的组合调用方式

# 多个ACL的逻辑处理
与:隐式(默认)使用
或:使用“or" 或“II”表示
否定:使用"!"表示

# 多个ACL调用方式:
#示例:
if valid_src valid_port #与关系,ACL中A和B都要满足为true,默认为与
if invalid_src ll invalid_port #或,ACL中A或者B满足一个为true
if!invalid_src #非,取反,不满足ACL才为true

ACL示例-域名匹配

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen stats
    mode http
    bind *: 9966
    stats enable
    stats refresh 3
    stats uri /status
    stats auth pc:pc
    
frontend webcluster
    bind *:80
    mode http
    acl test hdr_dom(host) -i www.pzc.org
    use_backend webcluster-host if test
    default_backend default-host

backend webcluster-host
    mode http
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5

backend default-host
    mode http
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5

匹配浏览器类型

拒绝curl和 wget的访问

frontend webcluster 
    bind *:80
    mode http
    #acl test hdr_dom(host) -i www.timinglee.org
    #acl test hdr_beg(host) -i bbs 
    #acl test hdr_end(host) -i com 
    #acl test base_sub -m sub lee   
    #acl test base_reg -i lee/$
    #acl ctrl_ip src 172.25.250.1 172.25.250.20
    acl  badwebrowers hdr_sub(User-Agent) -i curl weget
    http-request deny if badwebrowers
    default_backend default-host

backend default-host
    mode http
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
    

基于文件后缀名实现动静分离

frontend webcluster
  bind *:80
  mode http
  
  acl static path_end -i .html .jpg .png .css .js
  acl php    path_end -i .php
 
 
  use_backend webcluster-host if php
  default_backend default-host
 
backend webcluster-host
  mode http
  server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
 
backend default-host
  mode http
  server web2 172.25.254.20:80 check inter 2 fall 2 rise 5




[root@webserver1 ~]# yum install php -y
[root@webserver1 ~]# cat /var/www/html/index.php
<?php
  phpinfo();
?>

ACL匹配访问路径实现动静分离

frontend webcluster
  bind *:80
  mode http
  
  acl static path_end -i .html .jpg .png .css .js
  acl php    path_end -i .php
 
 
  use_backend webcluster-host if php
  default_backend default-host
 
backend webcluster-host
  mode http
  server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
 
backend default-host
  mode http
  server web2 172.25.254.20:80 check inter 2 fall 2 rise 5

#webserver访问配置
[root@webserver1 ~]# mkdir /var/www/html/static
[root@webserver1 ~]# mkdir /var/www/html/php
[root@webserver1 ~]# echo static - 172.25.254.10 > /var/www/html/static/index.html
[root@webserver1 ~]# echo php - 172.25.254.20 > /var/www/html/php/index.html
 
[root@webserver2 ~]# mkdir /var/www/html/static
[root@webserver2 ~]# mkdir /var/www/html/php
[root@webserver2 ~]# echo static - 172.25.254.20 > /var/www/html/static/index.html
[root@webserver2 ~]# echo php - 172.25.254.20 > /var/www/html/php/index.html

9. 自定义haproxy错误界面

对指定的报错进行重定向,使用errorfile和errorloc指令的两种方式,可以实现自定义各种错误页面


#haproxy配置
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000
    errorfile 503 /etc/haproxy/errorpage/503.http
 
#创建错误文件
[root@haproxy ~]# mkdir /etc/haproxy/errorpage/
[root@haproxy errorpage]# cat 503.http
HTTP/1.0 503 Service Unavailable
Cache-Control: no-cache
Connection: close
Content-Type: text/html;charset=UTF-8
 
<html><body><h1>xixi</h1>
66666666666666
</body></html>
 
#关闭webserver的web服务
[root@webserver1 ~]# systemctl stop httpd.service

10. HAProxy四层负载

主要针对于除http外的tcp协议协议应用服务访问的应用场景,例如MySQL、Redis、Memcache、RabbitMQ等

#haproxy配置
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen dbserver
    bind *:3306
    mode tcp
    balance roundrobin
    server db1 172.25.254.10:3306 check inter 2 fall 2 rise 5
    server db2 172.25.254.20:3306 check inter 2 fall 2 rise 5

 
#在webserver上配置mariadb服务
[root@webserver1 ~]# yum install mariadb-server -y
[root@webserver2 ~]# yum install mariadb-server -y
[root@webserver1 ~]# cat /etc/my.cnf.d/mariadb-server.cnf | grep server-id
server-id=1
[root@webserver2 ~]# cat /etc/my.cnf.d/mariadb-server.cnf | grep server-id
server-id=2
#启动mariadb服务
[root@webserver1 ~]# systemctl start mariadb.service
[root@webserver2 ~]# systemctl start mariadb.service
#我们通过命令行创建mysql用户,并对其进行授权
[root@webserver1 ~]# mysql -e "grant all on *.* to niu@'%' identified by 'niu'"
[root@webserver2 ~]# mysql -e "grant all on *.* to niu@'%' identified by 'niu'"
 
#在测试机上也要安装mariadb服务,这里我们使用haproxy主机进行测试
[root@haproxy ~]# yum install mariadb -y
[root@haproxy ~]# systemctl start mariadb

测试

[root@haproxy ~]# mysql -uniu -pniu -h 172.25.254.10 -e "show variables like 'hostname'"
+---------------+--------------------------+
| Variable_name | Value                    |
+---------------+--------------------------+
| hostname      | webserver1.timinglee.org |
+---------------+--------------------------+
[root@haproxy ~]# mysql -uniu -pniu -h 172.25.254.10 -e "show variables like 'hostname'"
+---------------+--------------------------+
| Variable_name | Value                    |
+---------------+--------------------------+
| hostname      | webserver2.timinglee.org |
+---------------+--------------------------+

11. HAProxy的https实现

HAProxy可以实现https的证书安全,从用户到haproxy为https,从haproxy到后端服务器为http通信,但基于性能考虑,生产中证书都是在后端服务器上实现的

制作证书

[root@haproxy ~]# mkdir /etc/haproxy/certs/
[root@haproxy ~]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /etc/haproxy/certs/pzc.org.key -x509 -days 365 -out /etc/haproxy/certs/pzc.org.crt
[root@haproxy certs]# cat pzc.org.key pzc.org.crt > pzc.org.pem

配置文件

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster-https
    bind *:443 ssl crt /etc/haproxy/certs/pzc.org.pem
    mode http
    use_backend webcluster-host
frontend webcluster
    bind *:80
    mode http
    redirect scheme https if !{ ssl_fc }    
 
backend webcluster-host
    mode http
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
 
backend default-host
    mode http
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
[root@haproxy ~]# mysql -uniu -pniu -h 172.25.254.10 -e "show variables like 'hostname'"
+---------------+--------------------------+
| Variable_name | Value                    |
+---------------+--------------------------+
| hostname      | webserver1.timinglee.org |
+---------------+--------------------------+
[root@haproxy ~]# mysql -uniu -pniu -h 172.25.254.10 -e "show variables like 'hostname'"
+---------------+--------------------------+
| Variable_name | Value                    |
+---------------+--------------------------+
| hostname      | webserver2.timinglee.org |
+---------------+--------------------------+

11. HAProxy的https实现

HAProxy可以实现https的证书安全,从用户到haproxy为https,从haproxy到后端服务器为http通信,但基于性能考虑,生产中证书都是在后端服务器上实现的

制作证书

[root@haproxy ~]# mkdir /etc/haproxy/certs/
[root@haproxy ~]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /etc/haproxy/certs/pzc.org.key -x509 -days 365 -out /etc/haproxy/certs/pzc.org.crt
[root@haproxy certs]# cat pzc.org.key pzc.org.crt > pzc.org.pem

配置文件

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster-https
    bind *:443 ssl crt /etc/haproxy/certs/pzc.org.pem
    mode http
    use_backend webcluster-host
frontend webcluster
    bind *:80
    mode http
    redirect scheme https if !{ ssl_fc }    
 
backend webcluster-host
    mode http
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
 
backend default-host
    mode http
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值