负载均衡的高可用集群(5)------haproxy的动静分离与读写分离

本文介绍了如何使用haproxy实现动静分离和读写分离。在动静分离中,配置了静态服务器(server3)用于处理图片请求,动态服务器(server4)处理PHP请求。在读写分离场景下,读操作在server3完成,而写操作如文件上传则在server4执行,确保了系统的高效和稳定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

haproxy的动静分离

1)实验前提

server2(haproxy):安装 haproxy

server3(静态服务器):在apache的默认发布目录下创建一个 images目录,并放一张图片

server4(动态服务器):安装php ,修改回 80 端口(之前做过端口转发,所以需要改回来)

2)server4中进行设置

  • 安装php

  • /etc/httpd/conf/httpd.con 中修改端口

 

  •  vim index.php

  •  在页面中进行访问

3)server3 中进行设置

  •  创建一个/var/www/html/images 放置图片

  • 访问

4)server2 中修改配置文件

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

 5)测试结果

haproxy的读写分离

1)实验前提

server2(haproxy):安装 haproxy

server3(读服务器):所有读取的动作都在server3

  • 举例:上传图片时,在未点击上传时,一直时在server3中
  • 安装PHP 
  • 读文件:在http默认发布目录中建立upload目录,并修改权限 ;将读写的php代码放入默认发布目录中

server4(写服务器):写入都在server4

  • 举例:点击上传后,是在server4中  
  • 写文件:在http默认发布目录中建立upload目录,并修改权限 ;将读写的php代码放入默认发布目录中

2)建立upload目录

以server4中为例,需要注意server3中也需要做此操作.(记得在server3安装php)

  • 删除之前做动静分离实验时创建的index.php文件,将upload文件中的文件已到apache的默认发布目录中

  •  可读写的php代码
[root@server2 html]# /var/www/html/index.php 
<html>
<body>
 
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>
 
</body>
</html>
 
 
 
[root@server2 html]# /var/www/html/upload_file.php
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 60000000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
 
    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

 

 

 3)修改配置文件

server2:

  • vim /etc/haproxy/haproxy.cfg
[root@server3 haproxy]# vim  /etc/haproxy/haproxy.cfg 
 acl read_request method GET
 acl read_request method HEAD
 acl write_request method PUT
 acl write_request method POST
 
    use_backend static          if read_request
    use_backend app             if write_request
    default_backend             static
#   default_backend             app
 
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin
    server      static 172.25.14.3:80 check
 
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
  balance     roundrobin
  #  balance     source
    # balance static-rr
    server  app1 172.25.14.3:80 check
#    server  app2 172.25.14.5:80 check
    server  backup 127.0.0.1:8000 backup

3)测试结果

  •  选择想要上传的图片读操作是在server3中

  • 点击Submit上传后出现如下信息,显示上传成功(写入到server4中了)

图片是被上传到了server4中的upload目录中!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值