docker - 如何使用 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 并阅读日志吗?如果是的话 - 我怎样才能做到这一点?
解决方案
经过更多研究后,我偶然发现了这个线程: https ://github.com/fluent/fluent-bit/issues/497
长话短说:
- 您需要以 root 身份运行 fluent-bit 容器,因为访问日志需要 root 权限
- 将 docker 中的机器 id 设置为与根机器中的相同
- 绑定 /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
推荐阅读
- intellij-idea - 如何使用 Intellij 在另一个项目中使用一个项目中的方法?
- php - 我如何在 Laravel 5 中的 guard()->attempt 方法中得到特定的错误
- javascript - 排序对象的对象
- premake - Premake 命令行选项以包含项目
- firebase - Firebase 云函数响应时间
- java - P6Spy Spring Boot starter 装饰器产生空输出
- python - 未填充正方形的 Matplotlib 散点图
- ios - 查找tableView的哪一行是CollectionView
- android - 无法在 ViewModel 中调用协程作用域函数
- javascript - material-ui 组件样式自定义 - 将选择组件的颜色更改为白色