如何使用位于Docker容器内的Fluent-bit访问journald中记录的日志

3

我正在使用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时,我可以看到所有的日志都被正常记录。 我的问题在于,当从systemd收集数据时,我的fluent-bit容器似乎无法获取任何数据:
fluent-bit.conf:
[SERVICE]
    Flush        5
    Daemon       Off
    Log_Level    debug

[INPUT]
    Name            systemd
    Tag             *


[OUTPUT]
    Name   stdout
    Match  *

我想这可能是因为它在容器中,无法到达日志位置,但绑定目录/run/log:/run/log没有效果。
所以我的问题是: 当fluent-bit在容器内部时,能否访问systemd并读取journal?如果可以 - 我该如何实现?
1个回答

5

我进行了更多研究后,偶然发现了这个帖子:https://github.com/fluent/fluent-bit/issues/497

简而言之:

  1. 您需要以root权限运行fluent-bit容器,因为访问journal需要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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接