容器的使用(四)

一 . 容器的日志

1.访问容器的日志

 

(1)访问quotes容器的日志

$ docker container logs quotes

(2)只检索前5条记录

[root@c720120 ~]# docker container logs --tail 5 quotes
{"quote":"Every adolescent has that dream every century has that dream every revolutionary has that dream, to destroy the family.","author":"Gertrude Stein","cat":"family"}
{"quote":"By all means let's be open-minded, but not so open-minded that our brains drop out.","author":"Richard Dawkins","cat":"funny"}
{"quote":"Trust me, I'm going to find out where the money has gone and how it has been spent, and see if we can't get it down there quicker to let that rebuilding start.","author":"Lynn Westmoreland","cat":"trust"}
{"quote":"You eat and sleep it all day long and play on the streets until mom calls you in. My story is no different than anybody else's.","author":"Adam Oates","cat":"mom"}
{"quote":"It's funny. All you have to do is say something nobody understands and they'll do practically anything you want them to.","author":"J. D. Salinger","cat":"funny"}

(3)使用流的方式进行检索日志

[root@c720120 ~]# docker container logs --tail 5 --follow quotes
{"quote":"Every adolescent has that dream every century has that dream every revolutionary has that dream, to destroy the family.","author":"Gertrude Stein","cat":"family"}
{"quote":"By all means let's be open-minded, but not so open-minded that our brains drop out.","author":"Richard Dawkins","cat":"funny"}
{"quote":"Trust me, I'm going to find out where the money has gone and how it has been spent, and see if we can't get it down there quicker to let that rebuilding start.","author":"Lynn Westmoreland","cat":"trust"}
{"quote":"You eat and sleep it all day long and play on the streets until mom calls you in. My story is no different than anybody else's.","author":"Adam Oates","cat":"mom"}

2. 日志驱动

Docker包括多种容器日志记录方式,这个机制又叫做logging drivers。logging driver经常在配置在Docker 守护进程级别,默认的logging driver是json-file. 还支持以下几种级别:

Driver Description
none No log output for the specific container is produced
json-file This is default driver. The logging information is stored in files, formatted as Json
journald If the journals daemon is running on the host machine, we can use this driver. It forwards logging to the journald daemon
syslogd If the syslog daemon is running on the host machine, we can configure this driver, which will forward the log messages to the syslgo daemon.
gelf When using the driver, log messages are written to a Graylog Extended Log Format(GELF) endpoint. Popular examples of such endpoints are Graylog and Logstash
fluentd Assuming that the fluentd daemon is installed on the host system, this driver wrties logs messages to it.

 

 

3.  在启动容器时,指定使用的logging driver 案例

(1) 运行busybox容器,且使用—log-driver参数去指定Logging driver的none.

[root@c720120 ~]# docker container run --name test -it
> --log-driver none
> busybox sh -c 'for N in 1 2 3; do echo "Hello $N"; done'
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
07a152489297: Pull complete
Digest: sha256:141c253bc4c3fd0a201d32dc1f493bcf3fff003b6df416dea4f41046e0f37d47
Status: Downloaded newer image for busybox:latest
Hello 1
Hello 2
Hello 3

 

(2)获取先前容器的日志

[root@c720120 ~]# docker container logs test
Error response from daemon: configured logging driver does not support reading

 

(3)从第(2)步可以看到,符合我们的预期,然后删除实验的容器

[root@c720120 ~]# docker container rm test
test

 

4. 改变默认的logging driver

(1)编辑以下文件

[root@c720120 ~]# vi /etc/docker/daemon.json

{
   "Log-driver": "json-log",
   "log-opts": {
     "max-size": "10m",
     "max-file": 3
   }
}

(2) 重新启动docker的进程,让配置进行生效。

[root@c720120 ~]#  sudo kill -SIGHUP $(pidof dockerd)

注意:可能由于docker版本不一样,导致要修改的地方不一样,本实例所使用的版本信息

Docker version 18.05.0-ce, build f150324。

二. 容器的解剖

1. 架构如下图所示

image

Talent without working hard is nothing.
原文地址:https://www.cnblogs.com/zangxueyuan/p/9142120.html