温馨提示×

Debian中Filebeat如何收集日志

小樊
50
2026-08-31 05:40:35
栏目: 智能运维

Debian 上用 Filebeat 收集日志,一般流程如下(以 Elastic Stack / ELK 为例,也可以单独使用 Filebeat 输出到 Elasticsearch、Logstash、Kafka 等)。


一、安装 Filebeat(Debian)

方式一:官方仓库(推荐)

# 1. 安装依赖
sudo apt update
sudo apt install curl gpg -y

# 2. 导入 Elastic GPG 密钥
curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elastic.gpg

# 3. 添加 Elastic 仓库
echo "deb [signed-by=/usr/share/keyrings/elastic.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 filebeat -y

如果是 7.x,把 8.x 改成 7.x


二、Filebeat 基本配置

配置文件路径:

/etc/filebeat/filebeat.yml

1️⃣ 配置日志输入(核心)

示例:收集系统日志

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

或(老版本写法,仍可用):

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

2️⃣ 配置输出(Output)

输出到 Elasticsearch(最常见)

output.elasticsearch:
  hosts: ["localhost:9200"]
  # 如果启用了安全认证
  # username: "elastic"
  # password: "your_password"

输出到 Logstash

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

3️⃣ 启动 Filebeat 模块(可选)

Filebeat 自带很多常用日志模块:

filebeat modules list

启用模块(如 nginx):

filebeat modules enable nginx

配置模块:

/etc/filebeat/modules.d/nginx.yml

三、测试并启动 Filebeat

1️⃣ 测试配置

filebeat test config
filebeat test output

2️⃣ 启动并设置开机自启

sudo systemctl enable filebeat
sudo systemctl start filebeat

3️⃣ 查看状态

systemctl status filebeat

四、常见日志收集示例

✅ 收集自定义应用日志

filebeat.inputs:
  - type: log
    enabled: true
    paths:
      - /opt/myapp/logs/*.log
    fields:
      app: myapp
    fields_under_root: true

✅ 按服务区分日志(推荐)

filebeat.inputs:
  - type: log
    paths:
      - /var/log/nginx/access.log
    fields:
      service: nginx

  - type: log
    paths:
      - /var/log/mysql/error.log
    fields:
      service: mysql

五、常见问题排查

1️⃣ Filebeat 没收集日志?

journalctl -u filebeat -f

检查:

  • 文件路径是否存在
  • 权限(filebeat 用户是否能读日志)
  • enabled: true

2️⃣ 日志重复收集?

Filebeat 使用注册表(registry)记录读取位置:

/var/lib/filebeat/registry

不要随意删除。


六、最小可用示例(总结)

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

output.elasticsearch:
  hosts: ["localhost:9200"]

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

  • ✅ 写一个 完整 filebeat.yml
  • ✅ 对接 Elasticsearch + Kibana
  • ✅ 使用 Logstash 过滤日志
  • ✅ 只收集 Docker / Nginx / Java 日志

告诉我你的 具体使用场景即可。

0