天天看點

12. Fluentd部署:多Workers程序模式

介紹如何使用Fluentd的多worker模式處理高通路量的日志事件。此模式會運作多個worker程序以最大利用多核CPU。

  1. 原理

    預設情況下,一個Fluentd執行個體會運作一個監控程序和一個工作程序。工作程序包含了Input/Filter/Output各類插件。

    多worker模式就是一個執行個體中啟動了多個工作程序,這些工作程序負責處理日志事件,接受監控程序的管理和排程。如下圖所示:

12. Fluentd部署:多Workers程式模式

Fluentd提供了一些特性以支援多worker模式,我們通過配置就能友善地使用這些特性。

  1. 配置

    2.1 workers參數

可在中設定工作程序的數目。

<system>
  workers 4
</system>
           

2.2 指令

有些插件不支援在多worker上運作,比如tail。

對這類插件,我們可通過

<worker N>

指定其在哪個worker上運作。

N代表worker的索引,起始為0.

<system>
  workers 4
</system>
# work on multi process workers. worker0 - worker3 run in_forward
<source>
  @type forward
</source>
# work on only worker 0. worker1 - worker3 don't run in_tail
<worker 0>
  <source>
    @type tail
  </source>
</worker>
# <worker 1>, <worker 2> or <worker 3> is also ok
           

這個例子中,啟動了4個工作程序。tail插件被放置在<worker 0>中,表明tail隻運作在索引為0的工作程序上。

這種配置可以混合使用多程序插件和單程序插件。

<system>
  workers 6
</system>

<worker 0-1>
  <source>
    @type forward
  </source>

  <filter test>
    @type record_transformer
    enable_ruby
    <record>
      worker_id ${ENV['SERVERENGINE_WORKER_ID']}
    </record>
  </filter>

  <match test>
    @type stdout
  </match>
</worker>
# work on worker 0 and worker 1.

<worker 2-3>
  <source>
    @type tcp
    <parse>
      @type none
    </parse>
    tag test
  </source>

  <filter test>
    @type record_transformer
    enable_ruby
    <record>
      worker_id ${ENV['SERVERENGINE_WORKER_ID']}
    </record>
  </filter>

  <match test>
    @type stdout
  </match>
</worker>
# work on worker 2 and worker 3.

<worker 4-5>
  <source>
    @type udp
    <parse>
      @type none
    </parse>
    tag test
  </source>

  <filter test>
    @type record_transformer
    enable_ruby
    <record>
      worker_id ${ENV['SERVERENGINE_WORKER_ID']}
    </record>
  </filter>

  <match test>
    @type stdout
  </match>
</worker>
# work on worker 4 and worker 5.
           
<system>
  workers 2
</system>

<match pattern>
  @type forward
  <buffer>
    @type file
    path /var/log/fluentd/forward # This is not allowed
  </buffer>
</match>
           
<system>
  workers 2
  root_dir /var/log/fluentd
</system>

<match pattern>
  @type forward
  @id out_fwd
  <buffer>
    @type file
  </buffer>
</match>
           
  1. 操作

    每個worker使用單獨的記憶體和磁盤空間,是以需要仔細配置緩存空間,并對記憶體和磁盤使用情況做好監控。