本文介绍使用Fluentd收集standalone容器日志的方法。Docker提供了很多logging driver,默认情况下使用的json-file,它会把容器打到stdout/stderr的日志收集起来存到json文件中,docker logs所看到的日志就是来自于这些json文件。当有多个docker host的时候你会希望能够把日志汇集起来,集中存放到一处,本文讲的是如何通过fluentd logging driver配合fluentd来达成这一目标。目标:将standalone容器打到stdout/stderror的日志收集起来收集的日志根据容器名分开存储日志文件根据每天滚动第一步:配置Fluentd实例首先是配置文件fluent.conf:<source> @type forward</source><match *> @type file path /fluentd/log/${tag}/${tag} append true <format> @type single_value message_key log </format> <buffer tag,time> @type file timekey 1d timekey_wait 10m flush_mode interval flush_interval 30s </buffer></match>新建一个目录比如/home/ubuntu/container-logs,并赋予权限chmod 777 /home/ubuntu/container-logs。然后启动Fluentd实例,这里使用的Docker方式:docker run -it \ -d \ -p 24224:24224 \ -v /path/to/conf/fluent.conf:/fluentd/etc/fluent.conf \ -v /home/ubuntu/container-logs:/fluentd/log fluent/fluentd:v1.3第二步:指定容器的logging driver在启动容器的时候执行使用fluentd作为logging driver:docker run -d \ … –log-driver=fluentd \ –log-opt fluentd-address=<fluentdhost>:24224 \ –log-opt mode=non-blocking \ –log-opt tag={{.Name}} \ <image>第三步:观察日志到/home/ubuntu/container-logs目录下能够看到类似这样的目录结构:.└── <container-name> └── <container-name>.20190123.log参考文档Configure logging driversCustomize log driver outputUse Fluentd logging driverDocker CLI - runFluentdFluentd - out_fileFluentd - formatter_single_valueFluentd - buf_fileFluentd - buffer