容器的使用(四)

一 . 容器的日志

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. 还支持以下几种级别:
DriverDescription
noneNo log output for the specific container is produced
json-fileThis is default driver. The logging information is stored in files, formatted as Json
journaldIf the journals daemon is running on the host machine, we can use this driver. It forwards logging to the journald daemon
syslogdIf the syslog daemon is running on the host machine, we can configure this driver, which will forward the log messages to the syslgo daemon.
gelfWhen using the driver, log messages are written to a Graylog Extended Log Format(GELF) endpoint. Popular examples of such endpoints are Graylog and Logstash
fluentdAssuming 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

转载于:https://www.cnblogs.com/zangxueyuan/p/9142120.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值