一. HAproxy的介绍
HAproxy是一款提供高可用性、负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。
HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,同时可以保护你的web服务器不被暴露到网络上。 HAProxy实现了一种事件驱动,
单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space)实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作
二. 实现apache的反向代理和负载均衡
1.下载并解压 haproxy-1.7.3.tar.gz
tar zxf haproxy-1.7.3.tar.gz
2.进入到haproxy-1.7.3文件后,查看以下有没有spec文件,有这个文件才可以打成rpm包
find . -name *.spec
3.解决依赖性和安装
yum install -y rpm-build
yum install -y gcc ##c语言调试工具gcc
yum install -y pcre-devel
4.打包成rpm包
rpmbuild -tb haproxy-1.7.3.tar.gz
5.安装haproxy软件
rpm -ivh haproxy-1.7.3-1.x86_64.rpm
6.配置文件的修改
cp /haproxy-1.7.3/examples/content-sw-sample.cfg /etc/haproxy/haproxy.cfg
vim /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 ## haproxy用户的uid
gid 200
chroot /var/empty
daemon
defaults
mode http
log global
option httplog
option dontlognull
monitor-uri /monitoruri
maxconn 8000
timeout client 30s
option prefer-last-server
retries 2
option redispatch
timeout connect 5s
timeout server 5s
stats uri /admin/stats
# The public 'www' address in the DMZ
frontend public
bind *:80 name clear
#bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
stats uri /admin/stats
#use_backend static if { hdr_beg(host) -i img }
#use_backend static if { path_beg /img /css }
default_backend dynamic
# the application servers go here
backend dynamic
balance roundrobin ## 负载均衡算法rr
server web1 172.25.77.2:80 check inter 1000 ##后端服务器server2
server web2 172.25.77.3:80 check inter 1000 ##后端服务器server3
7.重启服务
/etc/init.d/haproxy restart
在server2和server3上安装并开启httpd
yum install -y httpd
vim /var/www/html/index.html
/etc/init.d/httpd start
测试:轮询访问server2和server3。实现apache的反向代理和负载均衡
curl 172.25.77.1
浏览器访问:172.25.77.1/admin/stats(监控页面)
当把server2后端服务器down掉
浏览器访问:172.25.77.1/monitoruri
三. HAproxy日志管理
1.修改日志文件的配置文件
vim /etc/rsyslog.conf
2.重启日志服务
/etc/init.d/rsyslog restart
3.查看日志内容
cat /var/log/haporxy.log
四. HAproxy的访问控制与错误重定向
(一). 访问控制
1.修改配置文件
vim /etc/haproxy/haproxy.cfg
##将测试用的主机ip加入黑名单使得其无法访问
acl blacklist src 172.25.77.250
http-request deny if blacklist
default_backend dynamic
2.重启haproxy
/etc/init.d/haproxy restart
物理机测试时:curl 172.25.77.1 错误显示
或者网页访问172.25.77.1时界面不友好,所以需要做错误重定向
(二)错误重定向
1.配置httpd服务
1)安装httpd服务
yum install httpd -y
2)修改httpd服务端口为8080
vim /etc/httpd/conf/httpd.conf
Listen 8080
3)错误重定向网页显示内容
vim /var/www/html/index.html
网站维护中.....
4)重启httpd服务
/etc/init.d/httpd start
2.修改配置文件并重新启动haproxy
vim /etc/haproxy/haproxy.cfg
##当访问172.25.77.1时显示403,则自动重定向到http://172.25.77.250:8080/index.html
error-loc 403 http://172.25.77.250:8080/index.html
/etc/init.d/haproxy restart
3.重定向测试:
浏览器访问172.25.77.1
五.HAproxy的动静分离
1.配置server2后端服务器
2.server3安装php,并在httpd默认发布目录写index.php(php测试页面)
3.修改配置文件
vim /etc/haproxy/haproxy.cfg
# The public 'www' address in the DMZ
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 }
#use_backend static if { path_beg /img /css }
#errorloc 403 http://172.25.77.1:8080/index.html if blacklist
use_backend dynamic if { path_end .php } ##以php结尾的访问动态页面
default_backend static ##默认访问静态页面
# the application servers go here
backend static ##静态页面
balance roundrobin
server web1 172.25.77.2:80 check inter 1000
backend dynamic ##动态页面
balance roundrobin
server web2 172.25.77.3:80 check inter 1000
4.重启haproxy
/etc/init.d/haproxy restart
测试:
浏览器访问:172.25.77.1
浏览器访问172.25.77.1/index.php
六.haproxy读写分离
1.配置后端服务器server2和server3
2.修改配置文件
vim /etc/haproxy/haproxy.cfg
/etc/init.d/haproxy restart
vi /var/www/html/index.php
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
测试:浏览器访问172.25.77.1
选择添加图片—>提交---->>查看