1.Haproxy负载均衡
(1)代理服务器server1:安装haproxy
tar zxf haproxy-1.7.3.tar.gz
ls
cd haproxy-1.7.3/
make TARGET=linux2628 ARCH=x86_64 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy #安装
yum list haproxy
yum install -y haproxy.x86_64
rpm -qa | grep haproxy #查看版本
rpm -ql haproxy.x86_64 0:1.5.18-3.el7 #安装haproxy后生成的文件
id haproxy #安装haproxy后生成haproxy用户
(2)代理服务器server1:修改haproxy配置文件并开启服务
vim /etc/haproxy/haproxy.cfg
修改为:
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
bind *:80
default_backend webserver
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend webserver
balance roundrobin
server web1 172.25.71.2:80 check
server web2 172.25.71.3:80 check
systemctl start haproxy.service
systemctl status haproxy.service
(3)后端服务器server2/server3:
yum install -y httpd
systemctl start httpd
vim /var/www/html/index.html
编辑默认发布页面
curl localhost
(4)客户端(物理机)测试:curl 172.25.71.1
轮询调度server2和server3,实现负载均衡
Haproxy的反向代理
1.添加日志
(1)代理服务器server1:查看日志格式,编辑日志服务的配置文件并重启服务,使设置生效
vim /etc/haproxy/haproxy.cfg
查看日志格式
vim /etc/rsyslog.conf
编辑日志服务的配置文件
写入:
$ModLoad imudp
$UDPServerRun 514
local2.* /var/log/haproxy.log
systemctl restart rsyslog
(2)客户端(物理机)测试:
curl 172.25.71.1
(3)代理服务器server1:查看haproxy服务的日志
cd /var/log
ls
(生成haproxy.log文件)
cat haproxy.log
2.添加管理页面
(1)代理服务器server1:配置文件中添加参数,实现web监控并返回状态码
vim /etc/haproxy/haproxy.cfg
写入:
stats uri /admin/stats
stats auth admin:sheena #设置加密访问,管理员名称admin,密码:sheena
stats refresh 3s #监控页面3s刷新一次
monitor-uri /monitoruri
systemctl restart haproxy
日志发送报错信息:
Message from syslogd@localhost at Sep 19 07:38:05 …
haproxy[4445]: backend app has no server available!
注释掉配置文件中的[backend app]模块即可
systemctl restart haproxy
(2)测试-物理机浏览器输入:http://172.25.71.1/admin/stats
进入时需要输入管理员名称和密码,显示监控页面
(3)后端服务器server2:
systemctl stop httpd
查看监控页面
systemctl start httpd
查看监控页面
(4)测试-物理机浏览器输入:http://172.25.71.1/monitoruri
会返回状态码
3.设置黑白名单,实现访问控制
(1)代理服务器server1:拒绝客户端(物理机)访问haproxy服务器
vim /etc/haproxy/haproxy.cfg
写入:
frontend main
bind *:80
acl blacklist src 172.25.71.250 #将172.25.71.250加入到黑名单
http-request deny if blacklist
errorloc 403 http://172.25.71.1:8080/index.html #若后端服务器均出现问题,则客户端访问代理服务器server1的apache发布页面
default_backend webserver
systemctl restart haproxy
(2)代理服务器server1:安装http服务,设置端口为8080(haproxy已经使用了80端口),并编辑apache默认发布页面
yum install -y httpd
vim /etc/httpd/conf/httpd.conf
Listen 8080
vim /var/www/html/index.html
Error: Permission Denied !!!
systemctl start httpd
systemctl restart httpd
(3)测试-物理机浏览器输入:http://172.25.71.1:8080/
(4)代理服务器server1:拒绝客户端(物理机)访问haproxy服务器
vim /etc/haproxy/haproxy.cfg
写入:
frontend main
bind *:80
acl blacklist src 172.25.71.250
http-request deny if blacklist
#errorloc 403 http://172.25.71.1:8080/index.html
default_backend webserver
systemctl restart haproxy
(5)测试-物理机浏览器输入:http://172.25.71.1
页面显示403 Forbidden
4.客户端访问的动静分离
(1)代理服务器server1:
vim /etc/haproxy/haproxy.cfg
写入:
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
bind *:80
# acl blacklist src 172.25.71.250
# http-request deny if blacklist
#errorloc 403 http://172.25.71.1:8080/index.html
default_backend webserver
use_backend dynamic if { path_end .php }
default_backend static
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend dynamic #动态资源访问后端服务器server2
balance roundrobin
server web1 172.25.71.2:80 check
backend static #静态资源访问后端服务器server3
balance roundrobin
server web2 172.25.71.3:80 check
systemctl restart haproxy
systemctl status haproxy
(2)后端服务器server2/server3:
yum install -y php
vim /var/www/html/index.php
写入:
<?php
phpinfo()
?>
systemctl restart httpd
(3)测试-物理机浏览器输入:http://172.25.71.1/
(静态资源访问后端服务器server3) http://172.25.71.1/index.php
(动态资源访问后端服务器server2)
5.客户端访问的读写分离
(1)代理服务器server1:
vim /etc/haproxy/haproxy.cfg
写入:
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
bind *:80
acl read method HEAD
acl read method GET
acl write method POST
acl write method PUT
use_backend dynamic if write
use_backend static if read
default_backend static
systemctl restart haproxy
systemctl status haproxy
(2)后端服务器server2/server3:
yum install php -y
#已安装
systemctl restart httpd
#开启http服务
mkdir /var/www/html/upload
#建立一个目录用来写入文件
chmod 777 /var/www/html/upload
cd /var/www/html/
vim index.php
配置页面的读取设置
写入:
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/from-data">
<label for="file">Filename(server2):</label> #加上主机名来区分读的页面来自哪个服务器的
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" id="Submit" />
</form>
</body>
</html>
vim upload_file.php
#设置存储路径
写入:
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
|| ($_FILES["file"]["type"] == "image/jpj"))
&& ($_FILES["file"]["size"] < 20000000)) #文件大小设置
{
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";
}
?>
systemctl restart httpd