温馨提示×

Filebeat如何定义索引名

小樊
34
2026-07-08 16:58:21
栏目: 编程语言

Filebeat 中,**索引名(index name)**通常是通过 Elasticsearch output 来定义的。下面按常见场景给你说明。


一、在 filebeat.yml 中直接定义索引名(最常见)

示例:按天生成索引

output.elasticsearch:
  hosts: ["http://localhost:9200"]
  index: "filebeat-%{+yyyy.MM.dd}"

✅ 结果:

filebeat-2026.01.21
filebeat-2026.01.22

二、使用 setup.template.namesetup.template.pattern

如果你希望 索引模板和索引名保持一致,可以这样写:

setup.template.name: "filebeat"
setup.template.pattern: "filebeat-*"

output.elasticsearch:
  hosts: ["http://localhost:9200"]
  index: "filebeat-%{+yyyy.MM.dd}"

⚠️ 注意:

  • setup.template.pattern 只影响 模板匹配
  • 真正决定索引名的是 output.elasticsearch.index

三、按模块 / 环境 / 业务定义不同索引

示例:不同环境使用不同索引

output.elasticsearch:
  hosts: ["http://localhost:9200"]
  index: "filebeat-prod-%{+yyyy.MM.dd}"

或根据字段动态生成索引(高级用法):

output.elasticsearch:
  hosts: ["http://localhost:9200"]
  indices:
    - index: "filebeat-app-%{+yyyy.MM.dd}"
      when.contains:
        fields:
          type: "app"
    - index: "filebeat-nginx-%{+yyyy.MM.dd}"
      when.contains:
        fields:
          type: "nginx"

四、使用自定义 fields 控制索引名

filebeat.yml

fields:
  env: "test"
fields_under_root: true

output.elasticsearch:
  hosts: ["http://localhost:9200"]
  index: "filebeat-%{[env]}-%{+yyyy.MM.dd}"

✅ 结果:

filebeat-test-2026.01.21

五、关闭 ILM(否则索引名可能被覆盖)

如果你 自定义了 index,但发现不生效,很可能是 ILM(索引生命周期管理) 在起作用。

关闭 ILM

setup.ilm.enabled: false

或者在 Elastic Stack ≥ 7.x 中:

setup.ilm:
  enabled: false

六、总结(最常用配置)

最简单的自定义索引方式

output.elasticsearch:
  hosts: ["http://localhost:9200"]
  index: "filebeat-%{+yyyy.MM.dd}"

setup.ilm.enabled: false

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

  • 你用的是 Elasticsearch / Logstash / Kafka 哪个 output
  • 你希望 按天 / 按业务 / 按环境 分索引

我可以帮你写一份 完整可用的 filebeat.yml 示例

0