第四部分Fluentd部署設置

1、日誌

本文介紹Fluentd的日誌記錄機制。
Fluentd有兩個日誌層:全局和每個插件。 可以為全局日誌記錄和插件級別日誌記錄設置不同的日誌級別。

1.1、Log Level

下面顯示的是支持值的列表,按照冗長的順序增加:

  • fatal

  • error

  • warn

  • info

  • debug

  • trace

默認日誌級別為info,Fluentd默認輸出 info, warn, error and fatal 日誌.

1.2、全局日誌

Fluentd core和插件使用全局日誌記錄就不能設置自己的日誌級別。可以向上或者向下調整全局日誌級別。

通過命令行選項

增加詳細程度

-v選項設置要調試(debug)的詳細程序

-vv選項將詳細程度設置為追蹤(trace)

Advertisements

$ fluentd -v ... # debug level$ fluentd -vv ... # trace level

這些選項對於調試目的很有用。

降低日誌精度

-q選項將verbosity設置為warn

-qq選項將verbosity設置為error。

$ fluentd -q ... # warn level$ fluentd -qq ... # error level

使用配置文件

您也可以使用下面的配置文件中的部分更改日誌記錄級別。

<system> # equal to -qq option log_level error</system>
1.3、每個插件日誌

log_level選項為每個插件設置不同級別的日誌記錄。它可以在每個插件的配置文件中設置。

Advertisements

例如,為了調試in_tail但是抑制所有fatal的日誌消息in_http,它們各自的log_level選項應該如下設置:

<source> @type tail @log_level debug path /var/log/data.log ...</source><source> @type http @log_level fatal</source>

如果不指定log_level參數,插件將使用全局日誌級別(默認是info)。

一些插件還不支持每插件日誌記錄。 「插件開發」文章的日誌記錄部分介紹如何更新此類插件以支持新的日誌級別系統。

1.4、抑制重複堆棧跟蹤

Fluentd可以使用--suppress-repeated-stacktrace來抑制相同的堆棧跟蹤。例如,如果您將--suppress-repeated-stacktrace傳遞給fluentd:

2013-12-04 15:05:53 +0900 [warn]: fluent/engine.rb:154:rescue in emit_stream: emit transaction failed error_class = RuntimeError error = #<RuntimeError: syslog> 2013-12-04 15:05:53 +0900 [warn]: fluent/engine.rb:140:emit_stream: /Users/repeatedly/devel/fluent/fluentd/lib/fluent/plugin/out_stdout.rb:43:in `emit' [snip] 2013-12-04 15:05:53 +0900 [warn]: fluent/engine.rb:140:emit_stream: /Users/repeatedly/devel/fluent/fluentd/lib/fluent/plugin/in_object_space.rb:63:in `run'2013-12-04 15:05:53 +0900 [error]: plugin/in_object_space.rb:113:rescue in on_timer: object space failed to emit error = "foo.bar" error_class = "RuntimeError" tag = "foo" record = "{ ...}"2013-12-04 15:05:55 +0900 [warn]: fluent/engine.rb:154:rescue in emit_stream: emit transaction failed error_class = RuntimeError error = #<RuntimeError: syslog>2013-12-04 15:05:53 +0900 [warn]: fluent/engine.rb:140:emit_stream: /Users/repeatedly/devel/fluent/fluentd/lib/fluent/plugin/o/2.0.0/gems/cool.io-1.1.1/lib/cool.io/loop.rb:96:in `run' [snip]

日誌更改為:

2013-12-04 15:05:53 +0900 [warn]: fluent/engine.rb:154:rescue in emit_stream: emit transaction failed error_class = RuntimeError error = #<RuntimeError: syslog> 2013-12-04 15:05:53 +0900 [warn]: fluent/engine.rb:140:emit_stream: /Users/repeatedly/devel/fluent/fluentd/lib/fluent/plugin/o/2.0.0/gems/cool.io-1.1.1/lib/cool.io/loop.rb:96:in `run' [snip] 2013-12-04 15:05:53 +0900 [warn]: fluent/engine.rb:140:emit_stream: /Users/repeatedly/devel/fluent/fluentd/lib/fluent/plugin/in_object_space.rb:63:in `run'2013-12-04 15:05:53 +0900 [error]: plugin/in_object_space.rb:113:rescue in on_timer: object space failed to emit error = "foo.bar" error_class = "RuntimeError" tag = "foo" record = "{ ...}"2013-12-04 15:05:55 +0900 [warn]: fluent/engine.rb:154:rescue in emit_stream: emit transaction failed error_class = RuntimeError error = #<RuntimeError: syslog> 2013-12-04 15:05:55 +0900 [warn]: plugin/in_object_space.rb:111:on_timer: suppressed same stacktrace

相同的堆棧跟蹤被替換為抑制相同的堆棧跟蹤消息,直到接收到其他堆棧跟蹤。

1.5、輸出到日誌文件

Fluentd默認將日誌output到STDOUT。 要輸出到文件,請指定-o選項。

$ fluentd -o /path/to/log_file
1.6、捕獲Fluentd日誌

Fluentd用Fluent標籤標記自己的日誌。 您可以使用<matchfluent.>或<match>處理Fluentd日誌(當然會捕獲其他日誌)。 如果您在配置中定義了<matchfluent.>,則Fluentd會將自己的日誌發送到此匹配目的地。這對於監視Fluentd日誌很有用。

例如,如果您具有以下<match fluent.**>:

# omit other source / match<match fluent.**> @type stdout</match>

然後Fluentd將fluent.info日誌輸出到stdout,如下所示:

2014-02-27 00:00:00 +0900 [info]: shutting down fluentd2014-02-27 00:00:01 +0900 fluent.info: { "message":"shutting down fluentd"} # by <match fluent.**>2014-02-27 00:00:01 +0900 [info]: process finished code = 0

案例1:將Fluentd日誌發送到監控服務

您可以通過插件將Fluentd日誌發送到監控服務,例如 datadog, sentry, irc, etc.

# Add hostname for identifying the server<filter fluent.**> @type record_transformer <record> host "#{Socket.gethostname}" </record></match><match fluent.**> @type monitoring_plugin # parameters...</match>

案例2:使用聚合/監控伺服器

您可以使用out_forward將Fluentd日誌發送到監控伺服器。 監控伺服器可以過濾併發送日誌到通知系統:chat,irc, etc.等

Leaf server example:

# Add hostname for identifying the server and tag to filter by log level<filter fluent.**> @type record_transformer <record> host "#{Socket.gethostname}" original_tag ${tag} </record></match><match fluent.**> @type forward <server> # Monitoring server parameters </server></match>

監控伺服器示例:

<source> @type forward label @FLUENTD_INTERNAL_LOG</source><label @FLUENTD_INTERNAL_LOG> # Ignore trace, debug and info log <filter fluent.**> @type grep regexp1 original_tag fluent.(warn|error|fatal) </match> <match fluent.**> # your notification setup. This example uses irc plugin @type irc host irc.domain channel notify message notice: %s [%s] @%s %s out_keys original_tag,time,host,message </match></label>

如果發生錯誤,您將在irc通知通道中收到通知消息。

01:01 fluentd: [11:10:24] notice: fluent.warn [2014/02/27 01:00:00] @leaf.server.domain detached forwarding server 'server.name'

2、監控

主要是監控Fluentd守護進程

2.1監控Agent

Fluentd有一個監控Agent,通過HTTP檢索JSON中的內部指標。 請將以下行添加到您的配置文件中。

<source> @type monitor_agent bind 0.0.0.0 port 24220</source>

接下來,請重新啟動agent並通過HTTP獲取指標。

$ curl host:24220/api/plugins.json{ "plugins":[ { "plugin_id":"object:3fec669d6ac4", "type":"forward", "output_plugin":false, "config":{ "type":"forward" } }, { "plugin_id":"object:3fec669dfa48", "type":"monitor_agent", "output_plugin":false, "config":{ "type":"monitor_agent", "port":"24220" } }, { "plugin_id":"object:3fec66aead48", "type":"forward", "output_plugin":true, "buffer_queue_length":0, "buffer_total_queued_size":0, "retry_count":0, "config":{ "type":"forward", "host":"192.168.0.11" } } ]}

Reuse(重用) plugins

v0.12.17以後,monitor_agent插件有tag參數。如果設置標籤monitor.metrics,則monitor_agent插件將內部指標發送到monitor.metrics標籤。這是一個stdout輸出的例子。

2015-09-16 20:28:19 +0900 monitor.metrics: {"plugin_id":"object:3fc62f0e5d64","plugin_category":"input","type":"monitor_agent","output_plugin":false,"retry_count":null}2015-09-16 20:28:19 +0900 monitor.metrics: {"plugin_id":"object:3fc62f0e9c84","plugin_category":"output","type":"stdout","output_plugin":true,"retry_count":null}

2.2、進程監控
執行兩個ruby進程(父和子)。 請確保這些進程正在運行。 td-agent的示例如下所示。

/opt/td-agent/embedded/bin/ruby /usr/sbin/td-agent --daemon /var/run/td-agent/td-agent.pid --log /var/log/td-agent/td-agent.log

對於Linux上的td-agent,可以使用以下命令檢查進程狀態。 如果沒有問題,應該顯示兩個過程。

$ ps w -C ruby -C td-agent --no-heading32342 ? Sl 0:00 /opt/td-agent/embedded/bin/ruby /usr/sbin/td-agent --daemon /var/run/td-agent/td-agent.pid --log /var/log/td-agent/td-agent.log32345 ? Sl 0:01 /opt/td-agent/embedded/bin/ruby /usr/sbin/td-agent --daemon /var/run/td-agent/td-agent.pid --log /var/log/td-agent/td-agent.log
2.3、埠監控

Fluentd根據配置文件打開幾個埠。 我們建議您檢查這些埠的可用性。 默認埠設置如下所示:

  • TCP 0.0.0.0 9880 (HTTP by default)

  • TCP 0.0.0.0 24224 (Forward by default)

調試(debug)埠

建議進行本地通訊的調試埠進行故障排除。 請注意,以下配置將被要求。

<source> @type debug_agent bind 127.0.0.1 port 24230</source>

您可以使用Fluent-debug命令通過dRuby附加(attach)該進程。

2.4、Datadog (dd-agent) 集成

Datadog是雲監控服務,其監控代理dd-agent與Fluentd進行本地集成。

請參閱本文檔了解更多詳細信息。

3、Fluentd的信號(Signal)處理

本文解釋了Fluentd如何處理UNIX信號。

3.1、進程模式(Process Model)

當您啟動Fluentd時,它將創建兩個進程:supervisor and worker。supervisor管進程式控制制工作進程的生命周期。 請確保發送任何信號到supervisor進程。

3.2、Signals(信號)

SIGINT或SIGTERM

優雅地停止守護進程。
Fluentd將嘗試一次刷新整個內存緩衝區,但如果刷新失敗,則不會重試。 Fluentd不會刷新文件緩衝區;默認情況下,日誌將保留在磁碟上。

SIGUSR1

強制緩衝的消息被刷新並重新打開Fluentd的日誌。
Fluentd會立即刷新當前的緩衝區(內存和文件),並在flush_interval上繼續刷新。

SIGHUP

通過正常重新啟動工作進程來重新載入配置文件。
Fluentd將嘗試一次刷新整個內存緩衝區,但如果刷新失敗,則不會重試。 Fluentd不會刷新文件緩衝區;默認情況下,日誌將保留在磁碟上。

SIGCONT

調用sigdump來轉儲流暢的內部狀態。 參見故障排除文章。

4、Fluentd的HTTP RPC

4.1、概述

HTTP RPC是管理Fluentd實例的一種方式。 幾個提供的RPC是信號的替換。 響應正文是JSON格式。

信號不受支持的環境,例如 Windows,您可以使用RPC而不是信號(signals)。

4.2、配置

默認關閉RPC。 如果要啟用RPC,請在部分中設置rpc_endpoint。

<system> rpc_endpoint 127.0.0.1:24444</system>

之後,您可以訪問RPC,如下所示。

$ curl 127.0.0.1:24444/api/plugins.flushBuffers{"ok":true}
4.3、RPCs

/api/processes.interruptWorkers

更換信號的SIGINT。 停止守護進程。

/api/processes.killWorkers

更換信號的SIGTERM。 停止守護進程。

/api/plugins.flushBuffers

更換信號的SIGUSR1。 刷新緩衝的消息。

/api/config.reload

更換信號的SIGHUP。 重新載入配置。

5、Fluentd高可用性配置

高流量的網站,我們建議使用一個高可用性配置Fluentd。

5.1、消息傳遞語義(Message Delivery Semantics)

Fluentd主要用於事件日誌傳送系統。

在這樣的系統中,可以提供幾種交付保證:

  • 最多一次:消息立即轉移。 如果傳輸成功,則不再發送消息。 然而,許多故障情況可能會導致丟失的消息(例如:不再有寫入容量)

  • 至少一次:每個消息至少傳送一次。 在故障情況下,消息可能會被傳送兩次。

  • 完全一次:每個消息只傳遞一次。 這是人們想要的。

如果系統「不能丟失單個事件」,並且還必須傳輸「一次」,則系統在寫入容量不足時必須停止攝取事件。正確的方法是在不能接受事件時使用同步記錄和返回錯誤。

這就是為什麼Fluentd提供「最多一次」和「至少一次」轉移。為了收集大量數據而不影響應用程序性能,數據記錄器必須非同步傳輸數據。這可以以潛在的交付失敗為代價改善性能。

然而,大多數故障情況是可以預防的。 以下部分介紹如何設置Fluentd的拓撲以實現高可用性。

5.2、網路拓撲結構

為高可用性配置Fluentd,我們假設您的網路組成的『log forwarders(日誌轉發)』 和『logaggregators(日誌聚合)』.

「日誌轉發(log forwarders)」通常安裝在每個節點上以接收本地事件。收到一個事件后,他們通過網路將其轉發到「日誌聚合器(log aggregators)」。

'日誌聚合器(log aggregators)'是從日誌轉發器不斷接收事件的守護進程。他們緩衝事件並定期將數據上傳到雲端。

Fluentd可以作為日誌轉發器log forwarders或日誌聚合器log aggregators,具體取決於其配置。接下來的部分將介紹各自的設置。我們假設活動日誌聚合器具有ip'192.168.0.1',並且備份具有ip'192.168.0.2'。

5.3、日誌轉發Log Forwarder配置

請將以下行添加到您的配置文件中用於日誌轉發器。 這將配置您的日誌轉發器將日誌傳輸到日誌聚合器。

# TCP input<source> @type forward port 24224</source># HTTP input<source> @type http port 8888</source># Log Forwarding<match mytag.**> @type forward # primary host <server> host 192.168.0.1 port 24224 </server> # use secondary host <server> host 192.168.0.2 port 24224 standby </server> # use longer flush_interval to reduce CPU usage. # note that this is a trade-off against latency. flush_interval 60s</match>

當活動聚合器(192.168.0.1)死機時,日誌將被發送到備份聚合器(192.168.0.2)。如果兩台伺服器都死機,則日誌在相應的轉發節點上緩存在磁碟上。

5.4、Log Aggregator配置

請將以下行添加到日誌聚合器的配置文件中。 日誌傳輸的輸入源是TCP。

# Input<source> @type forward port 24224</source># Output<match mytag.**> ...</match>

傳入的日誌被緩衝,然後定期上傳到雲端。 如果上傳失敗,日誌將存儲在本地磁碟上,直到重傳成功。

5.5、失敗案例情景

Forwarder Failure轉發器故障

當日誌轉發器從應用程序接收到事件時,事件首先寫入磁碟緩衝區(由buffer_path指定)。在每次flush_interval之後,緩衝的數據將轉發到聚合器。

這個過程本質上是健壯的,可以防止數據丟失,如果日誌轉發器的Fluentd進程中斷,緩衝的數據將在重新啟動后正確傳輸到其聚合器。如果轉發器和聚合器之間的網路中斷,數據傳輸將自動重試。

然而,可能存在消息丟失場景:

  • 在接收到事件后,進程立即終止,但在將它們寫入緩衝區之前。

  • 轉發器的磁碟被破壞,文件緩衝區丟失。

聚合器Aggregator故障

當日誌聚合器接收來自日誌代理的事件時,事件首先寫入磁碟緩衝區(由buffer_path指定)。每次flush_interval之後,緩衝數據就會被上傳到雲端。
這個過程本質上是健壯的,可以防止數據丟失。如果一個日誌聚合器的fluentd進程死亡,那麼來自日誌傳送程序的數據在重新啟動后就被正確地重新傳輸。如果聚合器和雲之間的網路斷開,數據傳輸就會自動重試。

然而,可能存在消息丟失場景:

  • 在接收到事件后,進程立即終止,但在將它們寫入緩衝區之前。

  • 聚合器的磁碟被破壞,文件緩衝區丟失。

5.6、故障排除

「沒有可用節點(no nodes are available)」

請確保您可以使用TCP和UDP通信。這些命令將有助於檢查網路配置。

$ telnet host 24224$ nmap -p 24224 -sU host

請注意,有一個已知的問題,VMware偶爾會丟失用於心跳的UDP包。

6、失敗場景Failure Scenarios

本文列出了各種Fluentd失敗場景。我們假設您已經配置了高可用性的Fluentd,因此每個應用程序節點都有它的本地代理,所有的日誌都聚合為多個聚合器。

6.1、應用程序不能將記錄發布到Forwarder

在失敗場景中,應用程序在使用不同語言的日誌程序庫時,有時無法將記錄提交到本地Fluentd實例。根據每個logger庫的成熟度,已經實現了一些聰明的機制來防止數據丟失。

1)內存緩衝(可用於Ruby、Java、Python和Perl)

如果目的地Fluentd實例死機,某些logger實現將使用額外內存來保存傳入日誌。當Fluentd返回時,這些記錄器會自動將緩衝日誌發送到Fluentd。一旦達到最大緩衝區內存大小,大多數當前實現將把數據寫到磁碟上,或者刪除日誌。

  1. Exponential Backoff (available for Ruby, Java)

當嘗試將日誌重新發送到本地代理時,一些實現將使用Exponential Backoff防止過度的重新連接請求。

6.2、Forwarder or Aggregator Fluentd Goes Down(停止)

當一個Fluentd進程由於某種原因而死亡時會發生什麼?這取決於你的緩衝配置。

buf_memory

如果使用buf_memory,緩衝數據完全丟失。這是對更高性能的權衡。降低flush_interval將降低丟失數據的概率,但會增加代理和聚合器之間的傳輸量。

buf_file

如果使用buf_file,緩衝數據存儲在磁碟上。在Fluentd恢復后,它將嘗試再次將緩衝數據發送到目的地。

請注意,如果由於I / O錯誤導致緩衝區文件被破壞,數據將丟失。如果磁碟是滿的,數據也會丟失,因為沒有地方存儲磁碟上的數據。

6.3、Storage Destination Goes Down(存儲目的地掛了)

如果存儲目的地(如AmazonS3、MongoDB、HDFS等)下降,Fluentd將繼續嘗試重新發送緩存數據。重試邏輯依賴於插件的實現。

如果使用buf_memory,聚合器將停止接受新的日誌,一旦它們達到緩衝區限制。如果使用buf_file,聚合器將繼續接受日誌,直到它們耗盡磁碟空間。

7、性能調優

7.1、使用top命令檢查伺服器情況

如果Fluentd沒有您預期的那麼好或者不執行,請先檢查top命令。您需要確定系統的哪個部分是瓶頸(CPU ?內存?磁碟I /O ?等等)。

7.2、避免額外的計算

這更像是一個一般性的建議,但最好不要在Fluentd中有額外的計算。Fluentd是靈活的,可以在內部做一些事情,但是在Fluentd的配置文件中添加太多的邏輯使得它很難讀和維護,同時也使它變得不那麼健壯。配置文件應該儘可能簡單。

7.3、使用num_threads參數

如果您的日誌的目的地是一個遠程存儲或服務,那麼添加num_threads選項將并行化您的輸出(默認值為1)。此參數適用於所有輸出插件。

<match test> @type output_plugin num_threads 8 ...</match>

重要的是這個選項並不能提高處理性能,例如數值計算,突變記錄等。

7.4、使用外部gzip命令S3 /TD

Ruby有GIL(全局解釋器鎖),它只允許一次執行一個線程。雖然I /O任務可以進行多路轉換,但cpu密集型任務將阻塞其他作業。在Fluentd中的一個cpu密集型任務是壓縮。

新版本的S3 /Treasure數據插件允許在Fluentd進程之外使用gzip進行壓縮。這樣可以釋放Ruby解釋器,同時允許Fluentd處理其他任務。

# S3<match ...> @type s3 store_as gzip_command num_threads 8 ...</match># Treasure Data<match ...> @type tdlog use_gzip_command num_threads 8 ...</match>

雖然不能完美地利用多個CPU核心,但這對於大多數Fluentd部署都是有效的。和以前一樣,您也可以使用num_threads選項來運行這個選項。

7.5、減少內存使用

Ruby有多個GC參數來調優GC性能,您可以通過環境變數(這裡的參數列表)配置這些參數。為了減少內存使用,將RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR設置為較低的值。RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR用於完整的GC觸發器,默認值為2.0。引用文檔。

Do full GC when the number of old objects is more than R * N where R is this factor and N is the number of old objects just after last full GC.

因此,默認GC行為不會調用完整的GC,直到舊對象的數量達到2.0*之前。這提高了吞吐量,但增加了總內存使用量。這種設置不利於低資源環境,例如小容器。對於這種情況,可以嘗試RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR= 0.9或RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR = 1.2。

參見Ruby 2.1垃圾收集:準備生產,並在工作文章中查看和理解Ruby 2.1垃圾收集器,了解更多細節。

7.6、多進程插件

CPU常常是處理數十億傳入記錄的流entd實例的瓶頸。為了利用多個CPU核心,我們建議使用in_multiprocess插件。

in_multiprocess

8、插件管理

本文解釋了如何管理Fluentd插件,包括添加第三方插件。

8.1、fluent-gem

fluent- gem命令用於安裝Fluentd插件。這是一個圍繞gem命令的包裝器。

fluent-gem install fluent-plugin-grep

Ruby不保證其主要版本之間的C擴展API兼容性。如果更新Fluentd的Ruby版本,應該重新安裝依賴於C擴展的插件。

如果使用td-agent,使用/ usr/sbin/td-agent-gem

如果您正在使用td-agent,請確保使用td-agent的td-agent-gem命令。否則(例如,您使用屬於system、rvm等的命令),您將無法找到您的「installed」插件。
有關更多信息,請參見此FAQ。

8.2、Gem and native extension

一些插件依賴於natvie擴展庫。這意味著您需要安裝開發包來構建它,例如gcc、make、autoconf等。如果您看到如下的日誌,那麼在安裝插件之前安裝開發包。

Building native extensions. This could take a while...ERROR: Error installing fluent-plugin-twitter: ERROR: Failed to build gem native extension. /opt/td-agent/embedded/bin/ruby extconf.rbchecking for rb_str_scrub()... yescreating Makefilemake "DESTDIR = " cleansh: 1: make: not foundmake "DESTDIR = "sh: 1: make: not foundmake failed, exit code 127Gem files will remain installed in /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/string-scrub-0.0.3 for inspection.Results logged to /opt/td-agent/embedded/lib/ruby/gems/2.1.0/extensions/x86_64-linux/2.1.0/string-scrub-0.0.3/gem_make.out
8.3 「-p」 option

Fluentd的- p選項用於向載入路徑添加一個額外的插件目錄。例如,如果你輸入out_foo.rb插件進入/ path/to/plugin,您可以載入out_foo.rb插件通過指定- p選項,如下圖所示。

fluentd -p /path/to/plugin

您可以多次指定- p選項。

8.4、通過/etc/fluent/plugin添加插件

在默認情況下,Fluentd將/ etc/ fluent/plugin目錄添加到它的載入路徑。因此,在/ etc/fluent/plugin中放置的任何附加插件都將自動載入。
例如,如果/etc/fluent/plugin/out_foo.rb存在,您可以在中使用@type foo。

如果使用 td-agent, Use /etc/td-agent/plugin

如果您使用的是td-agent,Fluentd使用/etc/td-agent/plugin目錄,而不是/etc/fluent/plugin。請把你的插件放在這裡。

8.5、插件版本管理

Fluentd和插件正在進化,因此您可能會遇到最新版本的意外錯誤,例如,通過新特性的回歸,刪除已棄用的參數,更改庫依賴等。如果您想更新fluentd或插件,請首先檢查測試環境中的行為。例如,在每個版本中,td- agent修復了fluentd和plugins版本。
Fluentd插件是rubygems,rubygems默認安裝最新版本。因此,我們不建議在生產上執行以下命令:

  • gem install fluentd

  • gem install fluent-plugin-elasticsearch

  • gem update # This is very dangerous. Update all existinggems(這是非常危險的。更新所有現有的gems)

另一個問題:如果您安裝的是依賴於fluentd v0.14的插件,即使安裝了fluentd v0.12,gem也將fluentdv0.14安裝在一起。 這是Fluentd v0.12用戶意想不到的結果。

您應該使用-v選項指定目標版本。

  • gem install fluentd -v 0.12.34

  • gem install fluent-plugin-elasitcsearch -v 1.9.3

/usr/sbin/td-agent-gem也是一樣的,因為/usr/sbin/td-agent-gem內部使用gem命令。

8.6、「-gemfile」選項

一個Ruby應用程序使用Gemfile和Bundler來管理gem依賴。Fluentd—gemfile選項採用相同的方法,對於管理與共享gems分離的插件版本很有用。
例如,如果您在/etc/fluent/gemfile中跟蹤Gemfile:

source 'https://rubygems.org'gem 'fluentd', '0.12.34'gem 'fluent-plugin-elasticsearch', '1.9.3'

您可以通過Gemfile選項將這個--gemfile傳遞到Fluentd。

fluentd --gemfile /etc/fluent/Gemfile

當指定--gemfile選項時,Fluentd將嘗試使用Bundler安裝列出的gem。Fluentd只會載入與共享gems分開的列出的gems,並且還可以防止意外的插件更新。

另外,如果您更新Fluentd的Ruby版本,Bundler將重新安裝新的Ruby版本的列出的gems。這樣可以避免C擴展API兼容性問題。

9、Fluentd故障排除

9.1、看日誌

如果事情沒有如預期的那樣發生,請先看看您的日誌。對於td代理(rpm/deb),日誌位於

/var/log/td-agent/td-agent.log
9.2、打開詳細日誌記錄

如果啟用詳細日誌記錄,則可以獲得更多關於日誌的信息。請按照下面的步驟操作。

rpm

  1. edit /etc/init.d/td-agent

  2. add -vv to TD_AGENT_ARGS

  3. restart td-agent

# at /etc/init.d/td-agent...TD_AGENT_ARGS="... -vv"...

deb

  1. edit /etc/init.d/td-agent

  2. add -vv to DAEMON_ARGS

  3. restart td-agent

# at /etc/init.d/td-agent...DAEMON_ARGS="... -vv"...

gem

Please add -vv to your command line.

$ fluentd .. -vv
9.3、Dump Fluentd內部信息

Fluentd使用sigdump向本地文件DumpFluentd內部信息,如線程Dump、對象分配等。如果您有一個與Fluentd類似的問題,請將SIGCONT發送給Fluentd父進程和子進程。

9.4、高CPU使用率的問題

如果fluentd突然出現意外的高CPU使用率問題,有以下幾個原因:

  • 一個插件有一個race condition或類似的錯誤

  • 依賴的gems有一個錯誤

  • 有破損數據的正則表達式

  • 系統調用有一個bug,比如有大量文件的inotify

在這種情況下,您可以在最近的Linux上使用perf工具來研究這個問題。參見Linux perf示例頁面。
如果您想知道是哪個調用導致了問題,pid2line.rb是有用的。

9.5、檢查未捕獲日誌

您有時會意外地關閉不為零的退出狀態,如下所示。

2016-01-01 00:00:00 +0800 [info]: starting fluentd-0.12.282016-01-01 00:00:00 +0800 [info]: reading config file path="/etc/td-agent/td-agent.conf"[...snip...]2016-01-01 00:00:02 +0800 [info]: process finished code=6

如果在ruby中出現了問題,例如分割錯誤、C擴展錯誤等等,那麼當fluentd進程被daemonized時,就無法得到完整的日誌。

例如,td-agent使用--daemon選項啟動fluentd。在td-agent的情況下,您可以使用以下命令獲取整個日誌,以模擬/etc/init.d/td-agentstart而不進行後台管理。

$ sudo LD_PRELOAD=/opt/td-agent/embedded/lib/libjemalloc.so /usr/sbin/td-agent -c /etc/td-agent/td-agent.conf --user td-agent --group td-agent

10、在ssl下轉發數據

這是一個快速教程,介紹如何使用安全轉發插件來支持fluentd -to- fluentd數據傳輸的SSL。

它的目的是作為一個快速介紹。全面的文檔,包括參數的定義,請結帳了out_secure_forward和in_secure_forward。

10.1、設置:接收機(Receiver)

首先,安裝安全轉發插件。

Fluentd: gem install fluent-plugin-secure-forwardtd-agent v2: /usr/sbin/td-agent-gem install fluent-plugin-secure-forwardtd-agent v1: /usr/lib/fluent/ruby/bin/fluent-gem install fluent-plugin-secure-forward

然後,按如下所示設置配置文件:

<source> @type secure_forward shared_key YOUR_SHARED_KEY self_hostname server.fqdn.local cert_auto_generate yes</source><match secure.**> @type stdout</match>

這個條件使用out_stdout,將轉發的消息列印到STDOUT
(td-agent的日誌在var/log/td-agent/td-agent.log)

10.2、設置發送

首先,安裝安全轉發插件。

Fluentd: fluent-gem install fluent-plugin-secure-forwardtd-agent v2: /usr/sbin/td-agent-gem install fluent-plugin-secure-forwardtd-agent v1: /usr/lib/fluent/ruby/bin/fluent-gem install fluent-plugin-secure-forward

然後,設置配置文件如下:

:::text <source> @type forward </source> <match secure.**> @type secure_forward shared_key YOUR_SHARED_KEY self_hostname "#{Socket.gethostname}" <server> host RECEIVER_IP port 24284 </server></match>

條件是用來將測試數據輸入到[in_forward](https://docs.fluentd.org/v0.12/articles/in_forward)的Fluentd中。請確保您的shared_key與接收方相同。

10.3、確認:通過SSL發送事件

在發送方機器上,使用fluent-cat運行以下命令

Fluentd: echo '{"message":"testing the SSL forwarding"}' | fluent-cat --json secure.testtd-agent v2: echo '{"message":"testing the SSL forwarding"}' | /opt/td-agent/embedded/bin/fluent-cat --json secure.testtd-agent v1: echo '{"message":"testing the SSL forwarding"}' | /usr/lib/fluent/ruby/bin/fluent-cat --json secure.test

現在,檢查接收方的Fluentd的日誌(對於td代理來說,這將是/var/log/td-agent/td-agent.log),應該有這樣一條線:

2014-10-21 18:18:26 -0400 secure.test: {"message":"testing the SSL forwarding"}
10.4、資源

in_secure_forward
out_secure_forward
the secure forward plugin』s GitHub repo

11、Fluentd UI

fluentd-ui是一個基於瀏覽器的fluentd和td-agent管理器,支持以下操作。

  • 安裝、卸載、升級Fluentd插件

  • 啟動/停止/啟動fluentd過程

  • 配置Fluentd設置配置文件等內容,pid文件路徑等

  • 查看Fluentd日誌查看器使用簡單的錯誤

11.1、開始

如果你已經安裝了td-agent,你可以開始了td-agent-ui start如下:

$ sudo /usr/sbin/td-agent-ui startPuma 2.9.2 starting...* Min threads: 0, max threads: 16* Environment: production* Listening on tcp://0.0.0.0:9292

或者如果使用fluentd gem,首先安裝fluentd-ui通過gem命令。

$ gem install -V fluentd-ui$ fluentd-ui startPuma 2.9.2 starting...* Min threads: 0, max threads: 16* Environment: production* Listening on tcp://0.0.0.0:9292

然後,打開你的瀏覽器localhost:9292。
默認的賬戶和密碼:admin changeme

11.2、截圖

Dashboard

setting

in_tail setting

Plugin

12、命令行選項

本文描述了內置命令及其選項

12.1、fluentd

調用fluentd。這是支持選項:

Usage: fluentd [options] -s, --setup [DIR=/etc/fluent] install sample configuration file to the directory` -c, --config PATH config file path (default: /etc/fluent/fluent.conf) --dry-run Check fluentd setup is correct or not -p, --plugin DIR add plugin directory -I PATH add library path -r NAME load library -d, --daemon PIDFILE daemonize fluent process --no-supervisor run without fluent supervisor --user USER change user --group GROUP change group -o, --log PATH log file path -i CONFIG_STRING, inline config which is appended to the config file on-fly --inline-config --emit-error-log-interval SECONDS suppress interval seconds of emit error logs --suppress-repeated-stacktrace [VALUE] suppress repeated stacktrace --without-source invoke a fluentd without input plugins --use-v1-config Use v1 configuration format (default) --use-v0-config Use v0 configuration format -v, --verbose increase verbose level (-v: debug, -vv: trace) -q, --quiet decrease verbose level (-q: warn, -qq: error) --suppress-config-dump suppress config dumping when fluentd starts -g, --gemfile GEMFILE Gemfile path -G, --gem-path GEM_INSTALL_PATH Gemfile install path (default: $(dirname $gemfile)/vendor/bundle)

重要的參數

–suppress-config-dump

Fluentd啟動沒有配dump。如果您不想在fluentd日誌中顯示配置,例如,不要顯示私鑰,那麼這個選項是有用的。

–suppress-repeated-stacktrace

如果設置true,在fluentd日誌中抑制重複的日誌堆棧stacktrace。從v0.12開始,默認情況下這個選項是true的。

–without-source

Fluentd開始時沒有輸入插件。此選項對於不使用新傳入事件的刷新緩衝區非常有用。

-i, –inline-config

如果您在XaaS上使用不支持持久磁碟的fluentd,這個選項是有用的。

–no-supervisor

如果你想使用你的supervisor tools,這個選項可以避免雙重 supervisor。

通過配置文件設置

可以通過配置文件設置幾個選項。參見配置文件文章。

12.2、fluent-cat

發送事件到fluentd的in_forward / in_unix插件。這對測試很有用。
發送事件到fluentd的in_forward / in_unix插件。這對測試很有用。

Usage: fluent-cat [options] <tag> -p, --port PORT fluent tcp port (default: 24224) -h, --host HOST fluent host (default: 127.0.0.1) -u, --unix use unix socket instead of tcp -s, --socket PATH unix socket path (default: /var/run/fluent/fluent.sock) -f, --format FORMAT input format (default: json) --json same as: -f json --msgpack same as: -f msgpack --none same as: -f none --message-key KEY key field for none format (default: message)

例子

使用debug發送json消息。本地fluentd日誌標籤:

% echo '{"message":"hello"}' | fluent-cat debug.log

發送到其他機器:

% echo '{"message":"hello"}' | fluent-cat debug.log --host testserver --port 24225

Advertisements

你可能會喜歡