温馨提示×

Debian怎么配置Filebeat

小樊
43
2026-07-09 21:46:58
栏目: 智能运维

下面以 Debian 11/12 为例,说明如何安装和配置 Filebeat(Elastic 官方日志采集器)。


一、安装 Filebeat

1. 安装依赖(一般系统已有)

sudo apt update
sudo apt install -y curl gnupg apt-transport-https

2. 导入 Elastic GPG 密钥

curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | \
sudo gpg --dearmor -o /usr/share/keyrings/elastic-keyring.gpg

3. 添加 Elastic APT 源

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

4. 安装 Filebeat

sudo apt update
sudo apt install -y filebeat

二、Filebeat 基础配置

配置文件路径:

/etc/filebeat/filebeat.yml

1. 配置采集日志路径

示例:采集 /var/log/*.log

filebeat.inputs:
  - type: filestream
    enabled: true
    paths:
      - /var/log/*.log

⚠️ 旧版本使用 log 类型,新版本推荐 filestream


2. 配置输出(常见三种)

✅ 输出到 Elasticsearch(最常见)

output.elasticsearch:
  hosts: ["localhost:9200"]
  username: "elastic"
  password: "your_password"

如未启用安全,可去掉 username/password


✅ 输出到 Logstash

output.logstash:
  hosts: ["localhost:5044"]

✅ 输出到本地文件(调试用)

output.file:
  path: "/tmp/filebeat"
  filename: filebeat.out

三、启动并设置开机自启

sudo systemctl enable filebeat
sudo systemctl start filebeat

查看状态:

sudo systemctl status filebeat

四、验证是否正常工作

1. 测试配置

sudo filebeat test config

2. 测试输出

sudo filebeat test output

3. 查看日志

sudo journalctl -u filebeat -f

五、常见示例:采集 Nginx 日志

filebeat.inputs:
  - type: filestream
    enabled: true
    paths:
      - /var/log/nginx/access.log
      - /var/log/nginx/error.log

如输出到 Elasticsearch,可直接在 Kibana 中查看索引:

filebeat-*

六、常见问题

1. 权限不足

sudo usermod -aG adm filebeat
sudo systemctl restart filebeat

2. 时间不更新

确认系统时间正确:

timedatectl

七、卸载 Filebeat

sudo apt remove --purge filebeat
sudo rm -rf /etc/filebeat /var/lib/filebeat

如果你愿意,我可以帮你:

  • ✅ 配置 Filebeat → Elasticsearch → Kibana
  • ✅ 写 systemd 或 Docker 部署方案
  • ✅ 按你的 日志格式定制解析规则

直接告诉我你的使用场景即可。

0