首页 > 解决方案 > 如何使用 docker 容器内的 fluent-bit 访问登录的日志

问题描述

我正在使用 docker-compose.yml 来启动我的服务。所有服务看起来都像这样:

A-service:
    image: A-service
    restart: always
    network_mode: host
    logging:
      driver: journald
      options: 
        tag: "{{.ImageName}}/{{.Name}}/{{.ID}}"


fluent-bit:
  image: 'bitnami/fluent-bit:latest'
  restart: always
  network_mode: host
  command: /fluent-bit/bin/fluent-bit -c /fluent-bit/etc/fluent-bit.conf
  volumes:
    - ./service/config/fluent-bit.conf:/fluent-bit/etc/fluent-bit.conf
    - type: bind
      source: /run/log
      target: /run/log

当我运行时,journalctl -e -f -u docker我看到所有日志都被记录得很好。我遇到的问题是我的 fluent-bit 容器在从 systemd 收集时似乎无法获取任何数据:

fluent-bit.conf:
[SERVICE]
    Flush        5
    Daemon       Off
    Log_Level    debug

[INPUT]
    Name            systemd
    Tag             *


[OUTPUT]
    Name   stdout
    Match  *

我想这可能是因为它在容器中并且无法到达日志位置,但绑定目录/run/log:/run/log没有效果。

所以我的问题是:当它在容器内时,流利的位可以到达 systemd 并阅读日志吗?如果是的话 - 我怎样才能做到这一点?

标签: dockerloggingsystemdfluent-bit

解决方案


经过更多研究后,我偶然发现了这个线程: https ://github.com/fluent/fluent-bit/issues/497

长话短说:

  1. 您需要以 root 身份运行 fluent-bit 容器,因为访问日志需要 root 权限
  2. 将 docker 中的机器 id 设置为与根机器中的相同
  3. 绑定 /run/log/journal:/run/log/journal

所以:

fluent-bit:
      image: 'bitnami/fluent-bit:latest'
      restart: always
      user: root
      network_mode: host
      command: /fluent-bit/bin/fluent-bit -c /fluent-bit/etc/fluent-bit.conf
      volumes:
        - ./service/config/fluent-bit.conf:/fluent-bit/etc/fluent-bit.conf
        - /etc/machine-id:/etc/machine-id:ro
        - /run/log/journal:/run/log/journal

然后,在 fluent-bit.conf 中,您需要编辑 INPUT 路径:

[INPUT]
    Name            systemd
    Tag             *
    Path            /run/log/journal
    Systemd_Filter    _SYSTEMD_UNIT=docker.service
    Systemd_Filter    _SYSTEMD_UNIT=kubelet.service

推荐阅读