CentOS 上部署 Filebeat 的标准流程
- 安装方式
- 官方 YUM 仓库(推荐,便于升级)
- 导入 GPG 并添加仓库:
sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
sudo tee /etc/yum.repos.d/filebeat.repo <<EOF
[filebeat]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF
- 安装并启动:
sudo yum install -y filebeat
sudo systemctl enable --now filebeat
- 直接 RPM 安装(适合离线)
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat--x86_64.rpm
sudo rpm -Uvh filebeat--x86_64.rpm
sudo systemctl enable --now filebeat
- 验证服务状态:
sudo systemctl status filebeat
sudo journalctl -u filebeat -f
- 配置文件路径:/etc/filebeat/filebeat.yml;服务日志:/var/log/filebeat/filebeat(或 journalctl)。
最小可用配置与系统日志采集
-
编辑配置文件 /etc/filebeat/filebeat.yml(示例为输出到本机 Elasticsearch,未启用安全认证):
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
- /var/log/messages
fields:
type: systemlog
log_topic: systemlog
fields_under_root: true
exclude_lines: [“^DBG”]
exclude_files: [“\.gz$”]
output.elasticsearch:
hosts: [“localhost:9200”]
index: “systemlog-%{+YYYY.MM.dd}”
-
检查配置并启动:
sudo filebeat test config -c /etc/filebeat/filebeat.yml
sudo systemctl restart filebeat
sudo tail -f /var/log/filebeat/filebeat
-
说明
- 使用通配符可批量采集目录日志;通过 fields 添加业务标识便于后续索引或路由。
- 如需采集 Nginx、Redis 等应用日志,只需在 inputs 中新增相应 paths。
常见输出目标配置
-
输出到 Elasticsearch(开启安全认证示例)
output.elasticsearch:
hosts: [“https://es-host:9200”]
username: “elastic”
password: “your_password”
index: “systemlog-%{+YYYY.MM.dd}”
- 若启用了 X-Pack Security,需在 ES 中创建具有写入权限的用户并在 Filebeat 中配置凭据。
-
输出到 Kafka(多业务按 topic 路由)
output.kafka:
hosts: [“kafka1:9092”,“kafka2:9092”,“kafka3:9092”]
version: “2.8” # 按实际集群版本设置
topic: “%{[fields.log_topic]}”
codec.format:
string: ‘%{[message]}’
required_acks: 1
compression: gzip
max_message_bytes: 10000000
- 通过 fields.log_topic 在输入处为不同日志指定不同 topic,实现多路复用。
-
输出到 Logstash(集中处理/脱敏/路由)
output.logstash:
hosts: [“logstash:5044”]
- 适合做更复杂的 Grok 解析、字段增强与策略路由。
进阶用法与最佳实践
-
使用 Filebeat 模块采集常见服务
- 启用模块目录并加载内置模块(如 system、nginx、redis 等):
sudo filebeat modules enable system
sudo filebeat setup --modules system,nginx,redis
- 模块包含预置的 inputs、processors、index template,可快速上线并在 Kibana 使用对应仪表盘。
-
多行日志合并(Java 堆栈等)
- 在对应 input 下增加:
multiline.pattern: ‘^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}’
multiline.negate: true
multiline.match: after
- 将堆栈行合并到以时间戳开头的首行,便于检索与展示。
-
JSON 日志解析
- 若应用日志已是 JSON,可直接解析到根字段:
json.keys_under_root: true
json.overwrite_keys: true
- 便于在 ES/Kibana 中直接按字段检索与聚合。
-
索引生命周期管理(ILM)
- 建议配合 Index Lifecycle Management 管理热/温/冷/删除阶段,控制存储成本与保留策略(在 Kibana 或通过索引模板设置)。
快速验证与排错清单
-
配置语法与连通性
- 校验配置:sudo filebeat test config -c /etc/filebeat/filebeat.yml
- 连通性测试(到 ES/Kafka/Logstash):在对应 output 段使用 test output 或查看 Filebeat 日志。
-
服务与日志
- 查看服务状态:sudo systemctl status filebeat
- 实时查看日志:sudo journalctl -u filebeat -f 或 tail -f /var/log/filebeat/filebeat。
-
常见问题
- 权限不足:确保 Filebeat 用户对日志文件有读取权限(/var/log 下文件通常属 root:root,必要时调整或加入 filebeat 组)。
- 主机名解析失败(Kafka 场景):在 /etc/hosts 绑定 broker 主机名与 IP,避免 DNS 解析问题。
- 多行合并不生效:核对 pattern/negate/match 顺序与日志时间格式一致性。