第8章 容器日志

摘要

本章介绍了容器日志管理在微服务架构中的重要性。容器因其短暂的生命周期,使得集中式日志管理成为生产环境不可或缺的部分。文章重点讲解了 Docker 日志机制:容器日志默认输出到 STDOUT/STDERR。通过 httpd 容器实例,演示了前台运行直接查看日志、后台运行使用 docker attachdocker logs 命令查看日志的两种方式,并对比了它们的优缺点。最终推荐使用 docker logs 命令(支持 -f 参数实时跟踪)作为查看容器历史与实时日志的标准方法。

续上篇博客 Docker 容器监控全攻略:原生命令与 cAdvisor 实操详解

高效的监控和日志管理对保持生产系统持续稳定地运行以及排查问题至关重要。

在微服务架构中,由于容器的数量众多以及快速变化的特性使得记录日志和监控变得越来越重要。考虑 到容器短暂和不固定的生命周期,当我们需要 debug 问题时有些容器可能已经不存在了。因此,一套集 中式的日志管理系统是生产环境中不可或缺的组成部分。

Docker logs

对于一个运行的容器,Docker 会将日志发送到 容器标准输出设备(STDOUT)和标准错误设备 (STDERR),STDOUT 和 STDERR 实际上就是容器的控制台终端。

举个例子,用下面的命令运行 httpd 容器:

[root@docker ~]# docker run -p 80:80 httpd
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
[Tue Oct 08 14:28:10.312876 2024] [mpm_event:notice] [pid 1:tid 1] AH00489: 
Apache/2.4.62 (Unix) configured -- resuming normal operations
[Tue Oct 08 14:28:10.316058 2024] [core:notice] [pid 1:tid 1] AH00094: Command 
line: 'httpd -D FOREGROUND'

我们在启动日志的时候没有用 -d 参数,httpd 容器以前台方式启动,日志会直接打印在当前的终端窗 口。

如果加上 -d 参数以后台方式运行容器,我们就看不到输出的日志了。

[root@docker ~]# docker run -d -p 80:80 httpd
a8286845e6f8afc09fdcdf0b94248239963e3ad04495e927a68a2148814c65fd

这种情况下如果要查看容器的日志,有两种方法:

  1. attach 到该容器。
  2. docker logs命令查看日志。

先来看 attach 的方法。运行 docker attach 命令。

[root@docker ~]# docker attach a8286845e6f8

attach 到了 httpd 容器,但并没有任何输出,这是因为当前没有新的日志信息。

为了产生一条新的日志,可以在 host 的另一个命令行终端执行 curl localhost

终端B:

[root@docker ~]# curl localhost
<html><body><h1>It works!</h1></body></html>
[root@docker ~]# curl localhost
<html><body><h1>It works!</h1></body></html>

这时,attach 的终端就会打印出新的日志。

终端A:

[root@docker ~]# docker attach a8286845e6f8
172.17.0.1 - - [08/Oct/2024:14:30:24 +0000] "GET / HTTP/1.1" 200 45
172.17.0.1 - - [08/Oct/2024:14:30:25 +0000] "GET / HTTP/1.1" 200 45

attach 的方法在实际使用中不太方便,因为:

  1. 只能看到 attach 之后的日志,以前的日志不可见。
  2. 退出 attach 状态比较麻烦(Ctrl+p 然后 Ctrl+q 组合键),一不小心很容器将容器杀掉(比如按下 Ctrl+C)。

查看容器日志推荐的方法是用 docker logs命令。

[root@docker ~]# docker logs a8286845e6f8
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
[Tue Oct 08 14:28:33.168712 2024] [mpm_event:notice] [pid 1:tid 1] AH00489: 
Apache/2.4.62 (Unix) configured -- resuming normal operations
[Tue Oct 08 14:28:33.168801 2024] [core:notice] [pid 1:tid 1] AH00094: Command 
line: 'httpd -D FOREGROUND'
[Tue Oct 08 14:29:37.160729 2024] [mpm_event:notice] [pid 1:tid 1] AH00491: 
caught SIGTERM, shutting down
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
[Tue Oct 08 14:30:08.379226 2024] [mpm_event:notice] [pid 1:tid 1] AH00489: 
Apache/2.4.62 (Unix) configured -- resuming normal operations
[Tue Oct 08 14:30:08.379375 2024] [core:notice] [pid 1:tid 1] AH00094: Command 
line: 'httpd -D FOREGROUND'
172.17.0.1 - - [08/Oct/2024:14:30:24 +0000] "GET / HTTP/1.1" 200 45
172.17.0.1 - - [08/Oct/2024:14:30:25 +0000] "GET / HTTP/1.1" 200 45
[Tue Oct 08 14:31:35.664386 2024] [mpm_event:notice] [pid 1:tid 1] AH00491: 
caught SIGTERM, shutting down
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
[Tue Oct 08 14:31:41.995159 2024] [mpm_event:notice] [pid 1:tid 1] AH00489: 
Apache/2.4.62 (Unix) configured -- resuming normal operations
[Tue Oct 08 14:31:41.995339 2024] [core:notice] [pid 1:tid 1] AH00094: Command 
line: 'httpd -D FOREGROUND'
172.17.0.1 - - [08/Oct/2024:14:30:24 +0000] "GET / HTTP/1.1" 200 45
172.17.0.1 - - [08/Oct/2024:14:30:25 +0000] "GET / HTTP/1.1" 200 45

docker logs 能够打印出自容器启动以来完整的日志,并且-f参数可以继续打印出新产生的日志,效
果上与 Linux 命令 tail -f 一样。

更多推荐