3.2、Docker八年老兵日志实战

    在 Docker 的整个生命周期中,超过 70%的时间我们都是在做 Docker 的运维工作。 查看Docker日志,是运维工作中非常重要的一个环节。

    在 Docker 的整个生命周期中,超过 70%的时间我们都是在做 Docker 的运维工作。 查看Docker日志,是运维工作中非常重要的一个环节。

1、访问Docker引擎的日志

    Docker引擎的日志是指,在Docker守护进程执行过程中产生的日志信息。如果 Docker引擎出现了问题,则需要在不同操作系统的宿主机上使用不同的方式进行查看。

    下表列举了不同操作系统上Docker引擎日志的位置

操作系统日志位置
CentOS7journalctl -u docker.service
RHEL7journalctl -u docker.service
Ubuntu(16.04)journalctl -u docker.service
Ubuntu(14.04)/var/log/upstart/docker.log
OpenSuSEjournalctl -u docker.service
Boot2Docker/var/log/docker.log

    以centos7-7宿主机为例操作系统是CentOS7(64位)因此可以使用以下命令查看 Docker引 擎的日志。为了方便查看,可以使用Linux的管道进行分页显示

[root@centos7-7 ~]# journalctl -u docker.service | more
-- Logs begin at 四 2023-09-21 04:35:30 CST, end at 四 2023-11-23 20:54:29 CST. --
11月 23 20:54:21 centos7-7 systemd[1]: Starting Docker Application Container Engine...
11月 23 20:54:21 centos7-7 dockerd-current[4357]: time="2023-11-23T20:54:21.519257551+08:00" level=warning msg="could not change group
 /var/run/docker.sock to docker: group docker not found"
11月 23 20:54:21 centos7-7 dockerd-current[4357]: time="2023-11-23T20:54:21.521587035+08:00" level=info msg="libcontainerd: new contai
nerd process, pid: 4363"
11月 23 20:54:22 centos7-7 dockerd-current[4357]: time="2023-11-23T20:54:22.682058757+08:00" level=info msg="Graph migration to conten
t-addressability took 0.00 seconds"
11月 23 20:54:22 centos7-7 dockerd-current[4357]: time="2023-11-23T20:54:22.683286947+08:00" level=info msg="Loading containers: start
."

2、访问 Docker应用的日志

    Docker将应用运行在容器中,应用输出日志也就输出到容器中了,访问 Docker 应用 的日志也就变成了访问Docker 容器的日志。要访问容器的日志,首先需要了解 Docker 的 日志引擎。下表列出了Docker支持的日志引擎。

日志引擎说明
journaldDocker默认的日志引擎。这种引擎把所有容器的回显日志输出到系统 的journald服务中
json-file把每个容器的回显日志以JSON文件格式输出到每个容器的内部。如果 在实际应用中某些应用产生了大量的日志信息,则可能导致容器的 JSON日志文件过大而占满宿主机的磁盘
syslog把所有容器的回显日志输出到系统的syslog服务中
 fluentd把所有容器的回显日志输出到系统的fuentd 服务中
gelf把所有容器的日志输出到支持GELF(Graylog Extended Log Format)格 式的服务中,如Logstash
none关闭Docker容器的日志。使用这种方式,则意味着无法通 过“dockerlogs”命令查看任何容器输出的日志

    利用以下命令可以查看Docker默认的日志引擎 

[root@centos7-7 ~]# docker info | grep Logging
  WARNING: You're not using the default seccomp profile
Logging Driver: journald

    修改Docker默认的日志引擎

(1)修改文件“/etc/sysconfig/docker”,删除“--log-driver=journald”

(2)修改文件“/etc/docker/daemon.json”的内容,输入以下配置。

[root@centos7-7 ~]# vim /etc/docker/daemon.json
{
 "log-driver": "json-file"
}

(3)重新加载Docker 的服务,并重启 Docker。

[root@centos7-7 ~]# systemctl daemon-reload 
[root@centos7-7 ~]# systemctl restart docker.service

(4)查看Docker的日志引擎。此时可以看到Docker 的日志引擎被修改为json-file

[root@centos7-7 ~]# docker info | grep Logging
  WARNING: You're not using the default seccomp profile
Logging Driver: json-file

(5)启动一个容器进行简单的测试,并进入这个容器的目录,可以看到这时容器的日志默认以 JSON 的格式存 储于“/var/lib/docker/containers//”目录下。

[root@centos7-7 ~]# docker run -d -p 1234:80 nginx 
4bc5a4d36c290d77bed517fae15ee13e593b51296004692ed073e3dbdd6fdd41
[root@centos7-7 ~]# cd /var/lib/docker/containers/4bc5a4d36c290d77bed517fae15ee13e593b51296004692ed073e3dbdd6fdd41/
[root@centos7-7 4bc5a4d36c290d77bed517fae15ee13e593b51296004692ed073e3dbdd6fdd41]# ls
4bc5a4d36c290d77bed517fae15ee13e593b51296004692ed073e3dbdd6fdd41-json.log  config.v2.json   hostname  resolv.conf       secrets
checkpoints                                                                hostconfig.json  hosts     resolv.conf.hash  shm

3、使用“docker logs”命令查看容器的日志 

但是并不推荐直接读取Docker日志的内容,原因是Docker提供了“Docker logs”命令来帮助我们读取日志的信息,下面是Docker logs的帮助信息

[root@centos7-7 ~]# docker help logs

Usage:	docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --help           Print usage
      --since string   Show logs since timestamp
      --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps

    使用“docker logs”命令查看容器的日志

(1)查看指定时间后的日志,只显示最后5行

[root@centos7-7 ~]# docker logs -f -t --since="2023-11-23" --tail=5 4bc5a4d36c29
2023-11-23T13:08:32.065374038Z 2023/11/23 13:08:32 [notice] 1#1: OS: Linux 3.10.0-1160.71.1.el7.x86_64
2023-11-23T13:08:32.065376849Z 2023/11/23 13:08:32 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023-11-23T13:08:32.065424602Z 2023/11/23 13:08:32 [notice] 1#1: start worker processes
2023-11-23T13:08:32.066667431Z 2023/11/23 13:08:32 [notice] 1#1: start worker process 11
2023-11-23T13:08:32.066688225Z 2023/11/23 13:08:32 [notice] 1#1: start worker process 12

(2)查看容器最近30分钟的日志。

[root@centos7-7 ~]# docker logs --since 30m 4bc5a4d36c29
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Ignoring /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh, not executable
/docker-entrypoint.sh: Ignoring /docker-entrypoint.d/15-local-resolvers.envsh, not executable
/docker-entrypoint.sh: Ignoring /docker-entrypoint.d/20-envsubst-on-templates.sh, not executable
/docker-entrypoint.sh: Ignoring /docker-entrypoint.d/30-tune-worker-processes.sh, not executable
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/11/23 13:08:32 [notice] 1#1: using the "epoll" event method
2023/11/23 13:08:32 [notice] 1#1: nginx/1.25.3
2023/11/23 13:08:32 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14) 
2023/11/23 13:08:32 [notice] 1#1: OS: Linux 3.10.0-1160.71.1.el7.x86_64
2023/11/23 13:08:32 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/11/23 13:08:32 [notice] 1#1: start worker processes
2023/11/23 13:08:32 [notice] 1#1: start worker process 11
2023/11/23 13:08:32 [notice] 1#1: start worker process 12

(3)查看某个时间之后的所有日志

[root@centos7-7 ~]# docker logs -t --since="2023-11-23" 4bc5a4d36c29
2023-11-23T13:08:32.044940892Z /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
2023-11-23T13:08:32.045013637Z /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
2023-11-23T13:08:32.048525910Z /docker-entrypoint.sh: Ignoring /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh, not executable
2023-11-23T13:08:32.048548110Z /docker-entrypoint.sh: Ignoring /docker-entrypoint.d/15-local-resolvers.envsh, not executable
2023-11-23T13:08:32.048553695Z /docker-entrypoint.sh: Ignoring /docker-entrypoint.d/20-envsubst-on-templates.sh, not executable
2023-11-23T13:08:32.048558047Z /docker-entrypoint.sh: Ignoring /docker-entrypoint.d/30-tune-worker-processes.sh, not executable
2023-11-23T13:08:32.052769149Z /docker-entrypoint.sh: Configuration complete; ready for start up
2023-11-23T13:08:32.065288329Z 2023/11/23 13:08:32 [notice] 1#1: using the "epoll" event method
2023-11-23T13:08:32.065366355Z 2023/11/23 13:08:32 [notice] 1#1: nginx/1.25.3
2023-11-23T13:08:32.065370897Z 2023/11/23 13:08:32 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14) 
2023-11-23T13:08:32.065374038Z 2023/11/23 13:08:32 [notice] 1#1: OS: Linux 3.10.0-1160.71.1.el7.x86_64
2023-11-23T13:08:32.065376849Z 2023/11/23 13:08:32 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023-11-23T13:08:32.065424602Z 2023/11/23 13:08:32 [notice] 1#1: start worker processes
2023-11-23T13:08:32.066667431Z 2023/11/23 13:08:32 [notice] 1#1: start worker process 11
2023-11-23T13:08:32.066688225Z 2023/11/23 13:08:32 [notice] 1#1: start worker process 12

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值