11

docker logs 没有日志问题定位解决

 3 years ago
source link: https://liqiang.io/post/debug-docker-no-showing-log?lang=ZH_CN
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

docker logs 没有日志问题定位解决

@POST· 2021-03-22 23:21 · 22 min read

先说明一下,这个场景不是在工作的生产环境,所以操作比较随意,主要还是想介绍一下 docker 日志相关的一些东西。

在运行一个项目的时候,发现业务不正常,然后想看下日志输出什么错误:

  1. [[email protected]]# docker logs -f xxx

居然发现没有任何日志内容,这就比较疑惑了,因为即使是没有任何错误,至少启动信息这些还是i应该要有的,那么是不是应用没有起来?

首先确认了一下 Docker Container 是正常运行的,这没有问题;然后再看下应用监听的端口是否运行着:

  1. [[email protected]]# ss -anlp | grep 222
  2. tcp LISTEN 0 4096 127.0.0.1:2222 *:* users:(("server",pid=27069,fd=10))

端口也是监听的,至少在业务上到这一步了,那么应用肯定是输出了日志的,那么问题就出在 infrature 层面了。

接着就看下 Docker 自身是否异常,先看下 docker 的日志吧:

  1. [[email protected]]# journalctl -xu docker
  2. Journal file /var/log/journal/ae6603ff6b1849af8525fbf6688811fd/system.journal uses an unsupported feature, ignoring file.
  3. Use SYSTEMD_LOG_LEVEL=debug journalctl --file=/var/log/journal/ae6603ff6b1849af8525fbf6688811fd/system.journal to see the details.
  4. -- No entries --

果然一看就发现问题所在了,Docker 自身的日志就出现问题了,看上去和 journal 日志有关,然后这里好在有提示,所以可以看一下:

  1. [[email protected]]# SYSTEMD_LOG_LEVEL=debug journalctl --file=/var/log/journal/ae6603ff6b1849af8525fbf6688811fd/system.journal
  2. Journal effective settings seal=no keyed_hash=yes compress=no compress_threshold_bytes=8B
  3. Journal file /var/log/journal/ae6603ff6b1849af8525fbf6688811fd/system.journal uses incompatible flag xz-compressed disabled at compilation time.
  4. Failed to open journal file /var/log/journal/ae6603ff6b1849af8525fbf6688811fd/system.journal: Protocol not supported
  5. mmap cache statistics: 0 hit, 1 miss
  6. Failed to open files: Protocol not supported

OK,这里问题看上去是因为我重新编译过 systemd (CentOS7 升级 systemd 242/243 )有关,然后看上去是我遗漏了一些压缩选项,所以我需要重新编译一下 systemd。

docker 日志配置

Docker 自己的日志出问题的原因找到了,那 container 为什么没有日志呢,于是还得看下 docker 里面的日志配置是怎样的,找了一番之后确定是这里:

  1. [[email protected]]# grep -ir log /etc/sysconfig/docker
  2. OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false'
  3. # Controls the /etc/cron.daily/docker-logrotate cron job status.
  4. # LOGROTATE=false

原来默认的 docker 日志使用的是 journal 日志,所以 journal 日志无法使用,docker 自己的日志也是无法查看。那么现在除了解决 journal 日志问题之外,还可以看下 docker 支持的其他日志 driver(Driver 列表)。那么不妨先选择本地的日志来尝试一下。

查看文档之后发现可以不修改全局配置,而是修改单个 container 的配置也行,于是我就 run 了一个:

  1. [[email protected]]# docker run \
  2. --log-driver local --log-opt max-size=10m \
  3. alpine echo hello world
  4. /usr/bin/docker-current: Error response from daemon: logger: no log driver named 'local' is registered.
  5. See '/usr/bin/docker-current run --help'.

然而现实就是这么打脸,运行失败,然后再看下文档里面的详细描述,没有发现特别的说明,不过看了一下自己的 docker 版本,发现已经是非常陈旧的版本了,所以就索性升级一把,直接升级到最新的 20.10.5 版本,升级之后发现 docker 日志居然就恢复正常了。

本文就差不多这样了,没有太多惊艳的东西,不过对于 docker 日志的处理确实之前没有关注太多,这次也算是涨了一把姿势。这里需要强调一下的是,官方文档中说明了,如果不设置 log 的 rotate 的话,默认是不会帮你清理日志的,所以需要自己注意。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK