• Docker:Fluentd收集容器日志


    快速启动Fluentd

    1、创建日志文件和配置文件

    mkdir /home/fluentd/container-logs  /home/fluentd/conf/

    2、编写配置文件

    vi /home/fluentd/conf/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>

    3、启动Fluentd

    # docker run -d -p 24224:24224  -v /home/fluentd/container-logs:/fluentd/log -v /home/fluentd/conf/fluent.conf:/fluentd/etc/fluent.conf    fluent/fluentd

    此时会在宿主机的/home/fluentd/container-logs目录下生成容器日志,所有收集到的日志文件将存储至此。

    启动其他容器时指定容器的logging driver

    在启动容器的时候执行使用fluentd作为logging driver,下面以启动nginx容器举例:

    # docker run -d --log-driver fluentd --log-opt fluentd-address=localhost:24224 --log-opt tag="nginx-test" --log-opt fluentd-async-connect --name nginx-test -p 8080:80 nginx
    
    --log-driver: 配置log驱动
    --log-opt: 配置log相关的参数
    tag:配置match tag fluentd-address: fluentd服务地址 fluentd-async-connect:fluentd-docker异步设置,避免fluentd挂掉之后导致Docker容器也挂了

    Fluentd配置

    配置文件用于让用户控制Fluentd输入(input)和输出(output)的行为:(1) 选择输入和输出插件,(2) 指定插件参数。配置文件要求必须存在以使Fluentd正常工作。

    字符编码

    Fluentd假设配置文件编码为UTF-8或ASCII。

    指令列表

    配置文件由下列指令组成:

    1. source 指令确定输入源。
    2. match 指令确定输出目的地。
    3. filter 指令确定事件处理管道。
    4. system 指令设置系统级配置。
    5. label 指令将output和filter分组以进行内部路由。
    6. @include 指令用于包括其它文件。

    配置示例 Step by Step

    1. source: 所有数据的来源

    选择和配置Fluentd的输入源,需要用source指令。Fluentd的标准输入插件包括http和forward。http监听HTTP端口,并接受从HTTP来的日志消息。forward让fluentd监听TCP端口,并接受TCP包。当然,两者可以同时开启(可以添加任意多需要的源)。

    # Receive events from 24224/tcp
    # This is used by log forwarding and the fluent-cat command
    <source>
      @type forward
      port 24224
    </source>
    
    # http://this.host:9880/myapp.access?json={"event":"data"}
    <source>
      @type http
      port 9880
    </source>

    每个source指令必须包含 @type 参数。@type 参数指定了何种输入插件将被使用。

    插播一下:Routing

    source 将事件提交到Fluentd的路由引擎。每个事件由3部分实体组成:tag, time 和 record。tag 是由’.'分隔的字符串(如:myapp.access),并且作为Fluentd内部路由的指示。time字段由 input 插件指定,并且必须得是Unix时间格式。record 是一个JSON对象。

    2. match: 告诉 fluentd 该干什么!

    match 指令查找所有具有匹配tag的事件,并且进行处理。match 指令最常见的用法是将事件输出到其它系统(因此,对应于match指令的插件被称为"output"插件)。Fluentd的标准输出插件包括 file 和 forward。现在来加入到配置文件中。

    # Receive events from 24224/tcp
    # This is used by log forwarding and the fluent-cat command
    <source>
      @type forward
      port 24224
    </source>
    
    # http://this.host:9880/myapp.access?json={"event":"data"}
    <source>
      @type http
      port 9880
    </source>
    
    # Match events tagged with "myapp.access" and
    # store them to /var/log/fluent/access.%Y-%m-%d
    # Of course, you can control how you partition your data
    # with the time_slice_format option.
    <match myapp.access>
      @type file
      path /var/log/fluent/access
    </match>

    每个 match 指令必须包括一个匹配模板和一个 @type 参数。只有tag 匹配到模板的事件会被送到输出目的地(在上述示例中,只有tag为"myapp.access"的事件会被匹配)。@type 参数指定了将要使用的 output 插件。

    3. filter: 事件处理管道

    filter 指令与 match 有相同的语法,但 filter 可以将处理串成管道。 使用 filter 时事件流看起来会像这样:

    Input -> filter 1 -> ... -> filter N -> Output

    现在添加一个标准的 record_transformer filter 到刚刚写的 match 示例中。

    # http://this.host:9880/myapp.access?json={"event":"data"}
    <source>
      @type http
      port 9880
    </source>
    
    <filter myapp.access>
      @type record_transformer
      <record>
        host_param "#{Socket.gethostname}"
      </record>
    </filter>
    
    <match myapp.access>
      @type file
      path /var/log/fluent/access
    </match>

    收到事件 {“event”: “data”} 后,先扔到filter: record_transformer中,record_transformer 在事件中添加了"host_param" 字段并且继续向后传,{“event”:“data”,“host_param”:“webserver1”},事件最终到达 file 输出插件。

    match 模板

    通配符和展开

    下列 match 模板可以用于<match> 和<filter>中的tag:

    • * 匹配单个tag部分。
      • 例如:模板 a.* 匹配 a.b,但不能匹配 a 与 a.b.c
    • ** 匹配0个或多个tag 部分
      • 例如:a.** 匹配a, a.b 和 a.b.c
    • {X,Y,Z} 匹配X, Y, 或 Z, 这里 X, Y 和 Z都是匹配模板
      • 例如:模板 {a,b} 匹配 a 和 b,但不匹配 c
      • 这种方法可以用于组合 * 和 ** 模板。例如: a.{b,c}.* 和 a.{b,c.**}
    • 当多个模板同时在单个tag中列出时(由一个或多个空格分隔),将匹配所列出的任意一个模板。例如:
      • 模板 <match a b> 匹配 a 和 b
      • 模板 <match a.** b.*> 匹配 a, a.b, a.b.c (匹配到前一个模板)和 b.d (匹配到后一个模板)

    匹配顺序

    Fluentd 尝试以模板在配置文件中出现的顺序进行匹配。所以看下面的例子:

    # ** matches all tags. Bad :(
    <match **>
      @type blackhole_plugin
    </match>
    
    <match myapp.access>
      @type file
      path /var/log/fluent/access
    </match>

    上面例子中,myapp.access 永远都匹配不到。更宽松的匹配模板应定义在更严格的匹配模板之后。像这样:

    <match myapp.access>
      @type file
      path /var/log/fluent/access
    </match>
    
    # Capture all unmatched tags. Good :)
    <match **>
      @type blackhole_plugin
    </match>

    当然,如果有两个同样的模板,后一个match将永远无法匹配。如果你希望将事件发送到多个输出,应考虑使用 out_copy 插件。

    常见的陷阱是将<filter>放到<match>之后。由于前面说的原因,事件将无法到达filter,也就无法按预期的方式工作。

    COPY 输出插件

    out_copy 是Fluentd的核心插件,无须另外安装,out_copy 将事件复制多份,分别送到多个输出插件中去。直接来个例子:

    <match pattern>
      @type copy
      <store>
        @type file
        path /var/log/fluent/myapp1
        ...
      </store>
      <store>
        ...
      </store>
      <store>
        ...
      </store>
    </match>

    下面的例子将事件存到本地文件夹/var/log/fluent/myapp下,同时发送到Elasticsearch实例的fluentd.test 集合中。具体请参见out_fileout_elasticsearch插件的细节说明。

    <match docker.*>
      @type copy
      <store>
        @type file
        path /var/log/fluent/myapp
        compress gzip
        <format>
          localtime false
        </format>
        <buffer time>
          timekey_wait 10m
          timekey 86400
          timekey_use_utc true
          path /var/log/fluent/myapp
        </buffer>
        <inject>
          time_format %Y%m%dT%H%M%S%z
          localtime false
        </inject>
      </store>
      <store>
        @type elasticsearch
        host 192.168.198.46
        port 9200
        index_name fluentd
        type_name test
      </store>
    </match>

    启动一个docker,并将log-driver指向fluentd,即可将日志输出到fluentd,再转发至elasticsearch,同时本地留有日志备份。

    docker run --rm --log-driver fluentd --log-opt fluentd-address=192.168.198.46:24224 --log-opt tag=docker.test alpine date

    out_copy 参数

    • @type - 此值必须为"copy"

    • deep_copy - 深度复制,默认为false,out_copy在各store插件间共享事件记录。当此值为true时,out_copy为每一store插件复制一份事件记录。

    • <store> 字段 - 指定存储目的地。格式与<match>指令一致。此字段必须至少出现一次。

    • ignore_error - 忽略错误。当某一store出现错误时,这个参数将作用于其余store,例如:

      <match app.**>
        @type copy
        <store>
          @type plugin1
        </store>
        <store>
          @type plugin2
        </store>
      </match>
    • 若plugin1出现错误时,plugin2将不会被执行。若希望忽略不重要的store出现的错误时,可以指定该store的ignore_error参数。

      <match app.**>
        @type copy
        <store ignore_error>
          @type plugin1
        </store>
        <store>
          @type plugin2
        </store>
      </match>
  • 相关阅读:
    C语言探索之旅 | 第二部分第十一课:练习题和习作
    C语言探索之旅 | 第二部分第十课: 实战"悬挂小人"游戏答案
    C语言探索之旅 | 第二部分第九课: 实战"悬挂小人"游戏
    C语言探索之旅 | 第二部分第八课:动态分配
    C语言探索之旅 | 第二部分第七课:文件读写
    最近迫切应学的编程语言
    C语言探索之旅 | 第二部分第五课:预处理
    封装axios方法之一
    react前置路由守卫
    React Router 4.0 实现路由守卫
  • 原文地址:https://www.cnblogs.com/-wenli/p/14047206.html
Copyright © 2020-2023  润新知