使用Fluentd收集Docker容器日志

本文介绍使用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 drivers
Customize log driver output
Use Fluentd logging driver
Docker CLI – run

Fluentd

Fluentd – out_file
Fluentd – formatter_single_value
Fluentd – buf_file
Fluentd – buffer

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理