docker日志管理

279 阅读2分钟

docker日志管理

  • docker日志分docker引擎日志和docker容器日志,引擎日志不太关注(yum安装的journalctl命令就能看),重点关注docker容器日志
  • docker通过各种引擎管理容器日志,默认引擎是 JSON File logging driver。

官方介绍:

  • By default, Docker captures the standard output (and standard error) of all your containers, and writes them in files using the JSON format. The JSON format annotates each line with its origin (stdout or stderr) and its timestamp. Each log file contains information about only one container. 默认docker抓取所有容器的标准输出和错误输出,通过json格式写入文件。json格式声明了原始日志是标准输出或者错误输出,还有时间戳。一个日志文件对用一个容器。

下面展示了一条日志json格式化后的样子

image.png

  • 日志驱动可以手动指定,配置文件是 /etc/docker/daemon.json
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3" 
  }
}
  • log-driver 指定日志驱动是json-file

  • log-opts 配置驱动的一些参数 主要就是max-size和max-file,不然容器不重启日志越来越大,服务器磁盘就蹦了。

  • max-size 意思单个日志最大值,超过最大值的就切割了,单位k,m,g

  • max-file 意思最对切割多少个日志,超过这个值就会把之前的日志删除掉

  • 官网复制的详细参数配置

OptionDescriptionExample value
max-sizeThe maximum size of the log before it is rolled. A positive integer plus a modifier representing the unit of measure (km, or g). Defaults to -1 (unlimited).--log-opt max-size=10m
max-fileThe maximum number of log files that can be present. If rolling the logs creates excess files, the oldest file is removed. Only effective when max-size is also set.  A positive integer. Defaults to 1.--log-opt max-file=3
labelsApplies when starting the Docker daemon. A comma-separated list of logging-related labels this daemon accepts. Used for advanced log tag options.--log-opt labels=production_status,geo
labels-regexSimilar to and compatible with labels. A regular expression to match logging-related labels. Used for advanced log tag options.`--log-opt labels-regex=^(production_statusgeo)`
envApplies when starting the Docker daemon. A comma-separated list of logging-related environment variables this daemon accepts. Used for advanced log tag options.--log-opt env=os,customer
env-regexSimilar to and compatible with env. A regular expression to match logging-related environment variables. Used for advanced log tag options.`--log-opt env-regex=^(oscustomer)`
compressToggles compression for rotated logs. Default is disabled.--log-opt compress=true