企业级——通过HAProxy实现负载均衡、添加日志、访问控制、动静分离模式、读写分离模式
1.环境配置
建立新的快照并导入修改ip,修改主机名,配置yum源等
在server1(172.25.66.1)上配置haproxy httpd 代理服务器
在server2(172.25.66.2)上httpd php 作为后端服务器1
在server3(172.25.66.3)上配置httpd php 作为后端服务器2
2.搭建最基本的haproxy服务器
server1:
(1)安装haproxy服务:
检查yum源,安装haproxy:
yum clean all
yum repolist
yum install haproxy -y
(2)修改配置文件:
cd /etc/haproxy
ls
vim haproxy.cfg
defaults
mode http #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
log global #应用全局的日志配置
option httplog # 启用日志记录HTTP请求,默认haproxy日志记录是不记录HTTP请求日志
option dontlognull # 启用该项,日志中将不会记录空连接。所谓空连接就是在上游的负载均衡器 或者监控系统为了探测该服务是否存活可用时,需要定期的连接或者获取某固定的组件或页面,或者探测扫描端口是否在监听或开放等动作被称为空连接;官方文档中标注,如果该服务上游没有其他的负载均衡器的话,建议不要使用该参数,因为互联网上的恶意扫描或其他动作就不会被记录下来
option http-server-close #每次请求完毕后主动关闭http通道
option forwardfor except 127.0.0.0/8
option redispatch
当使用了cookie时,haproxy将会将其请求的后端服务器的serverID插入到cookie中,以保证会话的SESSION持久性;而此时,如果后端的服务器宕掉了, 但是客户端的cookie是不会刷新的,如果设置此参数,将会将客户的请
求强制定向到另外一个后端server上,以保证服务的正常。
retries 3 # 定义连接后端服务器的失败重连次数,连接失败次数超过此值后将会将对应后端
服务器标记为不可用
timeout http-request 10s #http请求超时时间
timeout queue 1m #一个请求在队列里的超时时间
timeout connect 10s #连接超时
timeout client 1m #客户端超时
timeout server 1m #服务器端超时
timeout http-keep-alive 10s #设置http-keep-alive的超时时间
timeout check 10s #检测超时
maxconn 3000 #每个进程可用的最大连接数
注释无用的信息:
添加新的配置:
添加后端服务器web1 web2:
listen admin *:8080
stats enable
stats uri /status # 监控页面地址
stats auth admin:westos # 管理帐号和密码
stats refresh 5s #刷新频率
listen westos *:80 #监听的实例名称,地址和端口
balance roundrobin #负载均衡算法
server web1 172.25.66.2:80 check
server web2 172.25.66.3:80 check
listen:可以理解为frontend和backend的组合体
(3)开启服务并检查端口
systemctl start haproxy
netstat -antlp
server2和server3:
(1)安装httpd服务:
检查yum源,安装httpd:
yum clean all
yum repolist
yum install httpd -y
(2)开启服务检查状态:
systemctl start httpd
systemctl status httpd
(3)编写默认发布页面:
vim /var/www/html/index.html
server2(3)
(4)重启服务:
systemctl restart httpd
测试:
(1)负载均衡:
curl 172.25.66.1 #此时会发现轮询访问,实现负载均衡
(2)网页浏览可以看到服务器工作情况
用admin账户登陆,密码为真机密码:
(3)停止后端的web1并重启HAProxy服务:
systemctl stop httpd
systemctl status httpd
systemctl restart haproxy
再次访问:
curl 172.25.66.1 #此时会发现至访问到了server3
网页再次浏览:
(4)重新打开server2服务:
systemctl start httpd
systemctl status httpd
再次访问:
curl 172.25.66.1 #此时会发现轮询访问,再次实现负载均衡
网页再次浏览:
3.添加haproxy日志
server1:
(1)编辑文件打开日志监控模块:
vim /etc/rsyslog.conf
$ModLoad imudp #接受 haproxy 日志
$UDPServerRun 514
*.info;mail.none;authpriv.none;cron.none; /var/log/messages
local2.* /var/log/haproxy.log #日志文件位置
(2)重启服务查看日志:
systemctl restart haproxy
cat /var/log/haproxy.log
4.实现动静分离模式
server1:
(1)修改配置文件:
vim /etc/haproxy/haproxy.cfg
动态静态访问分离:(可以自己加虚拟主机)
#frontend:用来匹配接收客户所请求的域名,uri等,并针对不同的匹配,做不同的请求处理
frontend westos *:80
acl url_static path_beg -i /images
acl url_static path_end -i .jpg .gif .png
use_backend static if url_static
default_backend app (默认访问app)
#backend:定义后端服务器集群
backend static (静态)
#balance roundrobin
server web2 172.25.66.3:80 check
backend app
#balance roundrobin
server web1 172.25.66.2:80 check
server local 172.25.66.1:8000 backup (如果后端real server down掉 则调度报错页面 但是正常情况下不会访问 因为 是“backup”)
(2)重启服务:
systemctl restart haproxy
(2)安装httpd服务:
yum install httpd -y
(3)修改httpd默认端口为之前设置的8000:
Vim /etc/httpd/conf/httpf.conf
systemctl start httpd #开启服务
编写默认发布页面;
vim /var/www/html/index.html
server1
重启服务:
systemctl restart httpd
server3:
(1)建立图片存放目录:
cd /var/www/html/
mkdir images
cd images #进入目录后将图片下载至此目录
ls
(2)重启服务
systemctl restart httpd
测试:
访问server1主机看到server2页面:因为之前设置默认为web1(server2) 备份为本机(server1):
若动态访问172.25.66.1/images则跳转:
关闭web1(server2)httpd服务:
systemctl stop httpd
在server1再次重启haproxy服务,在浏览器则看到server1页面:
systemctl restart haproxy