Web集群部署与EFK日志收集综合实操指南
项目概述
本项目实现了一个基于Nginx的Web集群架构,包含负载均衡器和不同终端的后端服务器,并通过EFK(Elasticsearch-Filebeat-Kibana)堆栈实现日志收集与分析。以下是完整实施方案:
一、架构设计
用户访问 → ELK91(负载均衡器:80) → 根据Host头路由
├─ iphone.oldboyedu.com → ELK92(移动端:81)
└─ pc.oldboyedu.com → ELK93(PC端:82)
二、节点配置详解
1. ELK92节点配置(移动端)
1.1 安装Nginx
apt -y install nginx
1.2 配置JSON格式日志
http {
log_format oldboyedu_nginx_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"SendBytes":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"uri":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"tcp_xff":"$proxy_protocol_addr",'
'"http_user_agent":"$http_user_agent",'
'"status":"$status"}';
access_log /var/log/nginx/access.log oldboyedu_nginx_json;
}
1.3 站点配置(监听81端口)
server {
listen 81 default_server;
root /var/www/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
1.4 部署移动端页面
<!DOCTYPE html>
<html>
<head>
<title>移动端页面</title>
<style>
div img { width: 900px; height: 600px; }
</style>
</head>
<body>
<h1 style="color: red">手机测试页面</h1>
<div><img src="iphone.jpg"></div>
</body>
</html>
2. ELK93节点配置(PC端)
2.1 安装Nginx(同ELK92)
2.2 配置JSON格式日志(同ELK92)
2.3 站点配置(监听82端口)
server {
listen 82 default_server;
root /var/www/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
2.4 部署PC端页面
<!DOCTYPE html>
<html>
<head>
<title>PC端页面</title>
<style>
div img { width: 900px; height: 600px; }
</style>
</head>
<body>
<h1 style="color: green">电脑测试页面</h1>
<div><img src="pc.jpg"></div>
</body>
</html>
3. ELK91负载均衡器配置
3.1 负载均衡配置
upstream iphone {
server 10.0.0.92:81;
}
upstream pc {
server 10.0.0.93:82;
}
server {
listen 80;
server_name iphone.oldboyedu.com;
location / {
proxy_pass http://iphone;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
server {
listen 80;
server_name pc.oldboyedu.com;
location / {
proxy_pass http://pc;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
3.2 测试命令
# 测试PC端
curl -H 'host: pc.oldboyedu.com' 10.0.0.91
# 测试移动端
curl -H 'host: iphone.oldboyedu.com' 10.0.0.91
三、Filebeat日志收集配置
1. Filebeat配置文件(07-nginx-to-es.yaml)
filebeat.inputs:
- type: log
paths:
- /var/log/nginx/access.log
json.keys_under_root: true
json.overwrite_keys: true
fields:
log_type: "nginx_access"
cluster: "web_production"
output.elasticsearch:
hosts: ["http://10.0.0.91:9200", "http://10.0.0.92:9200", "http://10.0.0.93:9200"]
index: "web-nginx-access-%{+yyyy.MM.dd}"
setup.ilm.enabled: false
setup.template:
name: "web-nginx"
pattern: "web-nginx-*"
overwrite: false
settings:
index.number_of_shards: 3
index.number_of_replicas: 1
2. 启动Filebeat
filebeat -e -c config/07-nginx-to-es.yaml
四、Kibana数据分析
1. 创建索引模式
- 进入Kibana → Stack Management → Index Patterns
- 创建模式:
web-nginx-access-*
2. 查询状态码200且访问pc.oldboyedu.com的记录
{
"query": {
"bool": {
"must": [
{ "match": { "status": "200" } },
{ "match": { "http_host": "pc.oldboyedu.com" } }
]
}
}
}
3. 可视化建议
- 状态码分布:饼图展示不同状态码比例
- 流量趋势:时间序列展示请求量变化
- 用户分析:地图展示客户端IP分布
- 性能监控:平均响应时间趋势
五、生产环境增强建议
-
安全加固:
- 为Nginx添加SSL/TLS加密
- 限制管理接口访问IP
- 启用Nginx的rate limiting
-
高可用:
- 部署多台负载均衡器
- 后端服务器健康检查
upstream pc { server 10.0.0.93:82 max_fails=3 fail_timeout=30s; server 10.0.0.94:82 backup; } -
日志优化:
- 添加业务相关字段(如用户ID、会话ID)
- 设置日志旋转策略
# /etc/logrotate.d/nginx /var/log/nginx/*.log { daily rotate 30 compress delaycompress missingok notifempty create 640 nginx adm sharedscripts postrotate systemctl reload nginx endscript } -
监控告警:
- 设置5xx错误告警
- 监控响应时间超过阈值的请求
六、故障排查指南
1. 访问路由不正确
- 检查负载均衡器配置的server_name
- 验证DNS解析或本地hosts文件
- 检查curl请求是否携带正确的Host头
2. 日志未收集
- 验证Filebeat进程状态:
ps aux | grep filebeat - 检查文件权限:
ls -l /var/log/nginx/access.log - 查看Filebeat日志:
journalctl -u filebeat -f
3. Kibana无数据
- 验证Elasticsearch索引是否存在:
curl -XGET "http://10.0.0.91:9200/_cat/indices?v" - 检查索引模式字段映射
总结
本方案实现了:
- 基于主机头的请求路由
- 结构化JSON日志记录
- 分布式日志收集与分析
- 终端自适应的内容服务
通过这套架构,您可以轻松扩展更多后端服务,并利用EFK堆栈获得全面的业务洞察。建议定期审查日志配置和索引策略,确保系统随着业务增长保持最佳性能。
浙公网安备 33010602011771号