haproxy

一、负载均衡

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

阿里云SLB介绍 :https://yq.aliyun.com/articles/1803
在这里插入图片描述
硬件,买来就能用负载均衡,可以网页使用,傻瓜式操作

1.1 四层负载均衡

虚拟url只能在七层中去做
在这里插入图片描述
1.通过ip+port决定负载均衡的去向。
2.对流量请求进行NAT处理,转发至后台服务器。
3.记录tcp、udp流量分别是由哪台服务器处理,后续该请求连接的流量都通过该服务器处理。
4.支持四层的软件
lvs:重量级四层负载均衡器。
Nginx:轻量级四层负载均衡器,可缓存。(nginx四层是通过upstream模块)
Haproxy:模拟四层转发

二、haproxy介绍

timeout queue:最好设置为60s

defaults
    mode                    http                # HAProxy实例使用的连接协议          
    log                     global              #指定日志地址和记录日志条目的 
syslog/rsyslog日志设备
                                                #此处的 global表示使用 global配置段中
设定的log值。
    
    option                  httplog             #日志记录选项,httplog表示记录与 HTTP
会话相关的各种属性值
                                                #包括 HTTP请求、会话状态、连接数、源地
址以及连接时间等
    
    option                  dontlognull         #dontlognull表示不记录空会话连接日志
    
    option http-server-close                    #等待客户端完整HTTP请求的时间,此处为等
待10s。
    
    option forwardfor       except 127.0.0.0/8  #透传客户端真实IP至后端web服务器
                                                #在apache配置文件中加入:<br>%{X
Forwarded-For}i   
                                                #后在webserer中看日志即可看到地址透传
信息
    
    option                  redispatch          #当server Id对应的服务器挂掉后,强制定
向到其他健康的服务器,重新派发
    
    option                  http-keep-alive     #开启与客户端的会话保持
    
    retries                 3                   #连接后端服务器失败次数
    
    timeout http-request    1000s                   #等待客户端请求完全被接收和处理的最
长时间
    
    timeout queue           60s                 #设置删除连接和客户端收到503或服务不可
用等提示信息前的等待时间
   
    timeout connect         120s                #设置等待服务器连接成功的时间
    
    timeout client          600s                #设置允许客户端处于非活动状态,即既不发
送数据也不接收数据的时间
    
    timeout server          600s                #设置服务器超时时间,即允许服务器处于既
不接收也不发送数据的非活动时间
    
    timeout http-keep-alive 60s                 #session 会话保持超时时间,此时间段内
会转发到相同的后端服务器
    
    timeout check           10s                 #指定后端服务器健康检查的超时时间
    
    maxconn                 3000
    
    default-server inter 1000 weight 3         

server配置

#针对一个server配置
check           
#对指定real进行健康状态检查,如果不加此设置,默认不开启检查,只有check后面没
有其它配置也可以启用检查功能
#默认对相应的后端服务器IP和端口,利用TCP连接进行周期性健康性检查,注意必须指定
端口才能实现健康性检查
addr <IP>       
port <num>      
inter <num>     
fall <num>      
#可指定的健康状态监测IP,可以是专门的数据网段,减少业务网络的流量
#指定的健康状态监测端口
#健康状态检查间隔时间,默认2000 ms
 #后端服务器从线上转为线下的检查的连续失效次数,默认为3
rise <num>      
#后端服务器从下线恢复上线的检查的连续有效次数,默认为2
 weight <weight> #默认为1,最大值为256,0(状态为蓝色)表示不参与负载均衡,但仍接受持久连接
backup          
Server
 disabled        
#将后端服务器标记为备份状态,只在所有非备份主机down机时提供服务,类似Sorry 
#将后端服务器标记为不可用状态,即维护状态,除了持久模式
#将不再接受连接,状态为深黄色,优雅下线,不再接受新用户的请求
maxconn <maxconn>                       
redirect prefix http://www.baidu.com/   #将请求临时(302)重定向至其它URL,只适用于http模
式
#当前后端server的最大并发连接数

实例检测

haproxy机

[root@localhost ~]# dnf install httpd -y

backup --sorryserver的端口

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf

# Change this to Listen on a specific IP address, but note that if
# httpd.service is enabled to run at boot time, the address may not be
# available when the service starts.  See the httpd.service(8) man
# page for more information.
#
#Listen 12.34.56.78:80

Listen 8080

[root@localhost ~]# systemctl enable --now httpd

[root@localhost ~]# echo sorry 下班了 > /var/www/html/index.html

[root@localhost ~]# systemctl restart httpd

[root@localhost ~]# 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 1
    server web_sorry 172.25.254.100:8080 backup

#web1和web2但凡有一台是开启状态,则web_sorry是访问不了的

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

下线指定realserver,相当于关闭,若想开启,则删除disabled

[root@localhost ~]# 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 1 disabled
    server web_sorry 172.25.254.100:8080 backup

#web1和web2但凡有一台是开启状态,则web_sorry是访问不了的

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

网页重定向

[root@localhost ~]# 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 disabled
    #server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1 disabled
    #server web_sorry 172.25.254.100:8080 backup


#web1和web2但凡有一台是开启状态,则web_sorry是访问不了的

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

本地终端检测

curl 172.25.254.100
sorry 下班了

---------------------------结束

2.1 socat工具

作用:动态调整haproxy的参数

#查看集群权重
[root@haproxy ~]# echo get weight webcluster/web1 | socat stdio 
/var/lib/haproxy/stats
 2 (initial 2)
 [root@haproxy ~]# echo get weight webcluster/web2 | socat stdio 
/var/lib/haproxy/stats
 1 (initial 1)
 #设置权重
[root@haproxy ~]# echo "set  weight  webcluster/web1 1 " | socat stdio 
/var/lib/haproxy/stats
 [root@haproxy ~]# echo "set  weight  webcluster/web1 2 " | socat stdio 
/var/lib/haproxy/stats
 #下线后端服务器
[root@haproxy ~]# echo "disable server  webcluster/web1 "  | socat stdio 
/var/lib/haproxy/stats
 #上线后端服务器
[root@haproxy ~]# echo "enable server  webcluster/web1 "  | socat stdio  
/var/lib/haproxy/stats

webcluster/后的这个名字是自己配置的,在上述实验中,名字为web1或web2或web_sorry

实例

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

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

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

下载socat工具

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

查看帮助

[root@localhost ~]# echo “help” | socat stdio/var/lib/haproxy/stats

查看状态

[root@localhost ~]# echo "show info " | socat stdio /var/lib/haproxy/stats

查看server状态

[root@localhost ~]# echo "show servers state " | socat stdio /var/lib/haproxy/stats

1
# be_id be_name srv_id srv_name srv_addr srv_op_state srv_admin_state srv_uweight srv_iweight srv_time_since_last_change srv_check_status srv_check_result srv_check_health srv_check_state srv_agent_state bk_f_forced_id srv_f_forced_id srv_fqdn srv_port srvrecord srv_use_ssl srv_check_port srv_check_addr srv_agent_addr srv_agent_port
2 webcluster 1 web1 172.25.254.10 0 5 2 2 204 1 0 0 14 0 0 0 - 80 - 0 0 - - 0
2 webcluster 2 web2 172.25.254.20 0 5 1 1 204 1 0 0 14 0 0 0 - 80 - 0 0 - - 0
4 static 1 static 127.0.0.1 0 0 1 1 203 8 2 0 6 0 0 0 - 4331 - 0 0 - - 0
5 app 1 app1 127.0.0.1 0 0 1 1 203 8 2 0 6 0 0 0 - 5001 - 0 0 - - 0
5 app 2 app2 127.0.0.1 0 0 1 1 203 8 2 0 6 0 0 0 - 5002 - 0 0 - - 0
5 app 3 app3 127.0.0.1 0 0 1 1 202 8 2 0 6 0 0 0 - 5003 - 0 0 - - 0
5 app 4 app4 127.0.0.1 0 0 1 1 202 8 2 0 6 0 0 0 - 5004 - 0 0 - - 0


查看权重

[root@localhost ~]# echo get weight webcluster/web1 | socat stdio /var/lib/haproxy/stats
2 (initial 2)

[root@localhost ~]# echo get weight webcluster/web2 | socat stdio /var/lib/haproxy/stats
1 (initial 1)

修改web2权重为2,修改成对172.25.254.10多访问一次

[root@localhost ~]# echo "set weight webcluster/web2 2 " | socat stdio /var/lib/haproxy/stats

[root@localhost ~]# echo get weight webcluster/web2 | socat stdio /var/lib/haproxy/stats
2 (initial 1)

​ 修改以后的权重,以当前为准,也就是最前头的数字

下线一台webserver

[root@localhost ~]# echo "disable server webcluster/web1 " | socat stdio /var/lib/haproxy/stats

启动该台webserver

[root@localhost ~]# echo "enable server webcluster/web1 " | socat stdio /var/lib/haproxy/stats
在这里插入图片描述

设置多进程

ll查询后发现只有一个进程

[root@localhost ~]# ll /var/lib/haproxy/
total 0
srw------- 1 root root 0 Aug 9 11:39 stats

haproxy多进程如何热处理

[root@localhost ~]# 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

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------

[root@localhost ~]# systemctl restart rsyslog.service

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

查看stats是否还可以使用

[root@localhost ~]# echo "show info " | socat stdio /var/lib/haproxy/stats

2024/08/09 11:55:11 socat[4133] E connect(5, AF=1 "/var/lib/haproxy/stats", 24): Connection refused

三、haproxy算法

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

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

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

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

3.1 静态算法

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

​ 根据权重来分配服务器

​ 不支持慢启动

​ 不支持热

下图为配置

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

listen webcluster
 	bind *:80
 	mode http
 	balance static-rr
 	server web1 192.168.0.10:80 weight 2 check inter 2 fall 3 rise 5
 	server web2 192.168.0.20:80 weight 1 check inter 2 fall 3 rise 5

刷新-systmctl

前提:一个进程,需要关闭第二个进程

first算法

​ 根据服务器在列表中的位置,自上而下进行调度

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

listen webcluster
 	bind *:80
 	mode http
 	balance first
 	server web1 192.168.0.10:80 weight 2 check inter 2 fall 3 rise 5
 	server web2 192.168.0.20:80 weight 1 check inter 2 fall 3 rise 5

在多台主机中执行死循环测试效果
在这里插入图片描述在这里插入图片描述

3.2 动态算法

3.2.1 roundrobin

找负载小的,就是谁闲了给谁

配置

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

listen webcluster
 	bind *:80
 	mode http
 	balance roundrobin
 	server web1 192.168.0.10:80 weight 2 check inter 2 fall 3 rise 5
 	server web2 192.168.0.20:80 weight 1 check inter 2 fall 3 rise 5

3.2.2 leastconn

​ 谁的链接最少的就给谁

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

listen webcluster
 	bind *:80
 	mode http
 	balance leastconn
 	server web1 192.168.0.10:80 weight 2 check inter 2 fall 3 rise 5
 	server web2 192.168.0.20:80 weight 1 check inter 2 fall 3 rise 5

3.3 其他算法

​ 其它算法即可作为静态算法,又可以通过选项成为动态算法

3.3.1 source

源地址hash,同一个源地址送往同一个服务器上。但一个服务器损毁后,会出现数据丢失。

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

listen webcluster
 	bind *:80
 	mode http
 	balance source
 	server web1 192.168.0.10:80 weight 2 check inter 2 fall 3 rise 5
 	server web2 192.168.0.20:80 weight 1 check inter 2 fall 3 rise 5

3.3.2 map-base 取模法

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

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

listen webcluster
 	bind *:80
 	mode http
 	balance source
 	server web1 192.168.0.10:80 weight 2 check inter 2 fall 3 rise 5
 	server web2 192.168.0.20:80 weight 1 check inter 2 fall 3 rise 5
 	
 #不支持动态调整权重值
[root@localhost ~]# echo "set weight webserver_80/webserver1 2" | socat stdio 
/var/lib/haproxy/haproxy.sock
 Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.
 
 #只能动态上线和下线
[root@localhost ~]# echo "set weight webserver_80/webserver1 0" | socat stdio 
/var/lib/haproxy/haproxy.sock
 [root@localhost ~]# echo "get  weight webserver_80/webserver1" | socat stdio 
/var/lib/haproxy/haproxy.sock
 0 (initial 1)

3.3.3 一致性hash

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

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

hash环偏斜问题

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

hash对象
在这里插入图片描述

一致性hash示意图

后端服务器在线与离线的调度方式

在这里插入图片描述

4.4 uri

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

uri一致性hash配置

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

listen webcluster
 	bind *:80
 	mode http
 	balance uri
 	hash-type consistent
 	server web1 192.168.0.10:80 weight 2 check inter 2 fall 3 rise 5
 	server web2 192.168.0.20:80 weight 1 check inter 2 fall 3 rise 5

测试:curl 172.25.254.100/index.html

4.4.1 url_param

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

listen webcluster
 	bind *:80
 	mode http
 	#balance uri
 	balance url_param name,userid
 	hash-type consistent
 	server web1 192.168.0.10:80 weight 2 check inter 2 fall 3 rise 5
 	server web2 192.168.0.20:80 weight 1 check inter 2 fall 3 rise 5

测试:curl 172.25.254.100/index.html?name=lee

4.4.2 hdr

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

listen webcluster
 	bind *:80
 	mode http
 	#balance uri
 	#balance uri_param name,userid
 	balance hdr_User-Agent
 	hash-type consistent
 	server web1 192.168.0.10:80 weight 2 check inter 2 fall 3 rise 5
 	server web2 192.168.0.20:80 weight 1 check inter 2 fall 3 rise 5

测试:curl 172.25.254.100/index.html

四、HAProxy状态页

通过web界面,显示当前HAProxy的运行状态

4.1 状态页配置项

stats enable           #基于默认的参数启用stats page      
stats hide-version     #将状态页中haproxy版本隐藏     
stats refresh <delay>  #设定自动刷新时间间隔,默认不自动刷新      
stats uri <prefix>     #自定义stats page uri,默认值:/haproxy?stats      
stats auth <user>:<passwd>  #认证时的账号和密码,可定义多个用户,每行指定一个用户
                            #默认:no authentication
stats admin { if | unless } <cond> #启用stats page中的管理功

4.2 配置状态页

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

listen stats:
 	mode http
 	bind *:9999
 	stats enable
 	stats refresh 1            #改为1s刷新一次
 	stats uri /status          #自定义stats page uri         
	stats auth lee:lee         #认证,此行可以出现多次         

浏览器测试
在这里插入图片描述

六、HAProxy高级功能

6.1 基于cookie的会话保持

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

​ 注意:不支持tcp mode(四层),使用http mode(七层)

效果:会话能保持多久,看浏览器,浏览器能保存多久,会话就能一致保持

打开hash一致性
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
在这里插入图片描述
[root@localhost ~]# systemctl restart haproxy.service

6.1.1 配置选项

cookie name [ rewrite | insert | prefix ][ indirect ] [ nocache ][ postonly ] [ preserve ][ httponly ] [ secure ][ domain ]* [ maxidle <idle> ][ maxlife ]

name:       #cookie 的 key名称,用于实现持久连接
insert:     #插入新的cookie,默认不插入cookie
indirect:   #如果客户端已经有cookie,则不会再发送cookie信息
nocache:    #当client和hapoxy之间有缓存服务器(如:CDN)时,不允许中间缓存器缓存
cookie:     #因为这会导致很多经过同一个CDN的请求都发送到同一台后端服务器

需要对cookie值做一个定义

6.1.2 配置实例

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

listen webcluster
    bind *:80
    mode http
    balance roundrobin
    cookie WEBCOOKIE insert nocache indirect
    server web1 172.25.254.10:80 cookie lee1 check inter 2 fall 3 rise 5 weight 1
    server web2 172.25.254.20:80 cookie lee2 check inter 2 fall 3 rise 5 weight 1

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

网页测试
在这里插入图片描述

终端测试

前提:注意在/etc/haproxy/haproxy.cfg中自己配置cookie行第二个参数,自己配置的名字

| 10/08/2024 09:59.18 /home/mobaxterm 》curl -b WEBCOOKIE=lee1 172.25.254.100webserver1 - 172.25.254.10

| 10/08/2024 09:59.47 /home/mobaxterm 》 curl -b WEBCOOKIE=lee2 172.25.254.100
webserver2 - 172.25.254.20

1.2 IP透传

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

6.2.1 配置

lvs机

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

listen webcluster
    bind *:80
    mode http   //改为tcp也可以使用
    balance roundrobin
    server web1 172.25.254.10:80 send-proxy check inter 2 fall 3 rise 5 weight 1
    server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1

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

webserver1

[root@localhost ~]# vim /etc/nginx/nginx.conf

server {
        listen       80 proxy_protocol;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

[root@localhost ~]# systemctl restart nginx

测试

终端:curl 172.25.254.100

webserver1:[root@localhost ~]# cat /var/log/nginx/access.log

webserver2

—先停止webserver2的nginx服务(disable , stop),然后下载httpd----

[root@localhost ~]# systemctl disable nginx.service

[root@localhost ~]# systemctl stop nginx.service

[root@localhost ~]# dnf install httpd -y

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf

<IfModule log_config_module>
    #
    # The following directives define some format nicknames for use with
    # a CustomLog directive (see below).
    #
    #在此处加入一行%{X-Forwarded-For}i
    LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      # You need to enable mod_logio.c to use %I and %O
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>

[root@localhost ~]# systemctl enable --now httpd

[root@localhost ~]# systemctl restart httpd

测试

终端:curl 172.25.254.100

webserver2:[root@localhost ~]# cat /etc/httpd/logs/access_log

6.3 ACL

访问控制列表ACL,Access Control Lists)

是一种基于包过滤的访问控制技术

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

6.3.1 acl配置

lvs机

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

#listen webcluster
#    bind *:80
#    mode http
#    balance roundrobin
    #balance first
    #balance static-rr
    #balance leastconn
    #balance source
    #balance uri
    #balance url_param name,userid
    #balance hdr(User-Agent)
    #hash-type consistent
    #redirect prefix http://www.baidu.com/
    #cookie WEBCOOKIE insert nocache indirect
#    server web1 172.25.254.10:80 send-proxy check inter 2 fall 3 rise 5 weight 1
#    server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
    #server web_sorry 172.25.254.100:8080 backup

listen stats
    mode http
    bind *:9999
    stats enable
    stats uri /stats
    stats auth lee:lee

frontend webcluster
    bind *:80
    mode http
    acl test hdr_dom(host) -i www.timinglee.org
    #acl test hdr_end(host) -i .org   #设置以.org结尾
    #acl test hdr_beg(host) -i bbs    #设置以bbs开头
    #acl domain hdr_beg(host) -i bbs
    #acl test base_sub -m sub bbs
    #acl test base_reg -i lee/$
    #acl test path_sub -m sub lee
    use_backend webcluster-host if test
    #use_backend webcluster-host if ! test
    #use_backend webcluster-host if test domain
    #use_backend webcluster-host if test || domain
    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


#frontend main
#    bind *:5000
#    acl url_static       path_beg       -i /static /images /javascript /stylesheets
#    acl url_static       path_end       -i .jpg .gif .png .css .js
#
#    use_backend static          if url_static
#    default_backend             app

webserver1

[root@localhost ~]# vim /etc/nginx/nginx.conf

server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

[root@localhost ~]# systemctl restart nginx

终端测试1

|  10/08/2024   14:43.28   /home/mobaxterm  > curl 172.25.254.100
webserver2 - 172.25.254.20

终端测试2

使用管理员权限打开记事本->点击文件->点击打开->输入C:\Windows\System32\drivers\etc路径->打开hosts文件->编写

172.25.254.100  www.timinglee.org  www.timinglee.com  bbs.timinglee.org

|  10/08/2024   14:49.20   /home/mobaxterm  > curl www.timinglee.org
webserver1 - 172.25.254.10

基于域名的访问

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

frontend webcluster
    bind *:80
    mode http
	#基于域名的访问
    acl domain har_dom(host) -i www.timinglee.org
    user_backend webcluster-host if domain
    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


基于ip的访问

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

frontend webcluster
    bind *:80
    mode http
	#基于ip的访问
    acl ctrl_ip src 172.25.254.1 172.25.254.20 192.168.0.0/24
    user_backend webcluster-host if ctrl_ip #允许ip
    #http-request denf if ctrl_ip #拒绝ip

    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@localhost ~]# vim /etc/haproxy/haproxy.cfg

frontend webcluster
    bind *:80
    mode http
    acl badwebrowers hdr_sub(User-Agent) -i curl wegt
    http-request deny if badwebrowers

    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@localhost ~]# vim /etc/haproxy/haproxy.cfg

frontend webcluster
    bind *:80
    mode http
    #acl static path_end -i .html .jpg .png .css .js
    #acl php    path_end -i .php
    acl static path_sub -m sub static
    acl php    path_sub -m sub 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

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

global
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    errorfile 503 /etc/haproxy/errorpage/503.http
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats 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

[root@localhost ~]# 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                 10000
    errorfile 503 /etc/haproxy/errorpage/503.http
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------

在这里插入图片描述

[root@localhost ~]# 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                 10000
    #errorfile 503 /etc/haproxy/errorpage/badpage.http
    errorloc 503 https://www.baidu.com
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------

webserver1

下载好mariadb-server,打开并确定3306端口打开

[root@localhost ~]# vim /etc/my.cnf.d/mariadb-server.cnf

[mysqld]
#server-id=1
server-id=2
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-error=/run/mariadb/mariadb.pid

:wq

listen dbserver
	bind *:3306
	mode tcp
	balance static-rr
	server db1 172.25.254.10:3306 check inter 2 fall 3 rise 5 
    server db2 172.25.254.20:3306 check inter 2 fall 3 rise 5 
	

在这里插入图片描述

[root@localhost ~]# vim /lib/systemd/system/haproxy.service

[root@localhost ~]# cd /etc/haproxy/haproxy.cfg

[root@localhost conf.d]# vim webcluster.cfg

listen stats
 	mode http
 	bind *:9999
 	stats enable
 	stats refresh 3        
 	stats uri /status              
	stats auth lee:lee       

[root@localhost ~]# systemctl restart nginx

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值