使用 Fluentd 將日誌數據引入 Azure Data Explorer

日誌擷取是指從應用程式、伺服器、容器及雲端服務收集、轉換及準備日誌資料的過程,以便您能夠儲存、分析及監控這些資料。 日誌會捕捉錯誤、警告、使用模式及系統效能等資訊。 可靠的日誌擷取確保營運與安全資料幾乎即時可用,方便故障排除與洞察。 本文說明如何將日誌從 Fluentd 傳送到 Azure Data Explorer(Kusto),包括安裝、設定及驗證步驟。

概觀

什麼是 Fluentd?

Fluentd 是一個開源的資料收集器,可以用來統一跨多個系統的日誌收集與路由。 它支援超過 1,000 個外掛,並提供靈活的篩選、緩衝與資料轉換選項。 你可以在雲端原生和企業環境中使用 Fluentd 進行集中式日誌聚合與轉發。

什麼是 Azure 資料總管?

Azure Data Explorer(ADX)是一款快速且全管理的分析服務,優化於對大量結構化、半結構化及非結構化資料的即時分析。 ADX 採用 Kusto 查詢語言(KQL),廣泛用於遙測、監控、診斷及互動式資料探索。

先決條件

  • 在需接收日誌的節點上安裝了 Ruby。 要使用 gem 套件管理器安裝 fluentd 相依性,請參考 Ruby 的安裝說明
  • 存取 Azure Data Explorer 叢集與資料庫。
  • Azure Active Directory 應用程式具有引入數據的權限。

如何開始使用 Fluentd 和 Azure Data Explorer

  1. 使用 RubyGems 安裝 Fluentd:

    gem install fluentd
    
  2. 安裝 Fluentd Kusto 外掛:

    gem install fluent-plugin-kusto
    
  3. 透過建立包含以下內容的設定檔(例如 ),fluent.conf。 用 Azure 和插件的值替換佔位符:

    <match <tag-from-source-logs>>
      @type kusto
      endpoint https://<your-cluster>.<region>.kusto.windows.net
      database_name <your-database>
      table_name <your-table>
      logger_path <your-fluentd-log-file-path>
    
      # Authentication options
      auth_type <your-authentication-type>
    
      # AAD authentication
      tenant_id <your-tenant-id>
      client_id <your-client-id>
      client_secret <your-client-secret>
    
      # Managed identity authentication (optional)
      managed_identity_client_id <your-managed-identity-client-id>
    
      # Workload identity authentication (optional)
      workload_identity_tenant_id <your-workload-identity-tenant-id>
      workload_identity_client_id <your-workload-identity-client-id>
    
      # Non-buffered mode
      buffered false
      delayed false
    
      # Buffered mode
      # buffered true
      # delayed <true/false>
    
      <buffer>
        @type memory
        timekey 1m
        flush_interval 10s
      </buffer>
    </match>
    

更多設定與認證細節,請參閱 Fluentd Kusto 外掛文件

  1. 準備 Azure Data Explorer 以供資料引入:

    1. 建立 ADX 叢集和資料庫。 請參見 建立 Azure Data Explorer 叢集與資料庫。

    2. 建立 Azure Active Directory 應用程式,並授權資料匯入 ADX 資料庫。 請參閱 於 Azure Data Explorer 中建立 Microsoft Entra 應用程式註冊。

      備註

      請儲存應用程式序號和應用程式 ID 以備未來使用。

    3. 建立一個用於日誌擷取的資料表。 例如:

    .create table LogTable (
      tag:string,
      timestamp:datetime,
      record:dynamic
    )
    
  2. 用設定檔執行 Fluentd:

    fluentd -c fluent.conf
    
  3. 透過以下方式驗證日誌擷取:

    1. 檢查 Fluentd 日誌檔,確認沒有錯誤,且擷取請求已成功傳送。

      Fluentd 日誌檔案截圖,顯示成功擷取請求

    2. 查詢 ADX 表格 以確保日誌正確被擷取:

    LogTable
    | take 10
    
  4. 擷取映射:使用 Kusto 中預先定義的擷取映射,將預設的三欄格式資料轉換成你想要的架構。 欲了解更多資訊,請參閱 「攝取映射支援」。