gpt4 book ai didi

docker - 如何使用 docker 容器内的 fluent-bit 访问登录的日志

转载 作者:行者123 更新时间:2023-12-04 08:45:52 26 4
gpt4 key购买 nike

我正在使用 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 *
我想这可能是因为它在容器中并且无法到达日志位置,但是绑定(bind)目录 /run/log:/run/log没有效果。
所以我的问题是:
fluent-bit 在容器内时可以到达 systemd 并读取日志吗?如果是的话 - 我怎样才能做到这一点?

最佳答案

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

  • 您需要以 root 身份运行 fluent-bit 容器,因为访问日志需要 root 权限
  • 将 docker 中的机器 ID 设置为与根机器中的相同
  • 绑定(bind)/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

    关于docker - 如何使用 docker 容器内的 fluent-bit 访问登录的日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64333292/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com