You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker Compose部署的Filebeat无法推送容器日志至Logstash求助

解决Filebeat无法推送Docker日志到Logstash的方案

1. 统一Elastic Stack版本

ELK栈使用7.16.2版本,但Filebeat用的是8.0.1,跨大版本存在兼容性问题,会导致通信失败。将Filebeat镜像版本改为与ELK一致:

# 客户端docker-compose.yml中的filebeat镜像
image: docker.elastic.co/beats/filebeat:7.16.2

2. 修复Filebeat的Docker Compose配置

原配置未将filebeat.yml正确挂载到容器内,Filebeat无法加载自定义配置。修改客户端的docker-compose.yml,添加配置文件挂载,并移除无用的build和configs节点:

version: "2.4"
services:
  filebeat:
    image: docker.elastic.co/beats/filebeat:7.16.2
    container_name: filebeat
    user: root
    restart: unless-stopped
    labels:
      co.elastic.logs/enabled: "false"
    volumes:
      - type: bind
        source: /var/run/docker.sock
        target: /var/run/docker.sock
      - type: bind
        source: /var/lib/docker
        target: /var/lib/docker
      # 添加配置文件挂载
      - type: bind
        source: /mnt1/filebeat/config/filebeat.yml
        target: /usr/share/filebeat/filebeat.yml

3. 优化Filebeat配置文件

完善filebeat.yml,添加Docker日志JSON解析,并开启调试日志方便排查:

filebeat.inputs:
- type: container
  enabled: true
  paths:
    - '/var/lib/docker/containers/*/*.log'
  # 解析Docker默认的JSON格式日志
  json.keys_under_root: true
  json.add_error_key: true
  json.message_key: log

output.logstash:
  hosts: ["a.b.c.d:5044"]

# 开启调试日志,排查通信问题
logging.level: debug
logging.to_files: true
logging.files:
  path: /var/log/filebeat
  name: filebeat
  keepfiles: 7
  permissions: 0644

4. 验证服务状态与日志

  • 查看Logstash日志,确认是否接收连接:

    docker logs -f logstash
    

    若成功接收,会出现类似Beats inputs: Starting input listener {:address=>"0.0.0.0:5044"}和New connection from a.b.c.d:xxxx的日志。

  • 查看Filebeat日志,排查是否存在报错:

    docker logs -f filebeat
    

    若出现连接超时、权限不足等错误,根据日志提示调整配置或权限。

5. 确认权限配置

虽然Filebeat以root用户运行,仍需验证容器内是否能读取Docker日志文件:

docker exec filebeat ls -l /var/lib/docker/containers

若无法读取,检查宿主机/var/lib/docker目录的权限,确保root用户可访问。


内容的提问来源于stack exchange,提问作者Aakarshit Agarwal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 20:15:34