在 Ubuntu 上配置 Filebeat 的数据输出
一 安装与准备
- 更新索引并添加 Elastic 官方 GPG 密钥与 APT 仓库(以 7.x 为例,若使用 8.x 请将仓库路径中的版本号替换为 8.x):
- sudo apt update
- wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
- echo “deb https://artifacts.elastic.co/packages/7.x/apt stable main” | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list
- sudo apt update
- sudo apt install filebeat
- 版本匹配建议:Filebeat 版本应与 Elasticsearch 版本保持一致,避免兼容性问题。
- 启用模块与初始化(可选,便于快速上手系统日志等):
- sudo filebeat modules enable system
- sudo filebeat setup
二 配置输出目标
-
输出到 Elasticsearch(本机或远程)
- 基本配置(示例为本地 ES):
- output.elasticsearch:
- hosts: [“localhost:9200”]
- index: “filebeat-%{[agent.version]}-%{+yyyy.MM.dd}”
- 启用安全认证(如 ES 开启安全特性):
- output.elasticsearch:
- hosts: [“10.0.130.91:9200”]
- protocol: “http” # 或 https
- username: “elastic”
- password: “your_password”
- 说明:hosts 填写 ES 的 IP:端口;云上或受保护网络请使用内网地址与相应端口。
-
输出到 Logstash(常用端口 5044)
- output.logstash:
- hosts: [“logstash.example.com:5044”]
- 说明:若 Logstash 启用了 TLS/认证,需在 Filebeat 中同步配置 TLS 与凭据。
-
仅控制台输出(用于调试)
- output.console:
- pretty: true
以上输出方式可二选一或并存(生产环境通常仅保留一个主输出)。
三 配置输入与处理器
- 基本输入示例(系统日志与 Apache 日志):
- filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/syslog
- /var/log/apache2/*.log
- 常用处理与过滤:
- 忽略旧文件与按条件排除行:
- filebeat.inputs:
- ignore_older: 72h
- exclude_lines: [‘^ERROR’, ‘^WARN’]
- 添加元信息处理器:
- processors:
- add_cloud_metadata: ~
- add_fields:
fields:
env: “production”
- 模块方式采集(如 system、nginx 等):
- sudo filebeat modules enable system
四 启动与验证
- 启动与开机自启:
- sudo systemctl start filebeat
- sudo systemctl enable filebeat
- 查看运行状态与日志:
- sudo systemctl status filebeat
- sudo journalctl -u filebeat -f
- 验证数据是否到达目标:
- Elasticsearch:
- 查看索引:curl -X GET “localhost:9200/_cat/indices?v”
- 简单查询:curl -X GET “localhost:9200/_search?pretty” -H ‘Content-Type: application/json’ -d ‘{ “query”: { “match_all”: {} } }’
- Logstash:
- 查看 Logstash 日志:sudo tail -f /var/log/logstash/logstash-plain.log
- 常见排错要点:
- 核对 hosts、端口、协议(http/https) 与 认证信息 是否正确。
- 检查 Elasticsearch/Logstash 是否允许来自 Filebeat 主机的连接(安全组/防火墙/监听地址)。
- 确保 Filebeat 与 ES 版本一致,避免映射/兼容问题。
五 生产建议
- 使用 Index Lifecycle Management(ILM) 管理索引滚动与保留,自动归档与删除旧数据:
- 示例策略思路:热阶段滚动(如 max_size: 50GB 或 max_age: 7d),删除阶段(如 min_age: 30d 后删除)。
- 为索引设置清晰命名与模板,结合 ILM 策略 与 Rollover 实现按大小/时间切分与自动清理。