Know container log (1) Docker logs & logging driver

Benpian has joined " the .NET Core ON K8S study and practice series index ", you can click to see more container technology related series of articles. Monitoring and logging has always been the key to stable operation and troubleshooting systems, micro Services Architecture, the number of containers as well as many fast-changing set of features that make centralized log management system becomes a part of the production environment can not be acquired . We will focus on this topic in the log management herein, this will introduce Docker's own logs as well as its sub-commands Logging driver.

A, Docker logs subcommand

   By default, Docker logs are sent to the standard output device containers (STDOUT) and the standard error device (STDERR), wherein the console is actually STDOUT and STDERR of the containers.

  We can command to view the log output to a specific container by logs child:

docker logs edc-k8s-demo

   This time to see the logs are static, so far off the log. If you want to continue to see the new print out the log information, you can add -f parameter, such as:

docker logs -f edc-k8s-demo

二、Docker logging driver

  We just learned default configuration, Docker logs will be sent to STDOUT and STDERR. But in fact, Docker also provides some other mechanism allows us to extract logs from the operation of the vessel, these mechanisms collectively referred to as logging driver.

  For Docker, its default logging driver is json-file, if not specified at startup will use the default logging driver.

  We json-file name will see in the console log by docker logs are stored in a json file, we can find this container json file directory on the server in the Host.

容器日志路径:/var/lib/docker/containers/<container-id>/<container-id>-json.log

  For example, we can see a exceptionless-api json container logs:

  A quick method to view the log file path of a container:

docker inspect exceptionless_api_1

  By inspect command, can be found in a series of configuration information and ID of the container, we can focus on LogPath:

  查到LogPath后,即可复制其后面的日志路径了,打开这个json文件你就可以看到输出的容器日志了。

  除了json-file,Docker还支持以下多种logging dirver,来源:Configure logging drivers

  其中,none 代表禁用容器日志,不会输出任何容器日志。

  其他几个logging driver解释如下:

  • syslog 与 journald 是Linux上的两种日志管理服务
  • awslog、splunk 与 gcplogs是第三方日志托管服务
  • gelf 与 fluentd 是两种开源的日志管理方案

  我们可以在容器启动时通过加上 --log-driver 来指定使用哪个具体的 logging driver,例如:

docker run -d --log-driver=syslog ......

  如果想要设置默认的logging driver,那么则需要修改Docker daemon的启动脚本,例如:

{
  "log-driver": "json-file",
  "log-opts": {
    "labels": "production_status",
    "env": "os,customer"
  }
}

  每个logging driver都有一些自己特定的log-opt,使用时可以参考具体官方文档。

三、小结

  本文介绍了Docker自带的logs子命令以及logging driver,默认的logging driver是json-file,当然Docker还支持多个不同机制的logging dirver,我们可以根据自己的需要在使用时进行指定。下一篇,我们会学习流行的开源日志管理三兄弟ELK(ElasticSearch、Logstash、Kibana)。

Elastic Stack

Guess you like

Origin yq.aliyun.com/articles/727546