HAProxy对实现负载均衡及常见配置(日志路径,访问控制,动静分离,读写分离)

一、知识点:
HAProxy是一款提供高可用性、负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于哪些负载特别大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在时下的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,同事可以保护你的web服务器不被暴露到网络上。

主机环境:rhel6.5 selinux and iptables disabled

主机名ip服务
server1172.25.254.1haproxy
server2(后端服务)172.25.254.2apache
server3(后端服务)172.25.254.3apache

二、负载均衡的实现(web页面的动态监控)
搭建步骤:
1.在server1上安装harpoxy(yum源必须高级)

yum install haproxy -y

2.配置代理策略

[root@server1 ~]# cd /etc/haproxy/
[root@server1 haproxy]# ls
haproxy.cfg
[root@server1 haproxy]# vim haproxy.cfg(将从5000开始到最后注释)
 87 listen westos *:80		#监听本机所有的80端口
 88         balance roundrobin		#轮询算法
 89         server web1 172.25.254.2:80 check	#后端负载
 90         server web2 172.25.254.3:80 check

3.在后端安装httpd,写默认发布页面

[root@server2 ~]# yum install httpd -y
[root@server2 ~]# cd /var/www/html/
[root@server2 html]# vim index.html
[root@server2 html]# cat index.html 
server2
[root@server2 html]# /etc/init.d/httpd start
[root@server3 ~]# yum install httpd -y
[root@server3 ~]# cd /var/www/html/
[root@server3 html]# vim index.html
[root@server3 html]# cat index.html 
server3
[root@server3 html]# /etc/init.d/httpd start

启动haproxy服务

[root@server1 haproxy]# /etc/init.d/haproxy start

测试:

curl 172.25.254.1

5.配置web动态监控页面配置文件

[root@server1 haproxy]# vim haproxy.cfg
 86 listen admin *:8080
 87         stats enable
 88         stats uri /status	#监控页面的目录
 89         stats auth admin:westos	#设置管理员及密码
 90         stats refresh 5s	#每隔5s刷新页面
 91 
 92 listen westos *:80
 93         balance roundrobin
 94         server web1 172.25.254.2:80 check
 95         server web2 172.25.254.3:80 check
 [root@server1 haproxy]# /etc/init.d/haproxy restart

测试:
打开浏览器输入172.25.254.1:8080/status
在这里插入图片描述
在这里插入图片描述
测试1:将server2的httpd停掉

[root@server2 html]# /etc/init.d/httpd stop

curl 172.25.254.1
server3 ##高可用的实现

web动态页面下可以看见web1变红

在这里插入图片描述

三、基于TCP和HTTP应用程序代理

  • 静动分离的实现

1.编辑配置文件

[root@server1 haproxy]# vim haproxy.cfg
 86 listen admin *:8080
 87         stats enable
 88         stats uri /status
 89         stats auth admin:westos
 90         stats refresh 5s
 91 
 92 frontend westos *:80		#定义模块
 93         acl url_static       path_beg       -i /images		#url访问的时候以images开头
 94         acl url_static       path_end       -i .jpg .gif .png 	#以.ipg或者.gif或者.png结尾的
 95 
 96         use_backend static      if url_static	#如果是静态
 97         default_backend         app				#默认动态
 98 
 99 backend static
100         server web2 172.25.254.3:80 check
101 
102 backend app
103 #       balance roundrobin
104         server web1 172.25.254.2:80 check
105         server local 172.25.254.1:8000 backup

2.在server1上安装httpd,编写默认发布页

[root@server1 haproxy]# yum install httpd -y
[root@server1 haproxy]# vim /etc/httpd/conf/httpd.conf
136 Listen 8000
[root@server1 haproxy]# cd /var/www/html/
[root@server1 html]# vim index.html
[root@server1 html]# cat index.html 
server1
[root@server1 html]# /etc/init.d/httpd start
[root@server1 html]# netstat -antlpe| grep httpd

在这里插入图片描述
4.在server1上重新加载配置

[root@server1 html]# /etc/init.d/haproxy reload

测试:(默认访问是静态的)
打开浏览器输入172.25.254.1/images,可以看到server3上的图片
在这里插入图片描述
另开窗口输入172.25.254.1,可以看到server2的内容在这里插入图片描述
将server2的httpd关闭,清除浏览器缓存后再次输入地址可以看到backup的内容

  • 访问控制的实现(url重定向)
    302:重定向(临时)旧地址依旧在,但网站希望你访问新的(盗链)
    301:重定向(永久)旧地址永久移除

1.编辑配置文件

[root@server1 haproxy]# vim haproxy.cfg
 86 listen admin *:8080
 87         stats enable
 88         stats uri /status
 89         stats auth admin:westos
 90         stats refresh 5s
 91 
 92 frontend westos *:80
 93         acl url_static       path_beg       -i /images
 94         acl url_static       path_end       -i .jpg .gif .png
 95 
 96         acl badhost src 172.25.254.73
 97         block if badhost
 98 
 99         use_backend static      if url_static
100         default_backend         app
101 
102 backend static
103         server web2 172.25.254.3:80 check
104 
105 backend app
106 #       balance roundrobin
107         server web1 172.25.254.2:80 check
[root@server1 haproxy]# /etc/init.d/haproxy reload

浏览器输入172.25.254.1会出现403 Forbidden 报错
在这里插入图片描述
显然将错误页面展示给客户不太好,所以需要将错误页面进行重定向

[root@server1 haproxy]# vim haproxy.cfg
 96         acl badhost src 172.25.254.5
 97         block if badhost
 98         errorloc 403 http://172.25.254.1:8000
[root@server1 haproxy]# /etc/init.d/haproxy reload
[root@server1 haproxy]# vim /var/www/html/index.html 
[root@server1 haproxy]# cat /var/www/html/index.html 
~~~~~~~~~~~
[root@server1 haproxy]# /etc/init.d/httpd restart

浏览器输入网址172.25.254.1会发现被定向到172.25.254.1:8000出现编写的内容
在这里插入图片描述
当出现报错时重新定向,不只是403

[root@server1 haproxy]# vim haproxy.cfg
 96         acl badhost src 172.25.254.73
 97         #block if badhost
 98         #errorloc 403 http://172.25.254.1:8000
 99         redirect location http://172.25.254.1:8000 if badhost
[root@server1 haproxy]# /etc/init.d/haproxy reload
[root@server1 haproxy]# vim /var/www/html/index.html 
[root@server1 haproxy]# cat /var/www/html/index.html 
!!!!!~~~~~~~~
[root@server1 haproxy]# /etc/init.d/httpd restart

浏览器输入网址172.25.254.1会发现被定向到172.25.254.1:8000出现内容
在这里插入图片描述

301 永久重定向的实现(类似于淘宝的,访问taobao.com也会被重新定向到www.taobao.com)

1.设置配置文件

[root@server1 haproxy]# vim haproxy.cfg
 96         acl badhost src 172.25.254.73
 97         acl westos.org hdr_beg(host) -i westos.org
 98         acl 172.25.254.1 hdr(host) -i 172.25.254.1
 99         redirect code 301 location http://www.westos.org if westos.org
100         redirect code 301 location http://www.westos.org if 172.25.254.1

2.重新加载服务

[root@server1 haproxy]# /etc/init.d/haproxy reload

3.在测试端(物理机)写入解析文件

vim /etc/hosts
172.25.254.1 www.westos.org westos.org

打开浏览器,此时发现输入www.westos.org或者westos.org或者172.25.8.3都会被重定向到www.westos.org

  • 日志路径

1.修改日志配置文件(local2在haproxy的配置文件里看到的,1-7是本地的)

[root@server1 haproxy]# vim /etc/rsyslog.conf
 13 $ModLoad imudp
 14 $UDPServerRun 514
 43 local2.*                                                /var/log/haproxy.log

2.重启日志服务,查看生成文件

[root@server1 haproxy]# /etc/init.d/rsyslog restart
  • 读写分离
    一般图片保存(专有的图片服务器)在文件系统上
    1.配置haproxy的配置文件,(如果是写入操作时访问的是server2)
[root@server1 haproxy]# vim haproxy.cfg
 92 frontend westos *:80
 93         acl url_static       path_beg       -i /images
 94         acl url_static       path_end       -i .jpg .gif .png
 95         acl read method GET
 96         acl read method HEAD
 97         acl write method PUT
 98         acl write method POST
 99         
100         acl badhost src 172.25.254.5
101         acl westos.org hdr_beg(host) -i westos.org
102         acl 172.25.254.1 hdr(host) -i 172.25.254.1
103         
104         use_backend app         if write
105         default_backend         static
106 
107 backend static
108         server web2 172.25.254.3:80 check
109 
110 backend app
111 #       balance roundrobin
112         server web1 172.25.254.2:80 check

2.在后端服务器安装php(server2、server3)

[root@server2 html]# yum install php -y
[root@server3 images]# yum install php -y

3.获得php脚本,放在/var/www/html下
在这里插入图片描述

在这里插入图片描述
4.更改php支持的文件上传大小,给目录权限

[root@server2 html]# vim upload_file.php
 5 && ($_FILES["file"]["size"] < 2000000))
[root@server2 html]# chmod 777 upload

[root@server3 html]# vim upload_file.php
5 && ($_FILES["file"]["size"] < 2000000))
[root@server3 html]# chmod 777 upload

5.重启后端的httpd服务,将代理服务器的haproxy配置重新加载

[root@server2 html]# /etc/init.d/httpd restart
[root@server3 html]# /etc/init.d/httpd restart
[root@server1 haproxy]# /etc/init.d/haproxy reload

测试:
清除浏览器的缓存,输入www.westos.org后看到php页面
在这里插入图片描述
测试1:上传一张图片后可以看到

在这里插入图片描述
注意:因为上传是写属于动态的,我们在后端主机server2可以查看到
在这里插入图片描述测试2:输入172.25.254.1/images/可以看到图片
在这里插入图片描述
注意:因为读是静态的,所以我们可以看到后端server3的文件
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值