HAProxy简介
HAProxy 是一款提供高可用性、负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。 HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在时下的硬件上,完全可以支持数以万计的 并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
- 实验环境
四台虚拟机
server1_lue2 : 172.25.6.4
server2_lue3 : 172.25.6.5
server3_lue4 : 172.25.6.6
server3_lue5 : 172.25.6.7
注:lue2和lue3做调度器,lue4和lue5做服务器
- 安装haproxy
在调度器中安装<lue3> 注:在调度器中必须关掉apache
yum install -y haproxy
vim /etc/haproxy/haproxy.cfg
62 #---------------------------------------------------------------------
63 # main frontend which proxys to the backends
64 #---------------------------------------------------------------------
65 frontend main *:80 <修改端口为80>
66 #acl blacklist src 172.25.6.250
67 #block if blacklist
68 #errorloc 403 http://172.25.6.5:8000
69 #acl testfie path /test.html
70 #http-request deny if testfile blacklist
71 #redirect location http://www.baidu.com if backlist
72 #acl url_static path_beg -i /images
73 #acl url_static path_end -i .jpg .gif .png .css .js
74 #acl read_request method GET
75 #acl read_request method HEAD
76 #acl write_request method PUT
77 #acl write_request method POST
78 #use_backend static if read_request
79 #use_backend app if write_request
80 default_backend app
81
82 #---------------------------------------------------------------------
83 # static backend for serving up images, stylesheets and such
84 #---------------------------------------------------------------------
85 #backend static
86 # balance roundrobin
87 # server static 172.25.6.6:80 check
88
89 #---------------------------------------------------------------------
90 # round robin balancing between the various backends
91 #---------------------------------------------------------------------
92 backend app
93 balance roundrobin
94 #balance source
95 #balance static-rr
96 server app1 172.25.6.6:80 check <负载均衡lue4>
97 server app2 172.25.6.7:80 check <负载均衡lue5>
98 #server backup 127.0.0.1:8000 backup
systemctl stop httpd
systemctl start haproxy
- 在服务器lue4和lue5中安装apache
在服务器lue4中
yum install -y httpd
systemclt enable --now httpd <开启apache>
cd /var/www/html
echo vm5 > index.html
在服务器lue5中
yum install -y httpd
systemclt enable --now httpd
cd /var/www/html
echo vm6 > index.html
测试:<在lue2中>
curl 172.25.6.5 <ip为lue3>
在服务器lue4中关掉apache
systemctl stop httpd 只能访问到lue5,监控显示lue4被down
curl 172.25.6.5
- 做监控以及用户认证
vim /etc/haproxy/haproxy.cfg
59 stats uri /status <添加监控>
60 stats auth lue:westos <用户名为lue;密码为westos>
61 stats refresh 5s <5秒刷新一次>
systemctl reload haproxy
在浏览器上访问lue3 172.25.6.5/status
- haproxy的日志
vim /etc/rsyslog.conf
14 # Provides UDP syslog reception
15 $ModLoad imudp <取消注释>
16 $UDPServerRun 514 <取消注释>
53 # Don't log private authentication messages!
54 *.info;mail.none;authpriv.none;cron.none;local2.none /var/log/messages <添加local2.none>
72 # Save boot messages also to boot.log
73 local7.* /var/log/boot.log
74 local2.* /var/log/haproxy.log <添加可以查看haproxy的日志>
systemctl restart rsyslog <重新启动>
- 端口映射:修改服务器和调度器的apache端口
在服务器lue5中,修改apache的端口
vi /etc/httpd/conf/httpd.conf
42 Listen 8080
systemctl restart httpd
在调度器lue3中,修改apache的端口
vi /etc/httpd/conf/httpd.conf
42 Listen 8000
systemctl start httpd <apache端口为8000>
服务器lue5:
调度器lue3 :修改Apache的端口(因为haproxy端口占用80端口,所以Apache不能开启,但是修改apache的端口就可以避免两个同时占用80端口),将lue3的Apache端口改为8000,当服务器的httpd都关闭时候,就会访问本机的httpd,告知客户端一些信息。(lue3中要yum install -y httpd)
vi /etc/httpd/conf/httpd.conf 修改80端口为8000
在httpd的发布目录下写入"sorry,try anain later please"
在haproxy.cfg中编辑:
vi /etc/haproxy/haproxy.cfg
96 server app1 172.25.6.6:80 check
97 server app2 172.25.6.7:8080 check <将端口改为8080>
98 server backup 127.0.0.1:8000 backup <将端口改为8000>
服务端没有关闭httpd时访问成功
如果把两个服务器的apache关闭,不会报错
- haproxy实现不同类型不同调用(动静分离)
vi /etc/haproxy/haproxy.cfg
63 # main frontend which proxys to the backends
64 #---------------------------------------------------------------------
65 frontend main *:80
66 acl url_static path_beg -i /images <修改>
67 acl url_static path_end -i .jpg .gif .png .css .js
68
69 use_banckend static if url_static
70 default_backend app
71
72 #---------------------------------------------------------------------
73 # static backend for serving up images, stylesheets and such
74 #---------------------------------------------------------------------
75 backend static
76 balance roundrobin
77 server static 172.25.6.6:80 check <读lue4>
78
79 #---------------------------------------------------------------------
80 # round robin balancing between the various backends
81 #---------------------------------------------------------------------
82 backend app <写lue5>
83 balance roundrobin
84 #balance source
85 #balance static-rr
86 server app1 172.25.6.6:80 check
87 server backup 127.0.0.1:8000 backup
服务器lue5
yum install php
- 读写分离
vi /etc/haproxy/haproxy.cfg
65 frontend main *:80
66
67 #acl url_static path_beg -i /images
68 #acl url_static path_end -i .jpg .gif .png .css .js
69
acl read_request method GET <读请求>
70 acl read_request method HEAD <读请求>
71 acl write_request method PUT <写请求>
72 acl write_request method POST <写请求>
73 use_backend static if read_request
74 use_backend app if write_requesti
75 default_backend app
76
77 #---------------------------------------------------------------------
78 # static backend for serving up images, stylesheets and such
79 #---------------------------------------------------------------------
80 backend static
81 balance roundrobin
82 server static 172.25.6.6:80 check
83
84 #---------------------------------------------------------------------
85 # round robin balancing between the various backends
86 #---------------------------------------------------------------------
87 backend app
88 balance roundrobin
89 #balance source
90 #balance static-rr
91 server app1 172.25.6.7:8080 check
92 server backup 127.0.0.1:8000 backup
在服务器lue4和lue5中
cd /var/www/html中
新建upload目录,
新建index.php和upload_file.php文件
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>
upload_file.php文件内容
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000))
{
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";
}
?>
上传成功!