HAProxy-----负载均衡,添加日志,访问控制,读写分离,动静分离

实验环境

主机(IP)服务
server1(172.25.24.1)haproxy
server2(172.25.24.2)apache
server3(172.25.24.3)apache
真机测试

HAProxy基础知识介绍

  • HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。
  • HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
  • HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。

Apache+HAProxy负载均衡的实现

server2和server3
  • 安装apache
  • 打开服务
  • 写测试页
server1
  • 安装服务yum install haproxy
  • 修改配置文件,开启服务
[root@server1 haproxy]# vim haproxy.cfg 

 63 frontend  main
 64         bind    *:80   #绑定 80端口
 65     default_backend            webserver    默认的后端
 66 
 67 #---------------------------------------------------------------------
 68 # static backend for serving up images, stylesheets and such
 69 #---------------------------------------------------------------------
 70 backend webserver
 71     balance     roundrobin   轮询  负载均衡
 72     server      web1 172.25.24.2:80 check
 73     server      web2 172.25.24.3:80 check
 
[root@server1 haproxy]# systemctl start haproxy.service
真机测试
  • 真机curl 172.25.24.1

添加日志

server1
  • 修改日志定向采集的配置文件
[root@server1 haproxy]# vim /etc/rsyslog.conf 

 15 $ModLoad imudp   取消注释
 16 $UDPServerRun 514
 74 local2.*                                             /var/log/haproxy.log   #定义日志采集的地点 日志级别和格式
 
 [root@server1 haproxy]# systemctl restart rsyslog.service 
真机测试
  • 真机测试curl 172.25.24.1
  • 在server1查看vim /var/log/haproxy.log

访问控制

监控
server1
  • 配置文件中添加以下,重启服务
[root@server1 haproxy]# vim haproxy.cfg 

 59     stats uri               /admin/stats
 60     monitor-uri             /monitoruri

[root@server1 haproxy]# systemctl restart haproxy.service 
测试
  • 真机浏览器访问http://172.25.24.1/admin/stats,测试,刷新。
    在这里插入图片描述
server2
  • 关闭httpd服务:[root@server2 ~]# systemctl stop httpd
真机
  • 真机访问,可以看到web1的状态发生改变(变红)。

在这里插入图片描述

  • 此时,在浏览器中访问http://172.25.24.1/monitoruri,显示200ok
    在这里插入图片描述
设置刷新时间以及登陆的用户名和密码
server1
[root@server1 haproxy]# vim haproxy.cfg 

 61     stats auth              admin:westos   用户名 admin 密码 westos
 62     stats refresh            5s   每5刷新一次 默认30s

[root@server1 haproxy]# systemctl restart haproxy.service 
真机
  • 真机浏览器访问
    在这里插入图片描述
  • 在server1查看日志文件vim /var/log/haproxy.log
    在这里插入图片描述
设置黑名单
server1
  • 配置文件中添加以下,这个ip访问时为403 forbidden界面:
[root@server1 haproxy]# vim haproxy.cfg 

 69     acl blacklist src 172.25.24.24
 70     http-request deny if blacklist

[root@server1 haproxy]# systemctl restart haproxy.service 

在这里插入图片描述

错误重定向(403)
  • 我们可以将用户的访问定向特定的端口和界面:
    将此用户的访问定向到apache默认发布页,端口修改为8080,给用户写上提示:
    给server1安装apache:
[root@server1 haproxy]# yum install httpd -y
[root@server1 haproxy]# vim /var/www/html/index.html
[root@server1 haproxy]# cat /var/www/html/index.html 
维护中...
[root@server1 haproxy]# vim /etc/httpd/conf/httpd.conf
 41 #Listen 12.34.56.78:80
 42 Listen 8080   修改8080端口
[root@server1 haproxy]# systemctl start httpd
  • 修改配置文件,重启服务。
[root@server1 haproxy]# vim haproxy.cfg
 69     acl blacklist src 172.25.24.24
 70     http-request deny if blacklist
 71     errorloc 403 http://172.25.24.1:8080/index.html
[root@server1 haproxy]# systemctl restart haproxy.service
  • 此时,该用户在浏览器中访问:可以看到自动调转到8080端口,默认的index.html,显示的是维护中
    在这里插入图片描述

动静分离

介绍
  • 实际应用环境中,往往需要根据业务请求将相关不同请求跳转到指定的后端server,比如客户静态资源请求交给静态资源server处理,php请求交给php server处理,jsp请求交给tomcat处理,即业务上的应用请求分离,而haproxy完全可以利用acl匹配规则实现这一目的 。
server1
  • 修改配置文件,重启服务。
[root@server1 haproxy]# vim haproxy.cfg 

 69     #acl blacklist src 172.25.24.24    #注释掉黑名单的设置
 70     #http-request deny if blacklist
 71     #errorloc 403 http://172.25.24.1:8080/index.html
 72     use_backend dynamic if { path_end .php }   #如果请求为.php结尾 就使用动态的后端
 73     default_backend            static   #默认为静态
 74 
 75 
 76 #---------------------------------------------------------------------
 77 # static backend for serving up images, stylesheets and such
 78 #---------------------------------------------------------------------
 79 backend dynamic
 80     balance     roundrobin
 81     server      web1 172.25.24.2:80 check   #动态主机的ip server2
 82 
 83 backend static
 84     balance     roundrobin
 85     server      web2 172.25.24.3:80 check   #静态主机的ip server3
 
[root@server1 haproxy]# systemctl restart haproxy.service 
server2(动)
  • 下载php,写默认的发布页,重启httpd服务。
[root@server2 ~]# yum install php -y
[root@server2 ~]# vim /var/www/html/index.php
[root@server2 ~]# cat /var/www/html/index.php 
<?php
	phpinfo();
?>
[root@server2 ~]# systemctl restart httpd
server3(静)
  • 下载php,写默认的发布页,重启httpd服务。
[root@server3 ~]# vim /var/www/html/index.php
[root@server3 ~]# cat /var/www/html/index.php 
server3php
[root@server3 ~]# systemctl restart httpd
真机
  • 测试,访问
    在这里插入图片描述
    在这里插入图片描述
    静态页面访问的是server3apache的默认发布页面

读写分离

server1
  • 修改配置文件。
[root@server1 haproxy]# vim haproxy.cfg 

 72     acl read method HEAD
 73     acl read method GET    #读的两种方法
 74     acl write method POST
 75     acl write method PUT   #写的两种方法
 76     
 77 
 78 #    use_backend dynamic if { path_end .php }  #注释
 79     use_backend dynamic if write    #如果为写 server2
 80     use_backend static if read    #读 server3
 81 
 82     default_backend            static
 83 #---------------------------------------------------------------------
 84 # static backend for serving up images, stylesheets and such
 85 #---------------------------------------------------------------------
 86 backend dynamic
 87     balance     roundrobin
 88     server      web1 172.25.24.2:80 check
 89 
 90 backend static
 91     balance     roundrobin
 92     server      web2 172.25.24.3:80 check

[root@server1 haproxy]# systemctl restart haproxy.service
server2(写)
  • 修改php的默认发布页面。
[root@server2 html]# cat index.php 
<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename server2:</label>    #server2 index.php文件的内容
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

[root@server2 html]# chmod 777 *
[root@server2 html]# scp -r index.php upload upload_file.php server3:/var/www/html/
server3(读)
  • 修改php的默认发布页面。
[root@server3 ~]# cat /var/www/html/index.php 
<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename server3:</label>    #server3 index.php文件内容
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>
真机
  • 访问,读和写测试。
    在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
上传(写)成功。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值