HAProxy的安装及配置------实现负载均衡、配置日志、访问控制、动静分离、读写分离
HAProxy的安装及配置-----实现负载均衡、配置日志、访问控制、动静分离、读写分离
- HAProxy简介
- HAProxy的安装
- HAProxy实现负载均衡的部署
- 设置采集日志目录
- 访问控制
- 后端服务器动静分离
- 读写分离
一、HAProxy简介
- HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。
- HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
- HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。
二、HAProxy的安装
系统环境:RedHat6.5系统
实验环境:
- haproxy服务器 :server1: 172.25.60.1/24
- 后端服务器:
server2: 172.25.60.2/24
server3 : 172.25.60.3/24
- 物理主机:172.25.60.250/24
安装过程如下:
1.解压安装包,进入解压后的目录中,只有找到.spec文件时,才能进行rpm编译安装
[[email protected] ~]# tar zxf haproxy-1.7.3.tar.gz
[[email protected] ~]# ls
anaconda-ks.cfg haproxy-1.7.3.tar.gz install.log.syslog haproxy-1.7.3 install.log
[[email protected] ~]# cd haproxy-1.7.3
[[email protected] haproxy-1.7.3]# find . -name *.spec
./examples/haproxy.spec
注意:找到haproxy.spec,可以进行下一步,编译rpm包
2.编译rpm包
- 安装rpm编译软件
yum install rpm-bulid -y
- 安装依赖
yum install pcre-devel gcc -y
3.进入压缩文件的目录中,编译安装包
[[email protected] ~]# rpmbuild --tb haproxy-1.7.3.tar.gz
4.进入rpmbuild/RPMS/安装haproxy
[[email protected] x86_64]# rpm -ivh haproxy-1.7.3-1.x86_64.rpm
Preparing... ########################################### [100%]
1:haproxy ########################################### [100%]
5.查看安装文件
[[email protected] x86_64]# rpm -qpl haproxy-1.6.11-1.x86_64.rpm
/etc/haproxy
/etc/rc.d/init.d/haproxy
/usr/sbin/haproxy
/usr/share/doc/haproxy-1.6.11
/usr/share/doc/haproxy-1.6.11/CHANGELOG
/usr/share/doc/haproxy-1.6.11/README
/usr/share/doc/haproxy-1.6.11/architecture.txt
/usr/share/doc/haproxy-1.6.11/configuration.txt
/usr/share/doc/haproxy-1.6.11/intro.txt
/usr/share/doc/haproxy-1.6.11/management.txt
/usr/share/doc/haproxy-1.6.11/proxy-protocol.txt
/usr/share/man/man1/haproxy.1.gz
三、HAProxy实现负载均衡的部署
server1:
1.将/root/haproxy-1.7.3/examples/content-sw-sample.cfg配置文件复制到/etc/haproxy/目录下,重命名为haproxy.cfg.
[[email protected] examples]# cp content-sw-sample.cfg /etc/haproxy/haproxy.cfg
注意:因为启动脚本访问文件为etc/haproxy/haproxy.cfg,所以我们起名要一致
2.编辑/etc/haproxy/haproxy.cfg
global
maxconn 10000 //最大连接数
stats socket /var/run/haproxy.stat mode 600 level admin
log 127.0.0.1 local0 //日志
uid 200 //默认用户uid
gid 200 //默认用户gid
chroot /var/empty //安全设置,根目录切换,锁在/var/empty下
daemondefaults
mode http
log global
option httplog
option dontlognull
monitor-uri /monitoruri
maxconn 8000 //最大连接数,这个会覆盖前面的10000
timeout client 30s
stats uri /admin/stats
option prefer-last-server
retries 2
option redispatch
timeout connect 5s
timeout server 5s# The public 'www' address in the DMZ
frontend public //前端访问配置
bind *:80 name clear //允许访问本机所有的ip
#bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem#use_backend static if { hdr_beg(host) -i img }
#use_backend static if { path_beg /img /css }
default_backend dynamic //默认访问dynamic# The static backend backend for 'Host: img', /img and /css.
backend static
balance roundrobin
option httpchk HEAD /favicon.ico
server statsrv1 192.168.1.8:80 check inter 1000
server statsrv2 192.168.1.9:80 check inter 1000# the application servers go here
backend dynamic
balance roundrobin
server dynsrv1 172.25.60.2:80 check inter 1000 //realserver1,默认1000毫秒检查一次
server dynsrv2 172.25.60.3:80 check inter 1000
3.创建haproxy用户
[[email protected] examples]# groupadd -g 200 haproxy
[[email protected] examples]# useradd -u 200 -g 200 -s /sbin/nologin -M haproxy-M:不创建家目录
server2:
开启httpd服务,并编辑默认发布页面
server3:
开启httpd服务,并编辑默认发布页面
测试:开启haproxy服务,在物理机上访问172.25.60.1haproxy服务器,会轮询访问后端的server2和server3服务器
[[email protected] haproxy]# /etc/init.d/haproxy start //开启服务
Starting haproxy: [ OK ]
注意:此页面表示haproxy服务器完好
注意:监控页面,不同颜色代表不同状态
4.编辑配置文件,使监控页面定时刷新
四、设置采集日志目录
1.编辑配置采集规则文件/etc/rsyslog.conf
13 $ModLoad imudp
14 $UDPServerRun 514
63 local0.* var/log/haproxy.log
类型 动作
2.重启rsyslog这个服务
[[email protected] ~]# /etc/init.d/rsyslog restart //重启服务
Shutting down system logger: [ OK ]
Starting system logger:
[ OK ]
3.cat /var/log/haproxy.log可以查看到日志
四、访问控制及访问重定向
- 访问控制配置
1.编辑配置文件 /etc/haproxy/haproxy.cfg
frontend public
bind *:80 name clear
#bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
#use_backend static if { hdr_beg(host) -i img }acl blacklist src 172.25.60.250 //访问控制
http-request deny if blacklist //对172.25.60.250的访问禁止use_backend static2 if { path_end -i .php }
default_backend static1
2.重新加载haproxy
[[email protected] ~]# /etc/init.d/haproxy reload
测试:在172.25.60.1上可以访问haproxy,但在172.25.60.250这台物理机上访问被拒绝
- 错误访问重定向
1.编辑配置文件 /etc/haproxy/haproxy.cfg
frontend public
bind *:80 name clear
#bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
#use_backend static if { hdr_beg(host) -i img }
acl blacklist src 172.25.60.250 //访问控制
http-request deny if blacklist //对172.25.60.250的访问禁止errorloc 403 http://172.25.60.1:8080/index.html ##当访问出现403错误时,将访问重定向到172.25.60.1:8080/index.html
use_backend static2 if { path_end -i .php }
default_backend static1
2.在server1上开启httpd服务,并将监听端口改为8080,并配置httpd的默认发布页面
注意:因为server1上80端口被haproxy占用,所以,更改httpd的端口为8080
编辑/var/www/html/index.html文件
2.重新加载haproxy,开启httpd服务
[[email protected] ~]# /etc/init.d/haproxy reload
[[email protected] haproxy]# /etc/init.d/httpd start
测试:在172.25.60.250这台物理机上访问172.25.60.1时,将访问重定向到172.25.60.1:8080/index.html
- 直接访问重定向
1.编辑配置文件 /etc/haproxy/haproxy.cfg
#acl blacklist src 172.25.60.250
#http-request deny if blacklist#errorloc 403 http://172.25.60.1:8080/index.html
redirect location http://172.25.60.3 ##将访问重定向到172.25.60.3
测试:
五、后端服务器动静分离
动静分离是指,静态页面与动态页面分开不同系统访问的架构设计方法。
动态:php,jsp页面
1.在server1上配置动静分离
47 use_backend dynamic if { path_end .php }
48 default_backend static
49 ##默认访问static,当访问.php结尾的文件时,访问动态服务器
50 # The static backend backend for 'Host: img', /img and /css.
51 backend static
52 balance roundrobin
53 server statsrv1 172.25.60.2:80 check inter 1000
54
55 # the application servers go here
56 backend dynamic
57 balance roundrobin
58 server dynsrv1 172.25.60.3:80 check inter 1000
2.在sever3上安装php,并编辑php测试页
[[email protected] ~]# yum install php -y
[[email protected] html]# cat /var/www/html/index.php
<?php
phpinfo()
?>
3.重启server1的haproxy和server3的httpd服务
[[email protected] html]# /etc/init.d/haproxy restart
[[email protected] html]# /etc/init.d/httpd restart
测试:当访问静态页面时,访问172.25.60.2的http默认发布页
当访问以.php结尾的页面时,访问172.25.60.3的php测试页
六、读写分离
为了确保数据库产品的稳定性,很多数据库拥有双机热备功能。也就是,第一台数据库服务器,是对外提供增删改业务的生产服务器;第二台数据库服务器,主要进行读的操作。·
1.在server2和server3上配置后端动态上传网页
[[email protected] Desktop]# scp -rp upload/ [email protected]:/var/www/html/在srver2上将upload中的文件移动到/var/www/html中,更改upload的权限为777
在server3进行相同的配置
2.在server1上编辑配置文件
acl blacklist src 172.25.27.7
acl write method POST
acl write method PUT ##写控制
use_backend static if write
default_backend dynamic
3.修改server2和server3上传文件的限定大小
测试:重启服务,在网页访问172.25.60.1
上传文件 Brower ---> Submit
在server2的upload中可以找到所上传的文件