Ubuntu 上部署与配置 Filebeat 收集日志
一 安装 Filebeat
- 使用 APT 安装(推荐)
- 更新索引并安装依赖
sudo apt update && sudo apt install -y apt-transport-https
- 导入 Elastic GPG 密钥
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor | sudo tee /usr/share/keyrings/elastic-keyring.gpg >/dev/null
- 添加 Elastic APT 仓库(按需选择 8.x 或 7.x)
echo “deb [signed-by=/usr/share/keyrings/elastic-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main” | sudo tee /etc/apt/sources.list.d/elastic-8.x.list
- 安装并启动
sudo apt update && sudo apt install -y filebeat
sudo systemctl enable --now filebeat
- 验证
sudo systemctl status filebeat && journalctl -u filebeat -f
- 使用 Snap 安装(可选)
sudo snap install filebeat --classic
sudo systemctl enable --now filebeat
配置文件路径为:/var/snap/filebeat/common/etc/filebeat.yml。
二 配置输入与输出
- 编辑主配置文件:/etc/filebeat/filebeat.yml(Snap 为 /var/snap/filebeat/common/etc/filebeat.yml)。
- 启用系统模块(可选,便于解析常见系统日志):
sudo filebeat modules enable system
- 示例 1:直接输出到 Elasticsearch(开发环境常用)
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/syslog
- /var/log/auth.log
output.elasticsearch:
hosts: [“http://localhost:9200”]
index: “filebeat-%{[agent.version]}-%{+yyyy.MM.dd}”
- 示例 2:输出到 Logstash(生产常用,便于过滤与丰富)
output.logstash:
hosts: [“192.168.1.10:5044”]
说明:Filebeat 支持输出到 Elasticsearch、Logstash 等目标,按实际环境选择其一或同时使用。
三 启动与验证
- 应用配置并重启:
sudo systemctl restart filebeat
- 查看运行状态与日志:
sudo systemctl status filebeat
journalctl -u filebeat -f
- 验证数据是否到达:
- Elasticsearch:
curl -X GET “localhost:9200/_cat/indices?v”
curl -X GET “localhost:9200/filebeat-*/_search?pretty” -H ‘Content-Type: application/json’ -d ‘{ “query”: { “match_all”: {} } }’
- Logstash:
tail -f /var/log/logstash/logstash-plain.log
以上命令可快速确认索引是否创建、是否有事件写入。
四 安全与运维要点
- 启用安全认证(Elasticsearch 开启安全时):在 output.elasticsearch 下添加
username: “elastic”
password: “your_password”
若使用 HTTPS,将 hosts 改为 https://… 并配置证书校验。
- 索引生命周期管理(ILM):Filebeat 不负责归档,建议在 Elasticsearch 配置 ILM 策略实现按大小/时间滚动与自动删除旧索引,例如创建策略在 7 天滚动、30 天删除,并绑定到索引模板。
- 常用调优与过滤:
- 忽略旧文件:ignore_older: 72h
- 排除指定行:exclude_lines: [“^ERROR”, “^WARN”]
- 添加云元数据:processors: [add_cloud_metadata: ~]
这些可减少噪声、提升采集效率与可观测性。