Fluentd 日志驱动

fluentd 日志驱动程序将容器日志作为结构化日志数据发送到 Fluentd 收集器。然后,用户可以使用 Fluentd 的各种输出插件 将这些日志写入各种目的地。

除了日志消息本身,fluentd 日志驱动程序还在结构化日志消息中发送以下元数据:

FieldDescription
container_idThe full 64-character container ID.
container_nameThe container name at the time it was started. If you use docker rename to rename a container, the new name isn't reflected in the journal entries.
sourcestdout or stderr
logThe container log

用法

一些选项可以通过根据需要多次指定--log-opt来支持:

  • fluentd-address: 指定一个套接字地址以连接到Fluentd守护进程,例如 fluentdhost:24224unix:///path/to/fluentd.sock
  • tag: 为Fluentd消息指定一个标签。支持一些Go模板标记,例如 {{.ID}}, {{.FullID}}{{.Name}} docker.{{.ID}}

要将fluentd驱动程序设置为默认的日志记录驱动程序,请在daemon.json文件中将log-driverlog-opt键设置为适当的值,该文件位于Linux主机上的/etc/docker/或Windows Server上的C:\ProgramData\docker\config\daemon.json。有关使用daemon.json配置Docker的更多信息,请参阅daemon.json

以下示例将日志驱动程序设置为 fluentd 并设置 fluentd-address 选项。

{
  "log-driver": "fluentd",
  "log-opts": {
    "fluentd-address": "fluentdhost:24224"
  }
}

重启Docker以使更改生效。

注意

log-opts 配置文件中的配置选项在 daemon.json 配置文件中必须以字符串形式提供。因此,布尔值和数值(例如 fluentd-asyncfluentd-max-retries 的值)必须用引号(")括起来。

要为特定容器设置日志驱动程序,请将--log-driver选项传递给docker run

$ docker run --log-driver=fluentd ...

在使用此日志驱动程序之前,请启动一个Fluentd守护进程。日志驱动程序默认通过localhost:24224连接到该守护进程。使用fluentd-address选项可以连接到不同的地址。

$ docker run --log-driver=fluentd --log-opt fluentd-address=fluentdhost:24224

如果容器无法连接到Fluentd守护进程,除非使用了fluentd-async选项,否则容器会立即停止。

选项

用户可以使用--log-opt NAME=VALUE标志来指定额外的Fluentd日志驱动程序选项。

fluentd-address

默认情况下,日志驱动程序连接到 localhost:24224。提供 fluentd-address 选项以连接到不同的地址。支持 tcp(默认)和 unix 套接字。

$ docker run --log-driver=fluentd --log-opt fluentd-address=fluentdhost:24224
$ docker run --log-driver=fluentd --log-opt fluentd-address=tcp://fluentdhost:24224
$ docker run --log-driver=fluentd --log-opt fluentd-address=unix:///path/to/fluentd.sock

上述两个指定了相同的地址,因为 tcp 是默认的。

标签

默认情况下,Docker使用容器ID的前12个字符来标记日志消息。 请参阅 日志标签选项文档以自定义 日志标签格式。

labels, labels-regex, env, 和 env-regex

labelsenv 选项各自接受一个逗号分隔的键列表。如果 labelenv 键之间存在冲突,env 的值将优先。这两个选项都会向日志消息的额外属性中添加额外的字段。

env-regexlabels-regex 选项分别与 envlabels 相似且兼容。它们的值是用于匹配日志相关环境变量和标签的正则表达式。它用于高级的 日志标签选项

fluentd-async

Docker 在后台连接到 Fluentd。消息会被缓冲,直到连接建立。默认为 false

fluentd-async-reconnect-interval

当启用fluentd-async时,fluentd-async-reconnect-interval选项定义了重新建立与fluentd-address连接的间隔时间,单位为毫秒。如果地址解析为一个或多个IP地址(例如Consul服务地址),此选项非常有用。

fluentd-buffer-limit

设置内存中缓冲的事件数量。记录将存储在内存中,直到达到此数量。如果缓冲区已满,记录日志的调用将失败。 默认值为1048576。 ( https://github.com/fluent/fluent-logger-golang/tree/master#bufferlimit)

fluentd-retry-wait

重试之间等待多长时间。默认为1秒。

fluentd-max-retries

最大重试次数。默认为4294967295 (2**32 - 1)。

fluentd-sub-second-precision

生成纳秒分辨率的事件日志。默认为false

使用Docker管理Fluentd守护进程

关于Fluentd本身,请参见 项目网页其文档

要使用此日志驱动程序,请在主机上启动fluentd守护进程。我们建议您使用 Fluentd docker 镜像。如果您想要在每个主机上聚合多个容器日志,然后再将日志传输到另一个Fluentd节点以创建聚合存储,此镜像特别有用。

测试容器日志记录器

  1. 编写一个配置文件(test.conf)来转储输入日志:

    <source>
      @type forward
    </source>
    
    <match *>
      @type stdout
    </match>
  2. 使用此配置文件启动Fluentd容器:

    $ docker run -it -p 24224:24224 -v /path/to/conf/test.conf:/fluentd/etc/test.conf -e FLUENTD_CONF=test.conf fluent/fluentd:latest
    
  3. 使用fluentd日志驱动程序启动一个或多个容器:

    $ docker run --log-driver=fluentd your/application