haproxy
为了提升网站承载的并发量,所以要搭建负载服务器集群!集群要求既要可以有很好的性能,还有能够实现对URL的一些过滤!
那么此时我们可以选择使用HAproxy
HAproxy可以在4层和7层工作,他有着不错的性能同时页可以通过ACL规则实现URL过滤!
配置准备:
CentOS-7.5虚拟机
三台虚拟机:
web1:192.168.136.15
web2:192.168.136.16
haproxy:192.168.136.17
(安装包用网络yum源进行安装)
关闭防火墙
setenfore 0
systemctl stop firewalld
web1:
yum -y install httpd
增加测试页面
echo 111 > /var/www/html/index.html
systemctl start httpd
web2:
yum -y install httpd
增加测试页面
echo 222 > /var/www/html/index.html
systemctl start httpd
haproxy:
安装haproxy
yum -y install haproxy
修改haproxy配置文件
vim /etc/haproxy/haproxy.cfg
(#的不用修改)
#---------------------------------------------------------------------
frontend stats
bind *:8080
stats uri /stats
stats auth admin:admin
stats refresh 2s
#frontend 5200 *:80
#---------------------------------------------------------------------
#main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend webservers *:80
#acl url_static path_beg -i /static /images /javascript /stylesheets
#acl url_static path_end -i .jpg .gif .png .css .js
#use_backend static if url_static
default_backend webservers
#---------------------------------------------------------------------
#static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#backend static
#balance roundrobin
#server static 127.0.0.1:4331 check
#---------------------------------------------------------------------
#round robin balancing between the various backends
#---------------------------------------------------------------------
backend webservers
balance roundrobin
server web1 192.168.136.15:80 check
server web2 192.168.136.16:80 check
开启haproxy服务
systemctl start haproxy
进入haproxy
测试:
关闭一台apached(这里关闭的是web2),haproxy自动检测状态
systemctl stop httpd
报警图如下:
想要恢复,重新开启关闭的apache,systemctl restart httpd
缓冲图如下:
(这里刷新的时间是当时自己在配置文件中写的,因为上面设置的是2秒的刷新时间"stats refresh 2s",所以刷新时间为2秒。)
恢复图如下: