使用HAProxy实现动静分离

解读:

  • 使用haproxy的ACL访问控制实现调度后端服务的动静分离
  • 每个RS上都部署相同业务实现负载均衡

实验环境

主机名 IP OS
haproxy eth0:172.25.254.100 Rocky Linux9.6
rs1 eth0:172.25.254.10 Rocky Linux9.6
rs2 eth0:172.25.254.20 Rocky Linux8.6

流程图

网络配置

网络配置使用自定义脚本来快速设置IP网关DNS主机名等

bash 复制代码
[root@haproxy ~]# vmset.sh eth0 172.25.254.100 haproxy
[root@rs1 ~]# vmset.sh eth0 172.25.254.10 rs1
[root@rs2 ~]# vmset.sh eth0 172.25.254.20 rs2

业务部署

RS部署web服务

bash 复制代码
[root@rs1 ~]# dnf install nginx -y
[root@rs2 ~]# dnf install nginx -y
[root@rs1 ~]# systemctl enable --now nginx
[root@rs2 ~]# systemctl enable --now nginx

[root@rs1 ~]# echo rs1 - webserver1 > /usr/share/nginx/html/index.html
[root@rs1 ~]# curl localhost
rs1 - webserver1

[root@rs2 ~]# echo rs2 - webserver2 > /usr/share/nginx/html/index.html
[root@rs2 ~]# curl localhost
rs2 - webserver2

RS2部署php

bash 复制代码
[root@rs2 ~]# dnf install php -y
[root@rs2 ~]# systemctl enable --now php-fpm

nginx添加php解析

bash 复制代码
[root@rs1+2 ~]# vim /etc/nginx/nginx.conf
server {
......
	location ~ \.php$ {
            root /usr/share/nginx/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  				      
            $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
[root@rs1+2 ~]# systemctl restart nginx.service

测试是否能访问

haproxy部署

安装haproxy

bash 复制代码
[root@haproxy ~]# dnf install haproxy -y

编写haproxy规则

bash 复制代码
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
......
frontend webcluster
    bind    172.25.254.100:80

    acl url_static path_end -i .jpg .png .css .js .html
    acl url_php path_end -i .php

    use_backend webservera if url_static
    use_backend webserverb if url_php

backend webservera
    server web1 172.25.254.10:80 check
    server web2 172.25.254.20:80 check
backend webserverb
    server web1 172.25.254.10:80 check
    server web2 172.25.254.20:80 check

[root@haproxy ~]# systemctl enable --now haproxy.service

测试

k

server web2 172.25.254.20:80 check

root@haproxy \~\]# systemctl enable --now haproxy.service **测试** ![image-20260130183554123](https://i-blog.csdnimg.cn/img_convert/b9bbf674a271b53e45c548118a636bb0.png) ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/deea89651ade40f6b9dc83394f5c793c.png#pic_center)

相关推荐
2023自学中16 小时前
笔记本电脑 连接 手机WIFI,开发板网线连接笔记本,开发板 和 虚拟机 同时上网
linux·单片机·嵌入式硬件·tcp/ip
funnycoffee1231 天前
linux系统DNS修改命令
linux·运维·服务器·linux dns
小哈里1 天前
【工具】Linux远程开发核心工具,Git命令缩写与SSH常用命令
linux·git·ssh·工具·远程开发
夏乌_Wx1 天前
深入理解x86内存寻址:从8086实模式到IA-32段页式映射&Linux内核实现
linux
czxyvX1 天前
012-Linux简易Shell编写
linux
清漠2331 天前
win11“网络和Internet“中无“以太网“这个选项解决记录
服务器·网络·数据库
S-码农1 天前
Linux 进程核心知识
linux
努力努力再努力wz1 天前
【Linux网络系列】:TCP 的秩序与策略:揭秘传输层如何从不可靠的网络中构建绝对可靠的通信信道
java·linux·开发语言·数据结构·c++·python·算法
袁小皮皮不皮1 天前
数据通信20-IPv6基础
运维·服务器·网络·网络协议·智能路由器
醒醒该学习了!1 天前
如何将json文件转成csv文件(python代码实操)
服务器·python·json