為什麼我們需要Logstash,Fluentd等日誌攝取器?

有態度的小碼甲發表於2020-08-23

前文傳送門:Logging with ElasticSearch, Kibana, ASP.NET Core and Docker
疑問:既然應用能直接向ElasticSearch寫日誌,為什麼我們還需要Logstash,Fluentd等日誌攝取器? 而且這些日誌攝取器元件還成為日誌收集的事實標準?

  1. 與成都大佬的溝通答疑:

  2. 最近讀到的十二要素方法論第11點:Treat logs as event streams

A twelve-factor app never concerns itself with routing or storage of its output stream. It should not attempt to write to or manage logfiles. Instead, each running process writes its event stream, unbuffered, to stdout. During local development, the developer will view this stream in the foreground of their terminal to observe the app’s behavior.

總結:您的應用不應該關注日誌的路由和儲存(Elasticsearch / Graylog / ...),您的日誌應該只輸出到stdout,整個系統所有應用保持統一輸出,由日誌攝取器無侵入式收集

在具有多種服務的dockerized環境中,每個容器都是隔離的並擁有自己的日誌,我們需要一個介面來收集這些日誌。
Docker Logging Driver就是幹這個的:每個docker守護程式都有一個日誌驅動程式,所有容器的日誌都會流經該驅動程式, Docker Logging Drive讓我們具備處理、轉發日誌的能力。

Fluent Bit vs Fluentd

流行的庫是Fluentd, 這是一個開源的日誌收集、處理、聚合元件,使用Ruby開發。
Fluent-Bit是從同一專案中fok出來的,用C寫成的開源日誌收集器。

Fluentd Fluent Bit
Scope Containers / Servers Containers / Servers
Language C & Ruby C
Memory ~40MB ~450KB
Performance High Performance High Performance
Dependencies Built as a Ruby Gem, it requires a certain number of gems. Zero dependencies, unless some special plugin requires them.
Plugins More than 650 plugins available Around 50 plugins available
License Apache License v2.0 Apache License v2.0

下面我們使用輕量級的Fluent-bit向ElasticSearch傳送容器日誌。

可通過檔案或者命令列配置Fluent-Bit,下面是關鍵的配置節:

  • Service: 定義Fluent-Bit引擎的全域性行為
  • Input: 定義Fluent-Bit從什麼地方收集資料
  • Parser: 將非結構化日誌轉換為結構化日誌
  • Filter: 修改Input外掛收集的傳入資料
  • Output:定義Fluent Bit將資料輸出到哪裡

Fluent Bit as Docker Logging Driver

為收集、轉發容器日誌,我們需要將Fluent Bit設定為Docker Logging Driver。

  • 使用foward輸入外掛,監聽Forward協議的轉發訊息
  • 要將日誌轉發到Elasticsearch,需設定es輸出外掛

fluent-bit.conf示例如下:

[SERVICE]
    log_level info

[INPUT]
    Name forward
    Listen 0.0.0.0
    port 24224
    
[OUTPUT]
    Name es
    Match **
    Host 127.0.0.1
    Port 9243
    # When Logstash_Format is enabled, the Index name is composed using a prefix and the date
    Logstash_Format True
    # HTTP_User <user>
    # HTTP_Passwd <pw>
    # Alternative time key, useful if your log entries contain an @timestamp field that is used by Elasticsearch
    # Time_Key es_time
    # If your Elasticsearch is using TLS, configure this
    # tls On
    # tls.verify Off

啟動ES、Fluent-Bit和一個產生日誌的測試專案:

version: "3.5"
services:
  elasticsearch:
    image: elasticsearch:7.6.2
    ports:
      - "9200:9200"
    environment:
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - discovery.type=single-node
  fluentbit:
    image: fluent/fluent-bit:1.5.3
    volumes:
      - type: bind
        source: ./fluent-bit.conf
        target: /fluent-bit/etc/fluent-bit.conf
    ports:
      - "24224:24224"
      - "24224:24224/udp"
    depends_on:
      - elasticsearch
  ubuntu:
    image: ubuntu
    command: [/bin/echo, "Dotnet Plus很乾,值得關注!"]
    depends_on:
      - fluentbit
    logging:
      driver: fluentd
      options:
        tag: docker-ubuntu

其中注意:

  1. Fluent-Bit容器外掛pipeline配置檔案
  2. Fluentd和Fluent Bit均使用fluentd作為Docker Logging Driver。

檢查ElasticSearch中的日誌

curl localhost:9200/_cat/indices

yellow open logstash-2020.08.22 vqoyvKE4SFCcJtfo6BRmQg 1 1 1 0 6.2kb 6.2kb

 curl localhost:9200/logstash-2020.08.22/_search?pretty=true&q={'matchAll':{''}}
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "logstash-2020.08.22",
        "_type" : "_doc",
        "_id" : "z0WsFnQBU8QzIbCaBXGY",
        "_score" : 1.0,
        "_source" : {
          "@timestamp" : "2020-08-22T14:56:33.000Z",
          "log" : "Dotnet Plus很乾,值得關注!",
          "container_id" : "e921435eb7b8dc61bbb8e938bf67cea2694e2afd699ca71c4ef5b6d7cca12e34",
          "container_name" : "/ef_ubuntu_1",
          "source" : "stdout"
        }
      }
    ]
  }
}

docker應用僅使用stdout,docker logging driver將日誌轉發至Fluent-Bit,Fluent-Bit將它們轉發給Elasticsearch。

小編結束語

以上就是利用Fluent-Bit從容器應用收集日誌併傳送到ElasticSearch的基本示例。

我們再回顧下Fluent-Bit產生的背景和特性:
如今,我們環境中的資訊源在不斷增加,資料收集越來越複雜,需要解決

  • 不同的資訊來源
  • 不同的資料格式
  • 資料可靠性
  • 安全
  • 靈活的路由
  • 多個目的地

Fluent-Bit旨在成為日誌收集和加工的通用瑞士軍刀, 同時Fluent Bit在設計時考慮了效能和低資源消耗。

相關文章