Filebeat 把数据输出到 Elasticsearch(ES) 是最常见用法之一。下面按 从简单到进阶 给你一个完整说明,包含配置示例和常见注意点。
Filebeat 的工作流程是:
日志文件 → Filebeat → Elasticsearch
Filebeat 不需要 Logstash,可以直接写 ES。
filebeat.ymlfilebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
output.elasticsearch:
hosts: ["http://localhost:9200"]
✅ 适合:
如果 ES 开启了安全认证(默认 8.x 都开启):
output.elasticsearch:
hosts: ["https://localhost:9200"]
username: "elastic"
password: "your_password"
ssl.verification_mode: none # 测试用,生产建议配证书
默认索引是:
filebeat-8.x.x-yyyy.MM.dd
output.elasticsearch:
hosts: ["http://localhost:9200"]
index: "app-log-%{+yyyy.MM.dd}"
setup.template.name: "app-log"
setup.template.pattern: "app-log-*"
⚠️ 注意
index 只支持 小写%{+yyyy.MM.dd} 是时间变量filebeat.inputs:
- type: log
paths:
- /var/log/app/*.log
fields:
log_type: app
fields_under_root: true
- type: log
paths:
- /var/log/nginx/*.log
fields:
log_type: nginx
fields_under_root: true
output.elasticsearch:
hosts: ["http://localhost:9200"]
indices:
- index: "app-log-%{+yyyy.MM.dd}"
when.equals:
log_type: "app"
- index: "nginx-log-%{+yyyy.MM.dd}"
when.equals:
log_type: "nginx"
output.elasticsearch:
hosts: ["http://localhost:9200"]
output.console:
pretty: true
⚠️ 实际生产环境 不要同时开启多个 output
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
output.elasticsearch:
hosts: ["http://localhost:9200"]
setup.kibana:
host: "http://localhost:5601"
filebeat -e -c filebeat.yml
curl http://localhost:9200/_cat/indices?v
你应该能看到类似:
filebeat-2026.01.21
| 问题 | 原因 |
|---|---|
| 没有数据 | 路径不对 / 文件已读完 |
| ES 连不上 | hosts 写错 / 端口不通 |
| 索引不创建 | 权限不足 |
| 时间字段异常 | ES 时间格式问题 |
| 数据重复 | filebeat 重启未记录 offset |
✅ 不需要 Logstash
❌ 建议用 Logstash
如果你愿意,可以告诉我:
我可以直接帮你写一份 可用配置。