# 1、Filebeat 基本介绍# 1.1 Filebeat 是什么Filebeat 是用于 "转发" 和 "集中日志数据" 的 "轻量型数据采集器" ; Filebeat 会监视指定的日志文件路径,收集日志事件并将数据转发到 Elasticsearch、Logstash、Redis、Kafka 存储服务器。
# 1.2 Filebeat 主要组件Filebeat 主要由两个核心组件组成:Input 和 Harvester 。
Input(输入) :负责定位和识别需要读取的日志文件。可以使用文件的全路径、也可以是文件名称匹配等。Harvester(收割机) :负责打开 Input 定义的这些文件,然后 逐行 读取文件内容。最后将内容发送到指定的输出端。这两个组件协同工作,确保将日志文件尾部 最新的数据 发送到指定的输出端。
配置示例 :
filebeat.inputs : - type : log enabled : true paths : - /var/log/oldwang.log - /var/log/*.log
# 1.3 Filebeat 工作流程当 filebeat 启动后,filebeat 通过 Input 读取指定的日志路径,然后为该日志启动一个收割进程 harvester,每一个收割进程读取一个日志文件的新内容,并发送这些新的日志数据到处理程序 spooler,处理程序会集合这些事件,最后 filebeat 会发送集合的数据到你指定的位置。
# 2、Filebeat 安装配置# 2.1 Filebeat 安装安装 Filebeat [ root@web01 ~] $ dpkg -i filebeat-8.18.2-amd64.deb[ root@web01 ~]
启动 Filebeat # 2.2 Filebeat 配置说明filebeat.inputs : - type : log enabled : true paths : - /var/log/nginx/access.log tags : [ "nginx-access" ] - type : log enabled : true paths : - /var/log/nginx/error.log tags : [ "nginx-error" ] output.elasticsearch : hosts : [ "elasticsearch:9200" ]
# 3.1 Filebeat 从终端读取数据配置 Filebeat 从终端读取数据,然后从终端输出消息 [ root@web01 ~] [ root@web01 ~] $ vim /etc/filebeat/conf.d/01-stdin-output-console.yml
配置文件内容:
filebeat.inputs : - type : stdin enable : true output.console : pretty : true enable : true
启动 Filebeat,并明确指定配置文件路径,然后输入一条测试内容 注意:基于如下方式启用 filebeat 的同时,需要将 filebeat 服务关闭。否则会报错: Exiting: /var/lib/filebeat/filebeat.lock: data path already locked by another beat. Please make sure that multiple beats are not sharing the same data path (path.data)
[ root@web01 ~] [ root@web01 ~] $ filebeat -e -c /etc/filebeat/conf.d/01-stdin-output-console.ymlhello,filebeat
结果返回 { "@timestamp" : "2025-07-26T04:37:46.332Z" , "@metadata" : { "beat" : "filebeat" , "type" : "_doc" , "version" : "8.18.2" } , "log" : { "offset" : 0 , <== Filebeat 当前从日志文件中读取到的位置 "file" : { "path" : "" <== 日志来源文件路径 (空值, 因为此日志来自 stdin 输入而非文件) } } , "message" : "hello,filebeat" , <== 核心日志内容 "input" : { "type" : "stdin" <== 表示输入来源为标准输入 } , "ecs" : { "version" : "8.0.0" } , "host" : { "name" : "es-node4.wang.org" <== 产生日志的主机名信息 } , "agent" : { "id" : "db23f4d1-8f1e-48ac-a632-b7411303f161" , "name" : "es-node4.wang.org" , "type" : "filebeat" , "version" : "8.18.2" , "ephemeral_id" : "ba6b6172-3cdf-4060-b43f-a018f79e5c42" } }
说明: offset: 0 表示 Filebeat 刚开始读取该文件,从第一个字节(起始位置)开始读取。如果 offset: 1500 ,表示 Filebeat 已经读取了前 1500 字节,它会从第 1501 字节继续读取。
# 3.2 Filebeat 从网络中读取数据在分布式系统或多服务架构中,直接从本地系统采集日志有时不太方便。为此,可以将应用程序日志通过 TCP 或 UDP 协议发送到特定网络端口,然后通过 Filebeat 接收此端口的日志数据。
# 3.2.1 监听 TCP 端口并输出到控制台编写配置 [ root@web01 ~] $ vim /etc/filebeat/conf.d/02-tcp-output-console.yml
配置文件内容:
filebeat.inputs : - type : tcp enable : true host : "0.0.0.0:5656" max_message_size : "10M" output.console : pretty : true enable : true
启动 Filebeat 并应用上述配置 [ root@web01 ~] $ filebeat -e -c /etc/filebeat/conf.d/02-tcp-output-console.yml
验证 Filebeat 是否成功监听了 5656 端口 [ root@web01 ~] $ netstat -nltp tcp6 0 0 :::5656 :::* LISTEN 64290 /filebeat
# 3.2.2 监听 UDP 端口并输出到控制台编写配置 [ root@web01 ~] $ vim /etc/filebeat/conf.d/02-udp-output-console.yml
配置文件内容:
filebeat.inputs : - type : udp enable : true host : "0.0.0.0:5658" max_message_size : "10M" output.console : pretty : true enable : true
启动 Filebeat 并应用上述配置 [ root@web01 ~] $ filebeat -e -c /etc/filebeat/conf.d/02-udp-output-console.yml
验证 Filebeat 是否成功监听了 5658 端口 [ root@web01 ~] $ netstat -nltup udp6 0 0 :::5658 :::* 86005 /filebeat
# 3.2.3 模拟发送数据[ root@web01 ~] $ echo "hello,tcp" | nc 192.168 .80.154 5656 [ root@web01 ~] $ echo "hello,udp" | nc -u 192.168 .80.154 5658
# 3.2.4 查看接收数据{ "@timestamp" : "2025-07-26T05:00:32.381Z" , "@metadata" : { "beat" : "filebeat" , "type" : "_doc" , "version" : "8.18.2" } , "input" : { "type" : "tcp" <== 当前日志的输入类型为 TCP } , "ecs" : { "version" : "8.0.0" } , "host" : { "name" : "es-node4.wang.org" <== 运行 Filebeat 代理的主机名 } , "agent" : { "type" : "filebeat" , "version" : "8.18.2" , "ephemeral_id" : "f3cc997d-dfc0-4716-a9ae-c1da98e63532" , "id" : "db23f4d1-8f1e-48ac-a632-b7411303f161" , "name" : "es-node4.wang.org" } , "message" : "hello,tcp" , <== 实际接收到的日志内容, 即通过 TCP 发送到 5656 端口的原始数据 "log" : { "source" : { "address" : "192.168.80.151:44322" <== 发送日志的客户端 IP 地址 } } }
{ "@timestamp" : "2025-07-26T05:08:43.767Z" , "@metadata" : { "beat" : "filebeat" , "type" : "_doc" , "version" : "8.18.2" , "truncated" : false } , "message" : "hello,udp\n" , <== 实际接收到的日志内容, 即通过 UDP 发送到 5658 端口的原始数据 "log" : { "source" : { "address" : "192.168.80.151:50754" <== 发送日志的客户端 IP 地址 } } , "input" : { "type" : "udp" <== 当前日志的输入类型为 UDP } , "host" : { "name" : "es-node4.wang.org" <== 运行 Filebeat 代理的主机名 } , "agent" : { "name" : "es-node4.wang.org" , "type" : "filebeat" , "version" : "8.18.2" , "ephemeral_id" : "af3b7435-9fc5-4c9b-882a-6a0e34ba1847" , "id" : "db23f4d1-8f1e-48ac-a632-b7411303f161" } , "ecs" : { "version" : "8.0.0" } }
# 3.3 Filebeat 从文件中读取数据注意:Filebeat 7.16.0 版本之后 Log 被废弃,官方建议使用 filestream 插件。
配置 Filebeat 从文件中读取数据 配置文件内容:
filebeat.inputs : - type : log enabled : true paths : - /var/log/test.log output.console : pretty : true enable : true
启动 Filebeat [ root@web01 ~] [ root@web01 ~] echo "hello,filebeat01" >> /var/log/test.log[ root@web01 ~] echo "hello,filebeat02" >> /var/log/test.log
查看 Filebeat 接收到的数据 { "@timestamp" : "2025-07-26T16:21:41.320Z" , "@metadata" : { "beat" : "filebeat" , "type" : "_doc" , "version" : "8.18.2" } , "log" : { "offset" : 0 , <== 起始位置 "file" : { "path" : "/var/log/test.log" } } , "message" : "hello,filebeat01" , <== 输入内容 "input" : { "type" : "log" } , "ecs" : { "version" : "8.0.0" } , "host" : { "name" : "es-node4.wang.org" } , "agent" : { "id" : "db23f4d1-8f1e-48ac-a632-b7411303f161" , "name" : "es-node4.wang.org" , "type" : "filebeat" , "version" : "8.18.2" , "ephemeral_id" : "e793a9b2-afaa-41c5-8075-c1899f770c14" } }
{ "@timestamp" : "2025-07-26T16:22:56.332Z" , "@metadata" : { "beat" : "filebeat" , "type" : "_doc" , "version" : "8.18.2" } , "log" : { "offset" : 17 , <== 断点续读 "file" : { "path" : "/var/log/test.log" } } , "message" : "hello,filebeat02" , <== 输入内容 "input" : { "type" : "log" } , "ecs" : { "version" : "8.0.0" } , "host" : { "name" : "es-node4.wang.org" } , "agent" : { "id" : "db23f4d1-8f1e-48ac-a632-b7411303f161" , "name" : "es-node4.wang.org" , "type" : "filebeat" , "version" : "8.18.2" , "ephemeral_id" : "e793a9b2-afaa-41c5-8075-c1899f770c14" } }
# 4、Filebeat 常用 OutputFilebeat 常用的 Output 参考地址:https://www.elastic.co/guide/en/beats/filebeat/current/configuring-output.html
# 4.1 Filebeat 输出至 ES 集群配置 Filebeat 将日志采集,并输出到 Elasticsearch 集群 # 4.1.1 编写配置文件配置文件内容:
filebeat.inputs : - type : log enabled : true paths : /var/log/test.log output.elasticsearch : hosts : [ "192.168.80.151:9200" , "192.168.80.152:9200" , "192.168.80.153:9200" ]
# 4.1.2 重启 Filebeat 服务# 4.1.3 向监控的日志文件追加新内容通过 Cerebro 观察新写入到 ES 的索引数据
索引默认格式说明 默认没有定义索引名称的情况下,索引默认格式为: .ds-{name}-{version}-{date}-{date}-{counter} 例如: .ds-filebeat-8.18.2-2099.05.01-000001
name:名称为 filebeat version:Filebeat 版本号 date:索引创建日期,格式为 年。月. 日 counter:自增的 6 位数计数器 # 4.2 Filebeat 自定义索引名称默认 Filebeat 写入 ES 的索引名称为 .ds-filebeat-* ,如果希望修改索引名称,则通过 index 关键字实现。
修改 Filebeat 配置文件 配置文件内容:
filebeat.inputs : - type : log enabled : true paths : - /var/log/test.log output.elasticsearch : hosts : [ "192.168.80.151:9200" , "192.168.80.152:9200" , "192.168.80.153:9200" ] index : "test-log-%{[agent.version]}-%{+yyyy.MM.dd}" setup.ilm.enabled : false setup.template.name : "test-log" setup.template.pattern : "test-log-*" setup.template.enabled : false setup.template.overwrite : true
重启 Filebeat 产生新的索引 [ root@web01 ~] [ root@web01 ~]
向监控的日志文件追加新内容 [ root@web01 ~] [ root@web01 ~]
检查结果 通过 Cerebro 检查结果 通过 Kibana 检查结果,索引为 test-log-8.18.2--2025.07.27 ,索引模板为 test-log # 4.3 Filebeat 自定义索引分片默认情况下 Filebeat 写入到 ES 的索引分片为 1,如果需要修改分片,可以通过以下两种方式:
# 方式 1:通过 Filebeat 配置文件修改 filebeat 配置文件 配置文件内容:
filebeat.inputs : - type : log enabled : true paths : - /var/log/test.log output.elasticsearch : hosts : [ "192.168.80.151:9200" , "192.168.80.152:9200" , "192.168.80.153:9200" ] index : "test-log-%{[agent.version]}-%{+yyyy.MM.dd}" setup.ilm.enabled : false setup.template.name : "test-log" setup.template.pattern : "test-log-*" setup.template.enabled : false setup.template.overwrite : true setup.template.settings : index.number_of_shards : 3 index.number_of_replicas : 1
删除索引、删除索引模板,最后重启 filebeat systemctl restart filebeat
产生新的数据及索引 [ root@web01 ~] [ root@web01 ~]
验证索引状态 # 方式 2:手动创建索引模板修改 filebeat 配置文件 配置文件内容:
filebeat.inputs : - type : log enabled : true paths : - /var/log/test.log output.elasticsearch : hosts : [ "192.168.80.151:9200" , "192.168.80.152:9200" , "192.168.80.153:9200" ] index : "test-log-%{[agent.version]}-%{+yyyy.MM.dd}" setup.ilm.enabled : false setup.template.name : "test-log" setup.template.pattern : "test-log-*" setup.template.enabled : false setup.template.overwrite : true
手动创建索引的模板,然后设定主分片数据和副本分片数量 PUT /_index_template/test-log // 索引模板名称 { "index_patterns": ["test-log-*"], // 模板匹配并关联哪些索引; "template": { "settings": { "index": { "number_of_shards": 3, "number_of_replicas": 1 } } } }
验证索引模板
删除此前的索引,然后重启服务,验证索引状态 (三分片,一副本)
# 5、Filebeat 字段添加在 Filebeat 中,可以使用 tags 配置项为每条数据添加一个标签列表。例如:可以为访问日志添加 access_log 标签,为错误日志添加 error_log 标签。这些标签在后续的日志处理和分析阶段可以用于有效地标识和筛选数据。
配置文件编写 配置文件内容:
filebeat.inputs : - type : log enabled : true paths : /var/log/access.log tags : [ "system-access" ] - type : log enabled : true paths : /var/log/error.log tags : [ "system-error" ] output.console : pretty : true enabled : true
启动对应的 filebeat 追加日志内容 [ root@web01 ~] [ root@web01 ~]
验证对应的结果 { "@timestamp" : "2025-07-27T07:22:52.136Z" , "@metadata" : { "beat" : "filebeat" , "type" : "_doc" , "version" : "8.18.2" } , "log" : { "file" : { "path" : "/var/log/access.log" <== 日志路径 } , "offset" : 0 } , "message" : "test-access" , <== 日志内容 "tags" : [ "system-access" <== 日志标签 ] , "input" : { "type" : "log" <== 输入类型 } , "ecs" : { "version" : "8.0.0" } , "host" : { "name" : "es-node4.wang.org" } , "agent" : { "version" : "8.18.2" , "ephemeral_id" : "b5cc1683-2c57-47dc-811d-b1627c94d9ad" , "id" : "db23f4d1-8f1e-48ac-a632-b7411303f161" , "name" : "es-node4.wang.org" , "type" : "filebeat" } }
{ "@timestamp" : "2025-07-27T07:22:52.136Z" , "@metadata" : { "beat" : "filebeat" , "type" : "_doc" , "version" : "8.18.2" } , "log" : { "file" : { "path" : "/var/log/error.log" } , "offset" : 0 } , "message" : "test-error" , "tags" : [ "system-error" ] , "input" : { "type" : "log" } , "ecs" : { "version" : "8.0.0" } , "host" : { "name" : "es-node4.wang.org" } , "agent" : { "version" : "8.18.2" , "ephemeral_id" : "b5cc1683-2c57-47dc-811d-b1627c94d9ad" , "id" : "db23f4d1-8f1e-48ac-a632-b7411303f161" , "name" : "es-node4.wang.org" , "type" : "filebeat" } }
# 5.2 使用 fields 添加字段在 Filebeat 中,可以使用 fields 配置项为每条数据添加自定义字段。
配置文件编写 配置文件内容:
filebeat.inputs : - type : log enabled : true paths : /var/log/access.log fields : namespace : system- access fields_under_root : true output.console : pretty : true console : true
启动对应的 filebeat [ root@web01 ~] [ root@web01 ~]
查看 Filebeat 输出的结果 { "@timestamp" : "2025-07-27T07:45:22.216Z" , "@metadata" : { "beat" : "filebeat" , "type" : "_doc" , "version" : "8.18.2" } , "namespace" : "system-access" , <== 自定义字段内容 "ecs" : { "version" : "8.0.0" } , "host" : { "name" : "es-node4.wang.org" } , "agent" : { "version" : "8.18.2" , "ephemeral_id" : "4d716c9b-06eb-4a30-a511-e2d0ddf32f60" , "id" : "db23f4d1-8f1e-48ac-a632-b7411303f161" , "name" : "es-node4.wang.org" , "type" : "filebeat" } , "log" : { "offset" : 24 , "file" : { "path" : "/var/log/access.log" } } , "message" : "test-access" , "input" : { "type" : "log" } }
# 6、Filebeat 处理器# 6.1 处理器 Processors在 Filebeat 中,Processors 用于在数据发送到目标后端之前,可以对源数据执行预处理的组件。这些处理器可以执行各种数据转换任务,如修改、添加或删除字段等。常见的处理器包括:
add_fields: 向数据中添加新的字段。 drop_fields: 删除数据中的指定字段。 rename: 重命名数据中的字段。 参考文档:https://www.elastic.co/guide/en/beats/filebeat/8.18/filtering-and-enhancing-data.html
# 6.2 删除消息中的内容配置文件编写(过滤掉 DEBUG 开头的日志) 配置文件内容:
filebeat.inputs : - type : log enabled : true paths : /var/log/test.log processors : - drop_event : when : regexp : message : "^DEBUG" output.console : pretty : true console : true
排除 log.level: info 的日志配置
processors : - drop_event : when : equals : log.level : "info"
启动 filebeat 并写入测试内容 [ root@web01 ~] echo "DEBUG: Hello" >> /var/log/test.logecho "DEBUG: Hello" >> /var/log/test.logecho "ADEBUG: Hello" >> /var/log/test.logecho "BDEBUG: Hello" >> /var/log/test.log
输出结果 仅显示 ADEBUG 和 BDEBUG 开头的日志,DEBUG 开头的日志被成功过滤。 { "@timestamp" : "2025-07-27T13:13:43.324Z" , "@metadata" : { "beat" : "filebeat" , "type" : "_doc" , "version" : "8.18.2" } , "ecs" : { "version" : "8.0.0" } , "host" : { "name" : "es-node4.wang.org" } , "log" : { "offset" : 14 , "file" : { "path" : "/var/log/test.log" } } , "message" : "ADEBUG: Hello" , <== "input" : { "type" : "log" } , "agent" : { "version" : "8.18.2" , "ephemeral_id" : "6bd21962-ed78-431b-9aac-e866a0643dd4" , "id" : "db23f4d1-8f1e-48ac-a632-b7411303f161" , "name" : "es-node4.wang.org" , "type" : "filebeat" } } { "@timestamp" : "2025-07-27T13:19:38.415Z" , "@metadata" : { "beat" : "filebeat" , "type" : "_doc" , "version" : "8.18.2" } , "host" : { "name" : "es-node4.wang.org" } , "log" : { "offset" : 54 , "file" : { "path" : "/var/log/test.log" } } , "message" : "BDEBUG: Hello" , <== "input" : { "type" : "log" } , "agent" : { "name" : "es-node4.wang.org" , "type" : "filebeat" , "version" : "8.18.2" , "ephemeral_id" : "6bd21962-ed78-431b-9aac-e866a0643dd4" , "id" : "db23f4d1-8f1e-48ac-a632-b7411303f161" } , "ecs" : { "version" : "8.0.0" } }
# 6.3 删除无用字段配置文件编写 配置文件内容:
filebeat.inputs : - type : log enabled : true paths : /var/log/test.log processors : - drop_fields : fields : [ "ecs" , "host" , "input" ] output.console : pretty : true console : true
启动 filebeat 并写入测试内容 [ root@web01 ~] echo "Hello filebeat" >> /var/log/test.log
检查对应的结果 会发现 ecs、host、input 等字段都被删除了。 { "@timestamp" : "2025-07-27T13:25:48.805Z" , "@metadata" : { "beat" : "filebeat" , "type" : "_doc" , "version" : "8.18.2" } , "log" : { "offset" : 68 , "file" : { "path" : "/var/log/test.log" } } , "message" : "Hello filebeat" , "agent" : { "version" : "8.18.2" , "ephemeral_id" : "d73fa509-7ce0-4832-94cb-9f572ca69ada" , "id" : "db23f4d1-8f1e-48ac-a632-b7411303f161" , "name" : "es-node4.wang.org" , "type" : "filebeat" } }
# 6.4 重写源数据信息配置文件编写 配置文件内容:
filebeat.inputs : - type : log enabled : true paths : /var/log/test.log processors : - rename : fields : - from : "agent.name" to : "agent_name" - from : "agent.type" to : "agent_type" - from : "log.file.path" to : "log_file_path" output.console : pretty : true console : true
启动 filebeat 并写入测试内容 [ root@web01 ~] echo "Hello filebeat" >> /var/log/test.log
检查 filebeat 输出的结果 字段结构从嵌套变为平级,更易于后续处理。 { "@timestamp" : "2025-07-28T15:26:45.256Z" , "@metadata" : { "beat" : "filebeat" , "type" : "_doc" , "version" : "8.18.2" } , "agent" : { "version" : "8.18.2" , "ephemeral_id" : "dea0a33b-419f-4900-9e2c-0a2fbace6092" , "id" : "db23f4d1-8f1e-48ac-a632-b7411303f161" } , "ecs" : { "version" : "8.0.0" } , "log" : { "offset" : 83 , "file" : { } } , "input" : { "type" : "log" } , "agent_name" : "es-node4.wang.org" , <== "agent_type" : "filebeat" , <== "log_file_path" : "/var/log/test.log" , <== "message" : "Hello filebeat" , "host" : { "name" : "es-node4.wang.org" } }