温馨提示×

Filebeat如何输出到ES

小樊
51
2026-07-08 16:31:22
栏目: 编程语言

Filebeat 把数据输出到 Elasticsearch(ES) 是最常见用法之一。下面按 从简单到进阶 给你一个完整说明,包含配置示例和常见注意点。


一、基本思路

Filebeat 的工作流程是:

日志文件 → Filebeat → Elasticsearch

Filebeat 不需要 Logstash,可以直接写 ES。


二、最简单示例(直接输出到 ES)

1️⃣ 修改 filebeat.yml

filebeat.inputs:
  - type: log
    enabled: true
    paths:
      - /var/log/*.log

output.elasticsearch:
  hosts: ["http://localhost:9200"]

✅ 适合:

  • 本地测试
  • 日志结构简单
  • 不需要复杂处理

三、指定 ES 用户名 / 密码(常见)

如果 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"

六、输出到 ES 并同时写控制台(调试用)

output.elasticsearch:
  hosts: ["http://localhost:9200"]

output.console:
  pretty: true

⚠️ 实际生产环境 不要同时开启多个 output


七、Filebeat 输出到 ES 的完整最小配置

filebeat.inputs:
  - type: log
    enabled: true
    paths:
      - /var/log/*.log

output.elasticsearch:
  hosts: ["http://localhost:9200"]

setup.kibana:
  host: "http://localhost:5601"

八、启动 & 验证

启动 Filebeat

filebeat -e -c filebeat.yml

查看 ES 中是否生成索引

curl http://localhost:9200/_cat/indices?v

你应该能看到类似:

filebeat-2026.01.21

九、常见坑总结

问题 原因
没有数据 路径不对 / 文件已读完
ES 连不上 hosts 写错 / 端口不通
索引不创建 权限不足
时间字段异常 ES 时间格式问题
数据重复 filebeat 重启未记录 offset

十、什么时候需要 Logstash?

不需要 Logstash

  • 日志结构简单
  • 只想存 ES + Kibana 查看

建议用 Logstash

  • 需要复杂解析(grok)
  • 多源数据统一处理
  • 需要字段脱敏、过滤

如果你愿意,可以告诉我:

  • ES 版本
  • 是否开启安全
  • 日志格式(JSON / 普通文本)

我可以直接帮你写一份 可用配置

0