1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 高可用集群下的负载均衡(5):haproxy的动静分离与读写分离

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

时间:2021-05-23 05:39:01

相关推荐

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

一、haproxy动静分离

实验环境:

server2(haproxy):安装 haproxy

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

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

1. server4中进行设置

【1】安装php

【2】/etc/httpd/conf/httpd.con 中修改端口

【3】vim index.php

【4】在页面中进行访问

2. server3 中进行设置

【1】创建一个/var/www/html/images 放置图片

【2】在浏览器中访问

3. server2 中修改配置文件

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

4. 测试

二、haproxy 读写分离

1 . 实现环境:

server2(haproxy):安装 haproxy

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

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

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

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

2. 实验

【1】建立upload目录

此处以server4中为例,需要注意server3中也需要做此操作.

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

【2】可读写的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<?phpif ((($_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 GETacl read_request method HEADacl write_request method PUTacl write_request method POSTuse_backend staticif read_requestuse_backend app if write_requestdefault_backend static# default_backend app#---------------------------------------------------------------------# static backend for serving up images, stylesheets and such#---------------------------------------------------------------------backend staticbalanceroundrobinserverstatic 172.25.15.3:80 check#---------------------------------------------------------------------# round robin balancing between the various backends#---------------------------------------------------------------------backend appbalanceroundrobin# balancesource# balance static-rrserver app1 172.25.15.3:80 check# server app2 172.25.14.5:80 checkserver backup 127.0.0.1:8000 backup

【4】测试

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

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。