主机环境 redhat6.5 64位
实验环境 服务端1 ip 172.25.29.2
服务端2 ip 172.25.29.3
管理端1 ip 172.25.29.1
防火墙状态:关闭
1.Haproxy的负载均衡及测试
A)负载均衡的配置
[[email protected] ~]# yum install haproxy -y #安装haproxy
[[email protected] ~]# vim /etc/haproxy/haproxy.cfg #进入主配置文件
59 stats uri /status #虚拟目录,后面的全标记,自己添加 60 ##--------------------------------------------------------------------- 61 ## main frontend which proxys to the backends 62 ##--------------------------------------------------------------------- 63 #frontend main *:5000 64 # acl url_static path_beg -i /static /images /javascript /sty lesheets 65 # acl url_static path_end -i .jpg .gif .png .css .js 66 # 67 # use_backend static if url_static 68 # default_backend app 69 # 70 ##--------------------------------------------------------------------- 71 ## static backend for serving up images, stylesheets and such 72 ##--------------------------------------------------------------------- 73 #backend static 74 # balance roundrobin 75 # server static 127.0.0.1:4331 check 76 # 77 ##--------------------------------------------------------------------- 78 ## round robin balancing between the various backends 79 ##--------------------------------------------------------------------- 80 #backend app 81 # balance roundrobin 82 # server app1 127.0.0.1:5001 check 83 # server app2 127.0.0.1:5002 check 84 # server app3 127.0.0.1:5003 check 85 # server app4 127.0.0.1:5004 check 86 listen wen *:80 #添加服务 87 balance roundrobin#机制:轮叫 88 server web1 172.25.29.2:80 check #真正的服务端 89 server web2 172.25.29.3:80 check #真正的服务端
[[email protected] ~]# /etc/init.d/haproxy start #开启haproxy
Starting haproxy: [ OK ]
B)测试
在测试之前,服务端1和服务端2要安装httpd,开启httpd服务,并且有不同的测试页。
[[email protected] ~]# /etc/init.d/httpd start
Starting httpd: [ OK ]
[[email protected] ~]# /etc/init.d/httpd start
Starting httpd: [ OK ]
开始测试:172.25.29.1/status (服务端1和服务端2httpd服务都开启
测试:172.25.29.1
刷新之后
当关闭服务端2的httpd服务时:
[[email protected] ~]# /etc/init.d/httpd stop
Starting httpd: [ OK ]
刷新之后,结果如图:(只有wed1运行,wed2停止)
当再关闭服务端1的httpd服务时:
[[email protected] ~]# /etc/init.d/httpd stop
Starting httpd: [ OK ]
刷新之后,结果如图:(wed1和wed2都停止)
2.Haproxy中将管理和负载均衡分开及测试
A) 将管理和负载均衡分开的配置
管理端要安装httpd,要有测试页,在/etc/httpd/conf/httpd.conf中将端口改成8080,不然会冲突。
[[email protected] ~]# vim /etc/haproxy/haproxy.cfg #修改主配置文件
86 frontend wen *:80 87 default_backend app 88 backend app 89 balance roundrobin 90 server web1 172.25.29.2:80 check 91 server web2 172.25.29.3:80 check 92 server local 127.0.0.1:8080 backup
[[email protected] ~]# /etc/init.d/haproxy reload#刷新
Reloading haproxy:
B)测试
开始测试(开启管理端、服务端1、服务端2的httpd服务)
访问172.25.29.1时,服务端1和服务端2再刷新前后交替出现。
当停止服务端2的httpd时:
[[email protected] ~]# /etc/init.d/httpd stop
Stopping httpd: [ OK ]
访问172.25.29.1时,只有服务端1刷新之后也不变。
再停止服务端1的httpd时:
[[email protected] ~]# /etc/init.d/httpd stop
Stopping httpd: [ OK ]
访问172.25.29.1时,出现管理端的测试页
3.Haproxy日志的独立及测试
A)日志独立的配置
[[email protected] ~]# vim /etc/rsyslog.conf #日志的配置文件
13 $ModLoad imudp 14 $UDPServerRun 514 #开启端口和端口号 42 *.info;mail.none;authpriv.none;cron.none;local2.none /var/log/ messages #haproxy的日志不在messages里做记录 63 local2.* /var/log/haproxy.log #将haproxy的日志文件单独记录在haproxy.log中
[[email protected] ~]# /etc/init.d/rsyslog restart
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
B)测试
[[email protected] ~]# tail -f /var/log/haproxy.log #刚开始没有日志
当在浏览器中访问172.25.29.1后出现日志,如图:
4.Haproxy黑名单及测试
A)黑名单的配置
[[email protected] log]# vim /etc/haproxy/haproxy.cfg
86 frontend wen *:80 87 acl badhost src 172.25.29.250 #禁止ip(172.25.29.250)的访问 88 block if badhost #如果禁止访问的ip进行访问 89 errorloc 403 http://172.25.29.1:8080 #如果访问,页面中出现的是http://172.25.29.1:8080中的信息 # redirect location http://172.25.29.1:8080 if badhost #这句和(88+89)的作用相同
[[email protected] log]# /etc/init.d/haproxy reload #刷新
Reloading haproxy:
B)测试
开始测试(管理端、服务端1、服务端2的httpd服务全开启)
Ip为172.25.29.250进行测试,刷新不变
用其他的ip进行测试
刷新之后
5.Haproxy的动静分离及测试
A)动静分离的配置
管理端
[[email protected] log]# vim /etc/haproxy/haproxy.cfg
86 frontend wen *:80 87 acl url_static path_beg -i /images #定义开始images 88 acl url_static path_end -i .gif #结尾.gif 89 use_backend app if url_static #如果是动态,访问app 90 default_backend html #默认访问html 91 92 backend html #名字 93 balance roundrobin 94 server web1 172.25.29.2:80 check 95 96 backend app 97 balance roundrobin 98 server web2 172.25.29.3:80 check
[[email protected] log]# /etc/init.d/haproxy reload #刷新
Reloading haproxy:
在服务端2作一些简单的配置
[[email protected] html]# pwd #当前目录
/var/www/html
[[email protected] html]# mkdir images #创建images目录
[[email protected] html]# mv osi.gif images/ #将图片移动到images
B)测试
测试 访问172.25.29.1结果:
如果访问172.25.29.1/images/osi.gif,结果:
6.Haproxy读写分离及测试
A)读写分离的配置
[[email protected] log]# vim /etc/haproxy/haproxy.cfg
86 frontend wen *:80 87 acl read method DET 88 acl read method HEAD 89 acl write method PUT 90 acl write method POST 91 92 use_backend app if write #如果是写,访问app 93 default_backend html #默认访问htm 94 95 backend html 96 balance roundrobin 97 server web1 172.25.29.2:80 check 98 99 backend app 100 balance roundrobin 101 server web2 172.25.29.3:80 check
[[email protected] log]# /etc/init.d/haproxy reload
Reloading haproxy:
对服务端1和服务端2安装php及写一些简单的php测试文件
服务端1
[[email protected] html]# yum install php -y #安装php
[[email protected] html]# pwd#当前目录
/var/www/html
[[email protected] html]# vim index.php #上传的测试文件(读)
1 <html> 2 <body> 3 4 <form action="upload_file.php" method="post" 5 enctype="multipart/form-data"> 6 <label for="file">Filename:</label> 7 <input type="file" name="file" id="file" /> 8 <br /> 9 <input type="submit" name="submit" value="Submit" /> 10 </form> 11 12 </body> 13 </html>
[[email protected] html]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
服务端2
[[email protected] html]# yum install php -y #安装php
[[email protected] html]# vim upload_file.php #存储的测试文件(写)
1 <?php 2 if ((($_FILES["file"]["type"] == "image/gif") 3 || ($_FILES["file"]["type"] == "image/jpeg") 4 || ($_FILES["file"]["type"] == "image/png")) 5 && ($_FILES["file"]["size"] < 2000000)) 6 { 7 if ($_FILES["file"]["error"] > 0) 8 { 9 echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; 10 } 11 else 12 { 13 echo "Upload: " . $_FILES["file"]["name"] . "<br />"; 14 echo "Type: " . $_FILES["file"]["type"] . "<br />"; 15 echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; 16 echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; 17 18 if (file_exists("upload/" . $_FILES["file"]["name"])) 19 { 20 echo $_FILES["file"]["name"] . " already exists. "; 21 } 22 else 23 { 24 move_uploaded_file($_FILES["file"]["tmp_name"], 25 "upload/" . $_FILES["file"]["name"]); 26 echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; 27 } 28 } 29 } 30 else 31 { 32 echo "Invalid file"; 33 } 34 ?>
[[email protected] html]# mkdir upload
[[email protected] html]# chmod 777 upload
[[email protected] html]# /etc/init.d/httpd restart #重启httpd
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
B)测试
开始测试 172.25.29.1
点Browse上传图片,从php的测试文件里可以看出后缀只能是.gif,.jpeg,.png的文件
点击Submit后,上传成功,下图是上传信息
虽然在服务端1上传了文件,但是存储到了服务端2,实现了读写分离
[[email protected] html]# cd upload
[[email protected] upload]# ls#查看
Screenshot from 2016-10-21 17:38:41.png