全网超详细Haproxy,包含理论和实验

理论部分:

HAProxy(High Availability Proxy)是一个使用C语言编写的自由及开放源代码软件,它主要用于提供高可用性、负载均衡以及基于TCP和HTTP的应用程序代理。以下是HAProxy的主要功能和特点:

一、主要功能

  1. 负载均衡

    • HAProxy能够将客户端的请求分发到多台后端服务器上,实现负载均衡,提高整体服务的处理能力和响应速度。

    • 支持多种负载均衡算法,如轮询调度(Round Robin)、加权轮询(Weighted Round Robin)、最少连接者先处理(Least Connections)等,以适应不同的应用场景和需求。

  2. 高可用性

    • 通过健康检查和故障转移机制,确保服务的连续性。HAProxy会定期检测后端服务器的状态,一旦发现故障服务器,会自动将其从负载均衡列表中摘除,并将请求转发到其他健康的服务器上。

    • 支持会话保持(Session Persistence),确保同一客户端的请求在一定时间内始终被转发到同一台后端服务器上,以提高应用的稳定性和用户体验。

  3. 代理服务

    • 支持基于TCP和HTTP的应用代理,适用于各种网络协议和服务类型。

    • 在HTTP代理模式下,HAProxy能够分析应用层协议,对请求和响应进行精细控制,如添加、修改或删除HTTP头部信息等。

  4. 安全性

    • HAProxy的运行模式可以保护后端服务器不被直接暴露在网络上,增加系统的安全性。

    • 支持SSL/TLS加密,确保数据传输过程中的安全性。

二、特点

  1. 高性能

    • HAProxy采用事件驱动模型,能够处理大量并发连接。其单进程模型相比多进程或多线程模型具有更好的资源和时间管理能力,适用于高负载场景。

    • 支持高并发连接数,最高可同时维护数万甚至数十万个并发连接。

  2. 灵活性强:

    • 配置简单灵活,支持丰富的配置选项和指令,满足不同用户的需求。

    • 支持动态配置更新,无需重启服务即可生效,提高系统的可用性和灵活性。

  3. 可靠性高:

    • 经过多年的发展和完善,HAProxy已经成为一款成熟稳定的软件产品,被广泛应用于各类生产环境中。

    • 与硬件级的负载均衡设备相比,HAProxy在性能和可靠性方面表现出色,且成本更低。

三、算法

在HAProxy中,负载均衡算法被分为静态算法、动态算法以及其他算法,每种算法都有其特定的描述、使用场景和语法。以下是对这些算法的详细解析:

静态算法

静态算法在运行时不会根据后端服务器的实时状态(如负载、连接数等)进行调整,而是按照预先设定的规则进行调度。

1. Static-RR(静态轮询)
  • 描述:基于权重的轮询调度,但不支持运行时权重的动态调整及后端服务器的慢启动。每个后端服务器根据其在列表中的位置和权重轮流接收请求,但算法会忽略服务器的当前负载和连接数。

  • 使用场景:适用于服务器性能相对固定,且不需要根据实时负载动态调整权重的场景。

  • 语法:在配置文件中,通过balance static-rr指令指定此算法。服务器配置示例:

    listen web_host  
        bind 10.0.0.7:80  
        mode http  
        balance static-rr  
        server web1 10.0.0.17:80 weight 1 check inter 3000 fall 2 rise 5  
        server web2 10.0.0.27:80 weight 2 check inter 3000 fall 2 rise 5
2. First(优先调度)
  • 描述:根据服务器在列表中的位置,自上而下进行调度。只有当列表中前面的服务器连接数达到上限时,新请求才会分配给后面的服务器。此算法会忽略服务器的权重设置。

  • 使用场景:使用较少,因为它不考虑服务器的权重和当前负载。

  • 语法:通过balance first指令指定。

动态算法

动态算法会根据后端服务器的实时状态(如负载、连接数等)动态调整调度策略,以优化请求分配。

1. Round Robin(轮询)
  • 描述:基于权重的轮询动态调度算法,支持权重的运行时调整。与Static-RR不同,Round Robin支持慢启动(新加入的服务器会逐渐增加转发数)。

  • 使用场景:广泛使用的默认调度算法,适用于大多数需要负载均衡的场景。

  • 语法:通过balance roundrobin指令指定。

2. Leastconn(最少连接)
  • 描述:加权的最少连接数算法,支持权重的运行时调整和慢启动。根据当前连接数最少的后端服务器进行优先调度,适合长连接场景。

  • 使用场景:适用于MySQL等需要长连接的场景。

  • 语法:通过balance leastconn指令指定。

3. Random(随机)
  • 描述:基于随机数的负载平衡算法,从1.9版本开始引入。支持权重的动态调整,权重较大的主机有更大概率获取新请求。

  • 使用场景:对于大型服务器场或经常添加或删除服务器的环境非常有用。

  • 语法:通过balance random指令指定。

其他算法
1. Source(源地址hash)
  • 描述:基于用户源地址hash并将请求转发到后端服务器,后续同一个源地址请求将被转发至同一个后端服务器。默认为静态方式,但可以通过hash-type选项更改。

  • 使用场景:适用于需要保持会话一致性的场景,如购物车、用户登录等。

  • 语法:通过balance sourcehash-type指令配置。

2. URI(URI hash)
  • 描述:基于用户请求的URI的左半部分或整个URI做hash,再将hash结果对总权重进行取模后,将请求转发到后端指定服务器。适用于后端是缓存服务器的场景。

  • 使用场景:适用于缓存服务器等需要根据URI分发请求的场景。

  • 语法:通过balance urihash-type指令配置。

四、应用场景

HAProxy广泛应用于各类企业中,特别是在需要处理大量并发请求、实现高可用性和负载均衡的场景下。例如,它可以用于构建高可用的Web服务器集群、数据库的读写分离和负载均衡、以及需要处理大量并发连接的应用程序等。

综上所述,HAProxy是一款功能强大、性能高效、灵活可靠的高可用性负载均衡器和代理服务器软件。

实验部分

一、haproxy基本配置信息

1、haproxy的基本部署
#环境准备
haproxy: 172.25.254.100
webserver1:172.25.254.10 
webserver2: 172.25.254.20
​
#webserver1、webserver2
dnf install nginx -y
​
[root@webserver1 ~]# echo webserver1 - 172.25.254.10 > /usr/share/nginx/html/index.html
[root@webserver2 ~]# echo webserver2 - 172.25.254.20 > /usr/share/nginx/html/index.html
​
systemctl enable --now nginx
​
#访问
curl 172.25.254.10
curl 172.25.254.20
​
#haproxy
[root@haproxy ~]# dnf install haproxy -y
​
#haproxy
[root@haproxy ~]# rpm -qc haproxy
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
#下图:两种写法都可以
frontend webcluster
    bind *:80
    mode http
    use_backend webcluster-host
​
backend webcluster-host
    balance rountrobin
    server web1 172.25.254.10:80
    server web2 172.25.254.20:80
​
或者
​
​
#修改缩进
[root@haproxy ~]# vim ~/.vimrc
set ts=4 ai sw=4
[root@haproxy ~]# systemctl restart haproxy.service 
#如果报错
[root@haproxy ~]# > /var/log/messages
[root@haproxy ~]# cat /var/log/messages
# 测试:
[root@haproxy ~]# curl 172.25.254.100
webserver1 - 172.25.254.10
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
​
#当我们停止时nginx时,就不能访问
[root@webserver1 ~]# systemctl stop nginx
[root@haproxy ~]# curl 172.25.254.100
webserver1 - 172.25.254.20
2、haproxy全局配
#多进程
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
    nbproc 2
    cpu-map 1 0
    cpu-map 2 1
[root@haproxy ~]# systemctl restart haproxy.service 
#查看多进程信息
[root@haproxy ~]# pstree -p | grep haproxy
           |-haproxy(31828)-+-haproxy(31831)
           |                `-haproxy(31832)
​

# 启用多线程
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
    nbthread 2
​
#定义日志
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
    log         127.0.0.1 local2
[root@haproxy ~]# systemctl restart haproxy.service
[root@haproxy ~]# vim /etc/rsyslog.conf
module(load="lmudp") #udp的这个打开
input(type="imudp" port="514")
local2.*                               /var/log/haproxy.log
#查看多线程信息
[root@haproxy ~]# pstree -p | grep haproxy
           |-haproxy(31841)---haproxy(31843)---{haproxy}(31844)
3、haproxy代理参数
#关掉webserver1\2的,可以访问100的
[root@webserver1 ~]# systemctl stop nginx.service 
[root@webserver2 ~]# systemctl stop nginx.service 
​
[root@haproxy ~]# dnf install httpd -y
[root@haproxy ~]# vim /etc/httpd/conf/httpd.conf 
#backup --sorryserver 的端口
Listen 8080
[root@haproxy ~]# systemctl enable --now httpd
[root@haproxy ~]# echo sorry > /var/www/html/index.html
[root@haproxy ~]# curl 172.25.254.100
sorry
​
​
#如果是开启的话,需在文件里注释掉
[root@webserver1 ~]# systemctl start nginx.service 
[root@webserver2 ~]# systemctl start nginx.service 
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
​
listen webcluster
    bind *:80
    mode http
    balance roundrobin
    #server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
    #server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 2
    server web_sorry 172.25.254.100:8080 backup
​
[root@haproxy ~]# systemctl restart haproxy.service 
[root@haproxy ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver1 - 172.25.254.10
webserver1 - 172.25.254.20
webserver1 - 172.25.254.10
webserver1 - 172.25.254.20
​
​
#下线指定realserver
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
​
listen webcluster
    bind *:80
    mode http
    balance roundrobin
    server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2 disabled
    server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 2
    server web_sorry 172.25.254.100:8080 backup
​
[root@haproxy ~]# systemctl restart haproxy.service 
[root@haproxy ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver1 - 172.25.254.20
webserver1 - 172.25.254.20
​
​
#网页重定向
[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 check inter 2 fall 3 rise 5 weight 2
    #server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 2
    #server web_sorry 172.25.254.100:8080 backup
​
[root@haproxy ~]# systemctl restart haproxy.service 
​

二、haproxy热处理

[root@haproxy ~]# dnf install socat -y
[root@haproxy ~]# echo "set weight webcluster/web1 1" | socat stdio /var/lib/haproxy/stats 
[root@haproxy ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
​
[root@haproxy ~]# echo "set weight webcluster/web1 2" | socat stdio /var/lib/haproxy/stats 
[root@haproxy ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
​
[root@haproxy ~]# echo "disable  server webcluster/web1 " | socat stdio /var/lib/haproxy/stats 
[root@haproxy ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
​
[root@haproxy ~]# echo "enable  server webcluster/web1 " | socat stdio /var/lib/haproxy/stats 
​
[root@haproxy ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
​
​
#haproxy多进程如何热处理
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
global
    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 
[root@haproxy ~]# ls /var/lib/haproxy/*
/var/lib/haproxy/stats  /var/lib/haproxy/stats1  /var/lib/haproxy/stats2
​

三、haproxy算法

静态算法
1、static-rr
  • 不能通过socat修改权重

# 不要多进程
stats socket /var/lib/haproxy/stats mode 600 level admin
​
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
    bind *:80
    mode http
    #balance roundrobin
    balance static-rr
    server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
    server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service 
​
# 测试
[root@haproxy ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver1 - 172.25.254.10
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.102、first
  • 其只会当第一台服务器的连接数达到上限,新请求才会分配给下一台服务

  • 其会忽略服务器的权重设置

  • 不支持用socat进行动态修改权重,可以设置0和1,可以设置其它值但无效

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
    bind *:80
    mode http
    #balance roundrobin
    #balance static-rr
    balance first
    server web1 172.25.254.10:80 maxconn 1 check inter 2 fall 3 rise 5 weight 2
    server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service 
​
# 测试: 多台主机执行死循环,可看到
[root@webserver1 ~]# while true ; do curl 172.25.254.100; sleep 0.1 ; done
​
[root@webserver2 ~]# while true ; do curl 172.25.254.100; sleep 0.1 ; done
​
动态算法
1、roundrobin
  • 给权重高负载小的

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

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

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

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
    bind *:80
    mode http
    balance roundrobin
    #balance static-rr
    #balance first
    server web1 172.25.254.10:80 maxconn 1 check inter 2 fall 3 rise 5 weight 2
    server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service 
​
#测试
[root@haproxy ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver1 - 172.25.254.10
webserver1 - 172.25.254.10
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
​
2、leastconn
  • 谁链接最少给谁

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
    bind *:80
    mode http
    #balance static-rr
    #balance first
    #balance roundrobin
    balance leastconn
    server web1 172.25.254.10:80 maxconn 1 check inter 2 fall 3 rise 5 weight 2
    server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service 
​
#测试
[root@haproxy ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
其他算法

其他算法有hash-type contsistent是动态,没有是静态

1、source
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
    bind *:80
    mode http
    #balance static-rr
    #balance first
    #balance roundrobin
    #balance leastconn
    balance source
    server web1 172.25.254.10:80 maxconn 1 check inter 2 fall 3 rise 5 weight 2
    server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service 
​
#测试
[root@haproxy ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
​
2、uri
#webserver1上
[root@webserver1 ~]# echo 172.25.254.10 - index1.html > /usr/share/nginx/html/index1.html
[root@webserver1 ~]# echo 172.25.254.10 - index2.html > /usr/share/nginx/html/index2.html
[root@webserver1 ~]# echo 172.25.254.10 - index3.html > /usr/share/nginx/html/index3.html
​
#webserver2
[root@webserver2 ~]# echo 172.25.254.20 - index1.html > /usr/share/nginx/html/index1.html
[root@webserver2 ~]# echo 172.25.254.20 - index2.html > /usr/share/nginx/html/index2.html
[root@webserver2 ~]# echo 172.25.254.20 - index3.html > /usr/share/nginx/html/index3.html
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
    bind *:80
    mode http
    #balance roundrobin
    #balance leastconn
    #balance static-rr
    #balance first
    #balance source
    balance uri
    hash-type consistent
    server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
    server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service 
​
#测试
[root@haproxy ~]# curl 172.25.254.100/index1.html
172.25.254.10 - index1.html
[root@haproxy ~]# curl 172.25.254.100/index2.html
172.25.254.20 - index2.html
[root@haproxy ~]# curl 172.25.254.100/index3.html
172.25.254.10 - index3.html
​
3、url_param
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
    bind *:80
    mode http
    #balance roundrobin
    #balance leastconn
    #balance static-rr
    #balance first
    #balance source
    #balance uri
    balance url_param name,userid
    hash-type consistent
    server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
    server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service 
​
#测试
[root@haproxy ~]# curl 172.25.254.100/index1.html?name=test
172.25.254.10 - index1.html
[root@haproxy ~]# curl 172.25.254.100/index1.html?name=haha
172.25.254.20 - index1.html
[root@haproxy ~]# curl 172.25.254.100/index1.html?name=xixi
172.25.254.10 - index1.html
[root@haproxy ~]# curl 172.25.254.100/index1.html?name=test
172.25.254.10 - index1.html
[root@haproxy ~]# curl 172.25.254.100/index1.html?name=haha
172.25.254.20 - index1.html
7、hdr
  • 不同浏览器访问不同

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
    bind *:80
    mode http
    #balance roundrobin
    #balance leastconn
    #balance static-rr
    #balance first
    #balance source
    #balance uri
    #balance url_param name,userid
    balance hdr(User-Agent)
    hash-type consistent
    server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
    server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service 
​
#测试 -vA 伪装浏览器
[root@haproxy ~]# curl -vA "firefox" 172.25.254.100/index.html

四、haproxy的高级功能

1、haproxy的状态页
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
listen stats
    mode http
    bind *:9999
    stats enable
    stats refresh 3
    stats uri /status  #自定义stats page uri
    stats auth lee:lee  #认证,可出现多次
访问浏览器:172.25.254.100:9999/status
2、基于cookie的会话保持

在一个浏览器访问后,会记住选择,之后刷新一直是该后端主机,另一个浏览器访问则是另一个后端主机

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
    balance roundrobin
    cookie WEBCOOKIE insert nocache indirect
    server web1 172.25.254.10:80 cookie lee1 check inter 2 fall 3 rise 5 weight 2
    server web2 172.25.254.20:80 cookie lee2 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service 
[root@haproxy ~]# curl -b WEBCOOKIE=lee1 172.25.254.100
webserver1 - 172.25.254.10
[root@haproxy ~]# curl -b WEBCOOKIE=lee2 172.25.254.100
webserver2 - 172.25.254.20
​
3、IP透传
七层代理
七层代理 mode--->http
#webserver1
[root@webserver1 ~]# systemctl disable nginx
[root@webserver1 ~]# systemctl stop nginx
[root@webserver1 ~]# dnf install httpd -y
[root@webserver1 ~]# echo webserver1 - 172.25.254.10 > /var/www/html/index.html
[root@webserver1 ~]# vim /etc/httpd/conf/httpd.conf 
如下图标注
%{X-Forwarded-For}i
[root@webserver1 ~]# systemctl enable --now httpd
​
​
#测试
[root@webserver1 ~]# tail -n 3 /etc/httpd/logs/access_log
[root@webserver2 ~]# tail -3 /var/log/nginx/access.log
有IP地址
# 如果把option forwardfor       except 127.0.0.0/8 注释掉则没有IP地址
四层代理
四层代理mode--->tcp
看不到IP地址
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
    mode tcp
    server web1 172.25.254.10:80  check inter 2 fall 3 rise 5 weight 2
    server web2 172.25.254.20:80 send-proxy check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service
​
#webserver2
[root@webserver2 ~]# 
 vim /etc/nginx/nginx.conf
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request"'
    ' "$proxy_protocol_addr"'
    
server {
        listen       80 proxy_protocol;
​
[root@webserver2 ~]# systemctl restart nginx
 
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
 server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
    server web2 172.25.254.20:80 send-proxy check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service
​
​
# 测试
[root@webserver2 ~]# tail -n 3 /var/log/nginx/access.log
[root@webserver1 ~]# tail -n 3 /etc/httpd/logs/access_log
再次访问后,查看日志可以看到地址
4、ACL常用参数
匹配域名
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster
    bind *:80
    mode http
    acl test hdr_dom(host) -i www.timinglee.org #判断规则
    use_backend webcluster-host if test  #是访问webcluster-host
    default_backend default-host   #不是访问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@haproxy ~]# systemctl restart haproxy.service
​
#注意 做这个要把四层代理proxy_protocol这个参数删掉
​
# 测试
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@haproxy ~]# curl www.timinglee.org
webserver1 - 172.25.254.10
​
以什么什么结尾
frontend webcluster
    bind *:80
    mode http
    acl test hdr_end(host) -i .org
    use_backend webcluster-host if test
    default_backend default-host
​
#测试
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@haproxy ~]# curl www.timinglee.org
webserver1 - 172.25.254.10
[root@haproxy ~]# curl www.timinglee.com
webserver2 - 172.25.254.20
​
以什么什么开头
frontend webcluster
    bind *:80
    mode http
    acl test hdr_beg(host) -i bbs
    use_backend webcluster-host if test
    default_backend default-host
    
    
#测试
[root@haproxy ~]# curl www.timinglee.org
webserver2 - 172.25.254.20
[root@haproxy ~]# curl bbs.timinglee.org
webserver1 - 172.25.254.10
base
frontend webcluster
    bind *:80
    mode http
    #acl test hdr_beg(host) -i bbs  
    acl test base_sub -m sub lee  #只要包含lee就是匹配成功
    use_backend webcluster-host if test
    default_backend default-host
​
​
[root@webserver1 ~]# mkdir /var/www/html/lee -p
[root@webserver1 ~]# echo 172.25.254.10 lee > /var/www/html/lee/index.html
[root@webserver1 ~]# curl 172.25.254.10/lee/
172.25.254.10 lee
​
​
#测试
[root@haproxy ~]# curl www.timinglee.com
webserver1 - 172.25.254.10
[root@haproxy ~]# curl bbs.timinglee.org
webserver1 - 172.25.254.10
[root@haproxy ~]# curl www.test.com
webserver2 - 172.25.254.20
[root@haproxy ~]# curl www.test.com/lee/
172.25.254.10 lee
5、ACL应用实例
基于源**IP或子网调度访问**

注意一定要有解析!!!!

frontend webcluster
    bind *:80
    mode http
    acl domain hdr_dom(host) -i www.timinglee.org
    use_backend webcluster-host if domain
    default_backend default-host
​
​
#测试
[root@haproxy ~]# curl www.test.com
webserver2 - 172.25.254.20
[root@haproxy ~]# curl www.timinglee.org
webserver1 - 172.25.254.10
​
基于源地址的访问控制

拒绝指定IP或者IP范围访问

frontend webcluster
    bind *:80
    mode http
    acl ctrl_ip  src 172.25.254.1 172.25.254.20
    use_backend webcluster-host if ctrl_ip
    default_backend default-host
​
#测试
​
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@webserver2 ~]# curl 172.25.254.100
webserver1 - 172.25.254.10
匹配浏览器类型

匹配客户端浏览器,将不同类型的浏览器调动至不同的服务器组、

范例: 拒绝curl和wget的访问

frontend webcluster
    bind *:80
    mode http
    acl badwebrowers hdr_sub(User-Agent) -i curl wget
    #use_backend webcluster-host if webrowers
    http-request deny if badwebrowers
    default_backend default-host
​
# 测试
去浏览器访问
基于文件后缀名实现动静分离
[root@webserver1 ~]# dnf install php -y
[root@webserver1 ~]# systemctl restart httpd
[root@webserver1 ~]# vim /var/www/html/index.php
[root@webserver1 ~]# cat /var/www/html/index.php 
<?php
    phpinfo();
?>
​
#haproxy
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
​
​
# 测试
浏览器进行测试

五、自定义HAProxy错误界面

基于自定义的错误页面文件

#webserver1\2主机上
system stop httpd/nginx
​
#haproxy主机上
[root@haproxy ~]# mkdir /etc/haproxy/errorpage -p
[root@haproxy ~]# vim /etc/haproxy/errorpage/503.http
HTTP/1.0 503 Service Unavailable
Cache-Control: no-cache
Connection: close
Content-Type: text/html;charset=UTF-8
​
<html><body><h1>什么动物生气最安静</h1>
大猩猩!!
</body></html>
[root@haproxy ~]# vim /etc/haproxy/haproxy.conf
defaults
    errorfile 503   /etc/haproxy/errorpage/503.http
[root@haproxy ~]# systemctl restart haproxy.service 
​
​
#测试
然后用浏览器去访问172.25.254.100

基于http重定向错误页面

#错误页面重定向
errorloc <code> <url>
#相当于errorloc302 <code> <url>,利用302重定向至指URL
#示例:
errorloc 503 https://www.baidu.com
​
​
# 实验
[root@haproxy ~]# vim /etc/haproxy/haproxy.conf
errorloc 503 https://www.baidu.com
[root@haproxy ~]# systemctl restart haproxy.service 
​
​
#测试
然后用浏览器去访问172.25.254.100

六、HaProxy 四层负载

# webserver1\2\3
dnf install mariadb-server -y
​
#webserver1
vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=1
systemctl start mariadb
mysql
> SELECT @@server_id;
> CREATE USER lee@'%' identified by 'lee';   #创建用户
> GRANT ALL ON *.* TO lee@'%';  
netstat -antup | grep 3306
​
#webserver2
vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=2
systemctl start mariadb
mysql
> SELECT @@server_id;
> CREATE USER lee@'%' identified by 'lee';
> GRANT ALL ON *.* TO lee@'%';
netstat -antup | grep 3306
​
#haproxy
dnf whatprovides */mysql
dnf install mariadb-server -y
mysql -uroot -h 172.25.254.10  #如果没有在webserver上加远程登录用户,则不能登录
vim /etc/haproxy/haproxy.conf
listen dbserver
    bind *:3306
    mode tcp
    balance static-rr
    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
    
systemctl restart haproxy
netstat -antup | grep 3306
​
# 测试
[root@haproxy ~]# mysql -ulee -plee -h 172.25.254.100
> SELECT @@server_id;

七、haproxy https实现

#haproxy
​
#证书制作
[root@haproxy ~]# mkdir -p /etc/haproxy/certs
[root@haproxy ~]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /etc/haproxy/certs/timinglee.org.key -x509 -days 356 -out /etc/haproxy/certs/timinglee.org.crt
CD shannxi XIan timinglee webserver www.timinglee.org admin@timinglee.org
​
[root@haproxy ~]# ls /etc/haproxy/certs/
[root@haproxy ~]# cat /etc/haproxy/certs/timinglee.org.key /etc/haproxy/certs/timinglee.org.crt > /etc/haproxy/certs/timinglee.pem
​
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
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
​
listen web-https
    bind *:443 ssl crt /etc/haproxy/certs/timinglee.pem
    mode http
    balance roundrobin
    server web1 172.25.254.10:3306 check inter 2 fall 2 rise 5
    server web2 172.25.254.20:3306 check inter 2 fall 2 rise 5
​
[root@haproxy ~]# systemctl restart haproxy
[root@haproxy ~]# netsata -antup | grep 443
​
测试:
开启webserver1\2的服务
访问https://172.25.254.100

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值