Elastic Stack EFK 日志采集与可视化
Elastic Stack EFK 日志采集与可视化实践
1. 目标架构
本笔记中的示例地址:
| 角色 | 主机名 | 文档示例地址 |
|---|---|---|
| 负载均衡器 | lb-node | 192.0.2.11 |
| 移动端 Web | mobile-node | 192.0.2.12 |
| PC 端 Web | pc-node | 192.0.2.13 |
| Elasticsearch | es-node-01~03 | 192.0.2.11~.13 |
| Kibana | kibana-node | 192.0.2.11 |
实验时可把 Nginx 和 Elastic Stack 部署在同一批节点上,但生产环境应根据负载、故障域和安全边界分别规划。
2. Nginx 自定义 JSON 日志
2.1 为什么使用 JSON
普通 Nginx 文本日志需要额外编写 Grok 或正则表达式;JSON 日志可以直接解析为结构化字段,便于:
- 按状态码、URL、客户端 IP 聚合。
- 计算响应时间、带宽和访问次数。
- 在 Kibana 中直接创建可视化和 Dashboard。
- 避免复杂正则带来的解析失败。
2.2 安装 Nginx
Debian/Ubuntu:
sudo apt update
sudo apt install -y nginx
2.3 配置日志格式
在 /etc/nginx/nginx.conf 的 http 配置块中定义日志格式:
log_format demo_nginx_json escape=json '{'
'"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"send_bytes":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"method":"$request_method",'
'"uri":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"proxy_protocol_addr":"$proxy_protocol_addr",'
'"http_user_agent":"$http_user_agent",'
'"status":$status'
'}';
access_log /var/log/nginx/access.log demo_nginx_json;
要求:
escape=json能对引号、反斜杠等字符进行 JSON 转义。- 数字字段不要额外加引号,例如
send_bytes、responsetime和status。 - 同一个
access_log目标不要同时保留默认文本格式,否则会生成混合格式日志。 - 如果
access_log配置在server块内,应确保使用新的 JSON 格式名称。
2.4 校验并加载配置
sudo nginx -t
sudo systemctl reload nginx
2.5 生成测试请求
while true; do
curl -s -o /dev/null "http://192.0.2.11/"
sleep 0.5
done
查看日志:
sudo tail -n 5 /var/log/nginx/access.log
每条记录应是一行合法 JSON。
3. Nginx Web 集群案例
3.1 端口与域名规划
| 访问入口 | 后端 | 端口 |
|---|---|---|
mobile.example.com:80 | 移动端 Web | 81 |
pc.example.com:80 | PC 端 Web | 82 |
所有节点都需要:
- 已安装 Nginx。
- 已配置相同的 JSON 日志格式。
- 能通过主机名或 Host 请求头区分不同站点。
3.2 移动端 Web 配置
在 mobile-node 的 /etc/nginx/sites-enabled/default 中配置:
server {
listen 81 default_server;
server_name _;
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
测试页面:
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>移动端测试页面</title>
</head>
<body>
<h1>移动端测试页面</h1>
</body>
</html>
保存到 /var/www/html/index.html,然后执行:
sudo nginx -t
sudo systemctl reload nginx
sudo ss -ntlp | grep ':81'
curl -I http://192.0.2.12:81/
3.3 PC 端 Web 配置
在 pc-node 的 /etc/nginx/sites-enabled/default 中配置:
server {
listen 82 default_server;
server_name _;
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
测试页面:
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>PC 端测试页面</title>
</head>
<body>
<h1>PC 端测试页面</h1>
</body>
</html>
校验:
sudo nginx -t
sudo systemctl reload nginx
sudo ss -ntlp | grep ':82'
curl -I http://192.0.2.13:82/
3.4 负载均衡配置
在 lb-node 的 Nginx 配置中增加:
upstream mobile_backend {
server 192.0.2.12:81;
}
upstream pc_backend {
server 192.0.2.13:82;
}
server {
listen 80;
server_name mobile.example.com;
location / {
proxy_pass http://mobile_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 80;
server_name pc.example.com;
location / {
proxy_pass http://pc_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
校验并加载:
sudo nginx -t
sudo systemctl reload nginx
3.5 使用 Host 请求头测试
curl -H 'Host: mobile.example.com' http://192.0.2.11/
curl -H 'Host: pc.example.com' http://192.0.2.11/
若使用浏览器访问,需要为示例域名配置本地 hosts 或 DNS。不要在生产环境中随意修改公共 DNS 记录。
3.6 Web 集群的 Filebeat 规划
每个承载日志的节点都应部署 Filebeat,或者把日志可靠地汇聚到统一采集节点:
lb-node采集负载均衡器访问日志。mobile-node采集移动端访问日志。pc-node采集 PC 端访问日志。
不要让单台 Filebeat 直接读取其他主机上的本地文件路径,因为文件并不在它自己的文件系统中。可通过以下方式汇聚:
- 在每台 Web 节点部署 Filebeat。
- 把日志发送到集中式 Syslog 或消息队列。
- 使用共享存储,但应评估延迟、锁和单点故障风险。
4. Filebeat 采集 Nginx JSON 日志
配置示例:/etc/filebeat/config/07-nginx-json.yml
filebeat.inputs:
- type: log
paths:
- /var/log/nginx/access.log
json.keys_under_root: true
json.overwrite_keys: true
json.add_error_key: true
output.elasticsearch:
hosts:
- "http://192.0.2.11:9200"
- "http://192.0.2.12:9200"
- "http://192.0.2.13:9200"
index: "demo-web-nginx"
setup.ilm.enabled: false
setup.template.name: "demo-web-filebeat"
setup.template.pattern: "demo-web-*"
setup.template.overwrite: false
setup.template.settings:
index.number_of_shards: 3
index.number_of_replicas: 0
说明:
json.keys_under_root: true把解析后的 JSON 字段放到事件顶层。json.overwrite_keys: true允许 JSON 字段覆盖同名事件字段。json.add_error_key: true在解析失败时添加错误字段,便于排查。- 配置中的
@timestamp应能被识别为事件时间。 - 若不需要原始的
message字段,可以使用处理器显式删除;不要假定 JSON 解析一定会自动删除它。
验证:
sudo filebeat test config -c /etc/filebeat/config/07-nginx-json.yml
sudo filebeat test output -c /etc/filebeat/config/07-nginx-json.yml
sudo filebeat -e -c /etc/filebeat/config/07-nginx-json.yml
检查索引:
GET http://192.0.2.11:9200/_cat/indices/demo-web-*?v
GET http://192.0.2.11:9200/demo-web-nginx/_search?pretty&size=3
5. Nginx 字段与映射建议
| 字段 | 建议类型 | 说明 |
|---|---|---|
@timestamp | date | 请求时间 |
host | keyword | Web 服务器地址 |
clientip | ip | 客户端地址 |
send_bytes | long | 响应字节数 |
responsetime | float | 请求处理时间 |
upstreamtime | keyword | 上游响应时间,可能包含逗号或 - |
upstreamhost | keyword | 上游地址 |
http_host | keyword | HTTP Host |
method | keyword | 请求方法 |
uri | keyword | 请求路径 |
domain | keyword | 业务域名或 Host |
xff | text | 代理链地址 |
referer | text | 来源页面 |
proxy_protocol_addr | keyword | Proxy Protocol 地址,可能为空 |
http_user_agent | text | User-Agent |
status | short | HTTP 状态码 |
不要把可能为空或含多个地址的字段直接定义为 ip,否则可能造成写入失败。
6. 部署 Tomcat
6.1 下载并解压
wget "https://dlcdn.apache.org/tomcat/tomcat-10/v${TOMCAT_VERSION}/bin/apache-tomcat-${TOMCAT_VERSION}.tar.gz"
sudo install -d /opt/demo-tomcat
sudo tar xf "apache-tomcat-${TOMCAT_VERSION}.tar.gz" \
-C /opt/demo-tomcat --strip-components=1
6.2 配置 Java 环境
Tomcat 需要兼容版本的 JDK。生产环境应使用独立维护的 JDK,不建议依赖其他软件的私有运行时目录。
sudo tee /etc/profile.d/tomcat-demo.sh >/dev/null <<'EOF'
export JAVA_HOME=/usr/lib/jvm/demo-jdk
export CATALINA_HOME=/opt/demo-tomcat
export PATH="$JAVA_HOME/bin:$CATALINA_HOME/bin:$PATH"
EOF
source /etc/profile.d/tomcat-demo.sh
java -version
6.3 启动与验证
sudo "$CATALINA_HOME/bin/catalina.sh" start
sudo ss -ntlp | grep ':8080'
curl -I http://192.0.2.11:8080/
sudo tail -f "$CATALINA_HOME/logs/catalina.out"
7. Tomcat JSON 访问日志
7.1 配置 AccessLogValve
在 tomcat.example.com 对应的 <Host> 中加入:
<Host name="tomcat.example.com" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs"
prefix="tomcat.example.com_access_log."
suffix=".json"
rotatable="true"
pattern="{"clientip":"%h","client_user":"%l","authenticated":"%u","access_time":"%t","request":"%r","status":"%s","send_bytes":"%b","query_string":"%q","referer":"%{Referer}i","http_user_agent":"%{User-Agent}i"}"/>
</Host>
说明:
- XML 属性中的双引号需要使用
"。 pattern输出的是一行 JSON。- 用户可控内容可能包含引号或反斜杠,生产环境应验证输出是否为合法 JSON;必要时使用具备 JSON 转义能力的访问日志阀。
access_time默认格式不一定是 Elasticsearch 最容易识别的格式,可在解析阶段转换或改用 ISO 8601 时间格式。
7.2 重启并测试
sudo "$CATALINA_HOME/bin/catalina.sh" stop
sudo "$CATALINA_HOME/bin/catalina.sh" start
curl -H 'Host: tomcat.example.com' http://192.0.2.11:8080/
sudo tail -n 3 "$CATALINA_HOME/logs/tomcat.example.com_access_log."*.json
7.3 Filebeat 配置
filebeat.inputs:
- type: log
paths:
- /opt/demo-tomcat/logs/tomcat.example.com_access_log.*.json
json.keys_under_root: true
json.overwrite_keys: true
json.add_error_key: true
output.elasticsearch:
hosts:
- "http://192.0.2.11:9200"
- "http://192.0.2.12:9200"
- "http://192.0.2.13:9200"
index: "demo-tomcat-access"
setup.ilm.enabled: false
setup.template.name: "demo-tomcat-filebeat"
setup.template.pattern: "demo-tomcat-*"
setup.template.overwrite: false
setup.template.settings:
index.number_of_shards: 3
index.number_of_replicas: 0
运行:
sudo filebeat -e -c /etc/filebeat/config/08-tomcat-access.yml
8. Tomcat 多行日志
8.1 适用场景
Java 异常堆栈通常包含多行,例如:
2026-07-12 10:20:30 ERROR Request failed
java.lang.IllegalStateException: example
at demo.Service.run(Service.java:42)
at demo.Main.main(Main.java:10)
如果不处理,堆栈每一行都会成为独立事件,不利于检索和统计。
8.2 按“行首时间”合并
配置示例:/etc/filebeat/config/09-tomcat-multiline.yml
filebeat.inputs:
- type: log
paths:
- /opt/demo-tomcat/logs/catalina.out
multiline:
type: pattern
pattern: '^\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}'
negate: true
match: after
max_lines: 500
timeout: 5s
output.elasticsearch:
hosts:
- "http://192.0.2.11:9200"
- "http://192.0.2.12:9200"
- "http://192.0.2.13:9200"
index: "demo-tomcat-catalina"
setup.ilm.enabled: false
setup.template.name: "demo-tomcat-filebeat"
setup.template.pattern: "demo-tomcat-*"
setup.template.overwrite: false
setup.template.settings:
index.number_of_shards: 3
index.number_of_replicas: 0
含义:
pattern匹配一条新日志的开头。negate: true表示不匹配开头模式的行被视为上一事件的续行。match: after表示把续行追加到前一个事件之后。- 正则应根据实际日志格式验证,不能盲目照搬。
max_lines防止异常日志形成无限大的单条事件。
8.3 按固定行数合并
当日志确实固定每 4 行一组时,可以使用:
multiline:
type: count
count_lines: 4
这种方式不适合行数不固定的异常堆栈,仅用于格式严格固定的数据。
8.4 判断多行配置是否生效
在 Kibana 中查看单条文档,确认堆栈行是否位于同一个 message 字段中。多行配置变更后,旧偏移位置的数据不一定会重新读取;不要在未理解影响的情况下清理 Filebeat 数据目录。
9. 多个 Input 写入不同索引
示例:文件输入写入文件索引,TCP 输入写入 TCP 索引。
filebeat.inputs:
- type: log
paths:
- /tmp/demo-file.log
tags:
- demo-file
- type: tcp
host: "0.0.0.0:9000"
tags:
- demo-tcp
output.elasticsearch:
hosts:
- "http://192.0.2.11:9200"
- "http://192.0.2.12:9200"
- "http://192.0.2.13:9200"
indices:
- index: "demo-log-file-%{+yyyy.MM.dd}"
when.contains:
tags: "demo-file"
- index: "demo-log-tcp-%{+yyyy.MM.dd}"
when.contains:
tags: "demo-tcp"
setup.ilm.enabled: false
setup.template.name: "demo-log-filebeat"
setup.template.pattern: "demo-log-*"
setup.template.overwrite: false
setup.template.settings:
index.number_of_shards: 3
index.number_of_replicas: 0
要点:
tags建议使用列表。- 每个
indices条目只能命中一个明确目标,避免条件重叠。 when.contains根据事件字段选择输出索引。- 索引模式
demo-log-*必须覆盖所有自定义索引。 - 自定义索引名要求禁用 ILM,否则可能被默认策略覆盖。
验证:
sudo filebeat test config -c /etc/filebeat/config/10-multi-input.yml
sudo filebeat test output -c /etc/filebeat/config/10-multi-input.yml
sudo filebeat -e -c /etc/filebeat/config/10-multi-input.yml
printf 'file input test\n' >> /tmp/demo-file.log
printf 'tcp input test\n' | nc 192.0.2.11 9000
10. Filebeat 多实例
10.1 为什么要使用不同数据目录
同一个数据目录包含锁文件、元数据和采集偏移。多个 Filebeat 实例共享同一数据目录可能产生:
- 锁冲突。
- 重复采集。
- 偏移量互相覆盖。
- 文件轮转后状态异常。
10.2 实验环境运行两个实例
sudo install -d -o root -g root /var/lib/filebeat-stdin
sudo install -d -o root -g root /var/lib/filebeat-tcp
sudo filebeat -e \
-c /etc/filebeat/config/02-stdin-es.yml \
--path.data /var/lib/filebeat-stdin
sudo filebeat -e \
-c /etc/filebeat/config/03-tcp-es.yml \
--path.data /var/lib/filebeat-tcp
两个命令应在不同终端前台运行。检查进程:
ps -ef | grep '[f]ilebeat'
sudo ls -la /var/lib/filebeat-stdin
sudo ls -la /var/lib/filebeat-tcp
10.3 systemd 多实例建议
生产环境应为每个实例使用:
- 独立的配置文件。
- 独立的数据目录。
- 独立的 systemd 服务单元。
- 独立的日志查看入口和故障告警。
不要依赖重复传入多个 --path.data 参数,也不要使用 /tmp 等可能被清理的位置保存长期偏移数据。
11. Filebeat filestream
在较新版本中,filestream 是文件采集的推荐输入类型。Elastic Stack 7.17 已支持该类型。
filebeat.inputs:
- type: filestream
id: demo-app-filestream
paths:
- /var/log/demo-app.log
parsers:
- ndjson:
target: ""
add_error_key: true
output.elasticsearch:
hosts:
- "http://192.0.2.11:9200"
- "http://192.0.2.12:9200"
- "http://192.0.2.13:9200"
index: "demo-app-stream"
setup.ilm.enabled: false
setup.template.name: "demo-app-filebeat"
setup.template.pattern: "demo-app-*"
setup.template.overwrite: false
要点:
- 每个
filestream输入必须有稳定且唯一的id。 - 不要在不同实例中复用相同
id和相同数据目录。 - 结构化 JSON 日志可使用
ndjson解析器。 - 从
log迁移到filestream前,应测试偏移、文件轮转和重复采集行为。
12. Filebeat 模块
12.1 查看模块
filebeat modules list
12.2 启用 Nginx 模块
sudo filebeat modules enable nginx
sudo filebeat modules list
sudo vi /etc/filebeat/modules.d/nginx.yml
在模块配置中设置实际日志路径:
- module: nginx
access:
enabled: true
var.paths:
- /var/log/nginx/access.log
error:
enabled: true
var.paths:
- /var/log/nginx/error.log
测试并初始化模块资源:
sudo filebeat test config -c /etc/filebeat/filebeat.yml
sudo filebeat setup --pipelines --modules nginx
sudo filebeat -e -c /etc/filebeat/filebeat.yml
注意:
- 手工日志输入和模块输入不要同时采集同一文件,否则会产生重复数据。
- 模块可能安装 Ingest Pipeline、索引模板和 Dashboard。
- 自定义索引和 ILM 配置可能与模块默认行为冲突。
13. 可选:通过 NFS 共享 Filebeat 配置
NFS 只适合集中管理配置的低复杂度场景。它引入网络依赖和单点故障,生产环境通常优先使用 Git、Ansible、配置管理平台或容器化配置分发。
13.1 服务端准备
仅在配置服务器安装 NFS 服务端:
sudo apt update
sudo apt install -y nfs-kernel-server
sudo install -d -m 0755 /srv/filebeat-config
编辑 /etc/exports:
/srv/filebeat-config 192.0.2.0/24(ro,sync,root_squash,no_subtree_check)
说明:
- 使用只读导出,防止客户端误改。
- 使用
root_squash,不要使用no_root_squash。 - 不应对所有网络地址开放共享。
- 服务端保留配置写入权限,客户端只读取。
加载导出:
sudo exportfs -ra
sudo exportfs -v
sudo systemctl enable --now nfs-kernel-server
13.2 客户端挂载
客户端安装:
sudo apt install -y nfs-common
sudo install -d -m 0755 /etc/filebeat/config
挂载:
sudo mount -t nfs \
-o ro,nosuid,nodev,soft,timeo=100 \
192.0.2.11:/srv/filebeat-config \
/etc/filebeat/config
验证:
mountpoint /etc/filebeat/config
findmnt /etc/filebeat/config
ls -la /etc/filebeat/config
如需持久化,在 /etc/fstab 中加入经过验证的一行:
192.0.2.11:/srv/filebeat-config /etc/filebeat/config nfs ro,nosuid,nodev,soft,timeo=100 0 0
NFS 不可用时 Filebeat 可能无法读取配置。应配置监控、超时和备用本地配置方案。
14. Kibana 可视化分析
14.1 创建索引模式
在 Kibana 中进入:
Stack Management → Index Patterns
较新版本中可能称为 Data Views。
创建:
demo-web-*
时间字段选择 @timestamp。
14.2 常用指标
| 指标 | 实现方式 | 说明 |
|---|---|---|
| PV | 文档数量 Count | 请求次数 |
| UV | clientip 的 Cardinality | 独立客户端数量近似值 |
| 带宽 | send_bytes 的 Sum | 响应总字节数 |
| 平均响应时间 | responsetime 的 Average | 平均请求耗时 |
| P95/P99 响应时间 | Percentiles | 长尾延迟 |
| 状态码分布 | status 的 Terms | 2xx/3xx/4xx/5xx 分析 |
| URI 请求分布 | uri 或 uri.keyword 的 Terms | 热门资源 |
| 域名流量 | domain 的 Terms | 移动端/PC 端等入口 |
| 趋势 | @timestamp 的 Date Histogram | 按时间查看访问量 |
14.3 常见可视化
- 访问量趋势:折线图,X 轴为时间,Y 轴为 Count。
- 状态码分布:饼图或柱状图,对
status聚合。 - 热门 URI:横向柱状图,对
uri.keyword做 Terms 聚合。 - 独立 IP 数:Metric,对
clientip做 Cardinality。 - 流量趋势:面积图,对
send_bytes做 Sum。 - 响应时间:折线图,对
responsetime做 Average、Max 或 Percentiles。
14.4 KQL 查询
查询状态码为 200 且访问 PC 端域名:
status: 200 and domain: "pc.example.com"
其他示例:
status >= 500
method: "POST"
uri: "/api/*"
not http_user_agent: "health-check"
status: 200 and domain: "mobile.example.com"
14.5 创建 Dashboard
- 在 Discover 中确认索引模式、时间范围和字段正确。
- 保存常用筛选为 Search。
- 在 Visualize Library 中创建各指标图表并保存。
- 新建 Dashboard,添加可视化。
- 设置统一时间范围和全局筛选器。
- 保存并验证刷新后的数据。
15. Elasticsearch 文档读写与优化补充
15.1 写入流程
简化流程:
- 协调节点接收写请求。
- 根据索引和
_id路由到目标主分片。 - 主分片写入内存缓冲区和事务日志。
- 主分片把操作复制到副本分片。
refresh后生成新的 Lucene Segment,文档可被搜索。flush后把持久化状态写入磁盘并清理事务日志。
15.2 查询流程
按 ID 查询时可以直接路由到目标分片;搜索请求通常分为两个阶段:
- Query Phase:各分片返回匹配文档的 ID、排序值等。
- Fetch Phase:协调节点从目标分片获取
_source和实际字段。
15.3 常用优化方向
- 使用 Bulk API 批量写入,合理控制单批大小。
- 根据数据量和节点数量规划主分片,避免索引过小、分片过多。
- 非实时检索场景适当增大
refresh_interval。 - 只查询必要字段,避免无意义返回完整
_source。 - 精确匹配、聚合和排序字段使用
keyword。 - 避免深层分页,优先使用
search_after。 - 控制副本数量,在可用性、写入成本和查询能力之间平衡。
- 监控磁盘水位、JVM 内存、线程池、慢日志和 Segment 数量。
- 只读历史索引可考虑 Force Merge,但不要在持续写入索引上频繁执行。
16. 验证与排错
16.1 Nginx
sudo nginx -t
sudo systemctl status nginx
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log
curl -H 'Host: pc.example.com' http://192.0.2.11/
检查:
- 实际访问日志是否只有一种格式。
- JSON 中是否存在转义错误。
- 负载均衡器是否使用正确的 Host 和后端端口。
16.2 Filebeat
sudo filebeat test config -c /etc/filebeat/filebeat.yml
sudo filebeat test output -c /etc/filebeat/filebeat.yml
sudo systemctl status filebeat
sudo journalctl -u filebeat -f
ps -ef | grep '[f]ilebeat'
16.3 Elasticsearch
GET http://192.0.2.11:9200/_cluster/health?pretty
GET http://192.0.2.11:9200/_cat/indices?v
GET http://192.0.2.11:9200/_cat/templates?v
GET http://192.0.2.11:9200/demo-web-nginx/_search?pretty&size=3
16.4 常见问题
| 现象 | 可能原因 | 处理方向 |
|---|---|---|
| Kibana 没有数据 | 时间范围不正确 | 扩大时间范围,检查 @timestamp |
| JSON 字段不存在 | 未启用 JSON 解析 | 检查 json.* 配置和原始日志 |
| 状态码变成文本 | 普通日志格式混合写入 | 清理错误数据源并统一 JSON 格式 |
| 自定义索引未生效 | ILM 覆盖了索引名 | 检查 setup.ilm.enabled |
| 日志重复 | 多实例共用数据目录或重复采集 | 检查进程、输入路径和 --path.data |
| 多行没有合并 | 正则与实际行首不匹配 | 用实际日志验证 pattern |
| 数据进入错误索引 | tags 或 when 条件错误 | 检查事件 tags 和 indices 条件 |
| Filebeat 无文件读取权限 | 运行用户不能读取目标日志 | 使用 ACL 或合适的组权限 |
| NFS 挂载后配置不可用 | 服务端导出、权限或网络异常 | 检查 exportfs -v、findmnt 和客户端日志 |
17. 综合
- Nginx 使用
log_format ... escape=json输出结构化访问日志。 - Filebeat 的 JSON 解析重点是
json.keys_under_root、字段覆盖和错误处理。 - Tomcat 访问日志需要关注 XML 转义和 JSON 内容合法性。
- 多行日志应匹配真实的行首特征,避免把不同事件错误合并。
- 同一 Filebeat 的多个输入可用
tags和when路由到不同索引。 - 多个 Filebeat 实例必须使用不同数据目录。
filestream要求稳定唯一的输入id,更适合较新版本的持续采集场景。- 自定义索引通常需要关闭 ILM,并核对模板匹配范围。
- NFS 共享配置应限制网段、只读导出并使用
root_squash。 - Kibana 常用指标包括 PV、UV、带宽、状态码、URI 和响应时间。
- Elasticsearch 排错应按集群健康、索引、映射、文档和查询逐层检查。