1. 程式人生 > 實用技巧 >Fluentd部署:多Workers程序模式

Fluentd部署:多Workers程序模式

介紹如何使用Fluentd的多worker模式處理高訪問量的日誌事件。此模式會執行多個worker程序以最大利用多核CPU。

  1. 原理
    預設情況下,一個Fluentd例項會執行一個監控程序和一個工作程序。工作程序包含了Input/Filter/Output各類外掛。
    多worker模式就是一個例項中啟動了多個工作程序,這些工作程序負責處理日誌事件,接受監控程序的管理和排程。如下圖所示:

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的工作程序上。
這種配置可以混合使用多程序外掛和單程序外掛。

2.3 指令
Fluentd v1.4.0開始支援<worker N-M>指令。這個很容易理解。
N-M代表工作程序索引範圍,指定了外掛可以執行在哪些工作程序中。

<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.

2.4 root_dir/@id引數
使用檔案作為buffer時,需要配置這幾個引數。
在多worker模式中,不能指定固定的path作為檔案buffer,因為這會不同程序中引起衝突。

<system>
  workers 2
</system>

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

Fluentd提供了基於root_dir和@id的動態path配置,實際的buffer路徑為:${root_dir}/worker${worker index}/${plugin @id}/buffer

<system>
  workers 2
  root_dir /var/log/fluentd
</system>

<match pattern>
  @type forward
  @id out_fwd
  <buffer>
    @type file
  </buffer>
</match>
  1. 操作
    每個worker使用單獨的記憶體和磁碟空間,因此需要仔細配置快取空間,並對記憶體和磁碟使用情況做好監控。