Docker日志无法检索的症状表现
在日常巡检服务状态时,通过Portainer界面访问容器日志,可能会在右上角看到红色的警告图标,并伴随提示信息:Unable to retrieve container logs。
鉴于此前未出现过此类异常,首先尝试在服务器端使用命令 docker logs -f containerID 直接查看日志输出。结果显示日志同样无法正常滚动,依然停留在过去某个时间点的记录上。
初步判断并非业务服务本身的日志打印机制出现故障。经过Google搜索排查,未发现与当前症状完全匹配的案例。由于部分日志能够正常收集而部分不能,推测这与Docker配置的日志驱动设置有关。
原本计划部署EFK(Elasticsearch, Fluentd, Kibana)日志栈,但考虑到当前日志体量尚未达到需要复杂架构的程度,因此未更改Docker的日志驱动,仍沿用系统默认的 journald。
[root@ad-official xiaoxiao]# docker info|grep Logging
WARNING: You're not using the default seccomp profile
Logging Driver: journald
查阅 journald 的官方文档 man journald.conf,其中包含关于速率限制的关键说明。
man journald.conf ... RateLimitInterval=, RateLimitBurst= Configures the rate limiting that is applied to all messages generated on the system. If, in the time interval defined by RateLimitInterval=, more messages than specified in RateLimitBurst= are logged by a service, all further messages within the interval are dropped until the interval is over. A message about the number of dropped messages is generated. This rate limiting is applied per-service, so that two services which log do not interfere with each other's limits. Defaults to 1000 messages in 30s. The time specification for RateLimitInterval= may be specified in the following units: "s", "min", "h", "ms", "us". To turn off any kind of rate limiting, set either value to 0. ...
文档指出默认配置为每30秒最多接收1000条日志。由此可知,近期某项服务单日日志文件大小接近3GB,导致其他服务的日志也受到影响,大量新日志被 journald 因超出限制而丢弃。调整相关配置即可解决此问题。
编辑 /etc/systemd/journald.conf 配置文件,将 RateLimitBurst 的值从默认的 1000 调整为 5000,具体数值可根据实际日志输出量进行适当调整。
[root@ad-official log]# cat /etc/systemd/journald.conf
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See journald.conf(5) for details.
[Journal]
#Storage=auto
#Compress=yes
#Seal=yes
#SplitMode=uid
#SyncIntervalSec=5m
#RateLimitInterval=30s
RateLimitBurst=5000
#SystemMaxUse=
#SystemKeepFree=
#SystemMaxFileSize=
#RuntimeMaxUse=
#RuntimeKeepFree=
#RuntimeMaxFileSize=
#MaxRetentionSec=
#MaxFileSec=1month
ForwardToSyslog=no
#ForwardToKMsg=no
#ForwardToConsole=no
ForwardToWall=no
#TTYPath=/dev/console
#MaxLevelStore=debug
#MaxLevelSyslog=debug
#MaxLevelKMsg=notice
#MaxLevelConsole=info
#MaxLevelWall=emerg
#LineMax=48K
同时建议将 ForwardToSyslog 和 ForwardToWall 设置为 no。因为默认值为 yes,这会导致清理 journal 日志文件后,Syslog 中的对应记录未被清除,长期积累可能耗尽磁盘空间。
完成配置修改后,执行以下命令重启 journald 服务以生效:systemctl restart systemd-journald.service
以上即为解决该问题的完整步骤,希望能为读者提供有益参考,欢迎继续支持本站内容。