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

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

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

主机名 ip 服务
server1 172.25.254.1 haproxy
server2(后端服务) 172.25.254.2 apache
server3(后端服务) 172.25.254.3 apache

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

yum install haproxy -y

2.配置代理策略

[[email protected] ~]# cd /etc/haproxy/
[[email protected] haproxy]# ls
haproxy.cfg
[[email protected] 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,写默认发布页面

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

启动haproxy服务

[[email protected] haproxy]# /etc/init.d/haproxy start

测试:

curl 172.25.254.1

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

[[email protected] 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
 [[email protected] haproxy]# /etc/init.d/haproxy restart

测试:
打开浏览器输入172.25.254.1:8080/status
HAProxy对实现负载均衡及常见配置(日志路径,访问控制,动静分离,读写分离)
HAProxy对实现负载均衡及常见配置(日志路径,访问控制,动静分离,读写分离)
测试1:将server2的httpd停掉

[[email protected] html]# /etc/init.d/httpd stop

curl 172.25.254.1
server3 ##高可用的实现

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

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

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

  • 静动分离的实现

1.编辑配置文件

[[email protected] 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,编写默认发布页

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

HAProxy对实现负载均衡及常见配置(日志路径,访问控制,动静分离,读写分离)
4.在server1上重新加载配置

[[email protected] html]# /etc/init.d/haproxy reload

测试:(默认访问是静态的)
打开浏览器输入172.25.254.1/images,可以看到server3上的图片
HAProxy对实现负载均衡及常见配置(日志路径,访问控制,动静分离,读写分离)
另开窗口输入172.25.254.1,可以看到server2的内容HAProxy对实现负载均衡及常见配置(日志路径,访问控制,动静分离,读写分离)
将server2的httpd关闭,清除浏览器缓存后再次输入地址可以看到backup的内容

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

1.编辑配置文件

[[email protected] 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
[[email protected] haproxy]# /etc/init.d/haproxy reload

浏览器输入172.25.254.1会出现403 Forbidden 报错
HAProxy对实现负载均衡及常见配置(日志路径,访问控制,动静分离,读写分离)
显然将错误页面展示给客户不太好,所以需要将错误页面进行重定向

[[email protected] 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
[[email protected] haproxy]# /etc/init.d/haproxy reload
[[email protected] haproxy]# vim /var/www/html/index.html 
[[email protected] haproxy]# cat /var/www/html/index.html 
~~~~~~~~~~~
[[email protected] haproxy]# /etc/init.d/httpd restart

浏览器输入网址172.25.254.1会发现被定向到172.25.254.1:8000出现编写的内容
HAProxy对实现负载均衡及常见配置(日志路径,访问控制,动静分离,读写分离)
当出现报错时重新定向,不只是403

[[email protected] 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
[[email protected] haproxy]# /etc/init.d/haproxy reload
[[email protected] haproxy]# vim /var/www/html/index.html 
[[email protected] haproxy]# cat /var/www/html/index.html 
!!!!!~~~~~~~~
[[email protected] haproxy]# /etc/init.d/httpd restart

浏览器输入网址172.25.254.1会发现被定向到172.25.254.1:8000出现内容
HAProxy对实现负载均衡及常见配置(日志路径,访问控制,动静分离,读写分离)

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

1.设置配置文件

[[email protected] 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.重新加载服务

[[email protected] 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是本地的)

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

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

[[email protected] haproxy]# /etc/init.d/rsyslog restart
  • 读写分离
    一般图片保存(专有的图片服务器)在文件系统上
    1.配置haproxy的配置文件,(如果是写入操作时访问的是server2)
[[email protected] 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)

[[email protected] html]# yum install php -y
[[email protected] images]# yum install php -y

3.获得php脚本,放在/var/www/html下
HAProxy对实现负载均衡及常见配置(日志路径,访问控制,动静分离,读写分离)

HAProxy对实现负载均衡及常见配置(日志路径,访问控制,动静分离,读写分离)
4.更改php支持的文件上传大小,给目录权限

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

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

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

[[email protected] html]# /etc/init.d/httpd restart
[[email protected] html]# /etc/init.d/httpd restart
[[email protected] haproxy]# /etc/init.d/haproxy reload

测试:
清除浏览器的缓存,输入www.westos.org后看到php页面
HAProxy对实现负载均衡及常见配置(日志路径,访问控制,动静分离,读写分离)
测试1:上传一张图片后可以看到

HAProxy对实现负载均衡及常见配置(日志路径,访问控制,动静分离,读写分离)
注意:因为上传是写属于动态的,我们在后端主机server2可以查看到
HAProxy对实现负载均衡及常见配置(日志路径,访问控制,动静分离,读写分离)测试2:输入172.25.254.1/images/可以看到图片
HAProxy对实现负载均衡及常见配置(日志路径,访问控制,动静分离,读写分离)
注意:因为读是静态的,所以我们可以看到后端server3的文件
HAProxy对实现负载均衡及常见配置(日志路径,访问控制,动静分离,读写分离)