使用Fluentd收集Docker容器日志

栏目: 编程工具 · 发布时间: 7年前

内容简介:本文介绍使用Fluentd收集standalone容器日志的方法。Docker提供了很多当有多个docker host的时候你会希望能够把日志汇集起来,集中存放到一处,本文讲的是如何通过

本文介绍使用Fluentd收集standalone容器日志的方法。

Docker提供了很多 logging driver ,默认情况下使用的 json-file ,它会把容器打到stdout/stderr的日志收集起来存到json文件中, docker logs 所看到的日志就是来自于这些json文件。

当有多个docker host的时候你会希望能够把日志汇集起来,集中存放到一处,本文讲的是如何通过 fluentd logging driver 配合 fluentd 来达成这一目标。

目标:

  1. 将standalone容器打到stdout/stderror的日志收集起来
  2. 收集的日志根据容器名分开存储
  3. 日志文件根据每天滚动

第一步:配置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

参考文档


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Foundations of PEAR

Foundations of PEAR

Good, Nathan A./ Kent, Allan / Springer-Verlag New York Inc / 2006-11 / $ 50.84

PEAR, the PHP Extension and Application Repository, is a bountiful resource for any PHP developer. Within its confines lie the tools that you need to do your job more quickly and efficiently. You need......一起来看看 《Foundations of PEAR》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

html转js在线工具
html转js在线工具

html转js在线工具