Fluentd 的日誌收集配置

前言

先簡單說明一下,當前生產中咱們全部的服務都運行在 docker 裏面,到目前爲止尚未使用 k8s 來管理 docker,平常的更新部署等主要仍是使用 ansible + jenkins 結合來進行的,例如 docker 中服務的日誌則是經過掛載到宿主機的目錄當中,而後經過運行 fluentd 的 docker 也是經過掛載的方式讀取宿主機所在的日誌目錄來進行收集。如下的配置說明示例使用的 fluentd 的 1.9.1 版本。docker

環境

fluentd 典型的部署架構須要包含兩種不一樣角色:轉發器(forwarder),聚合器(aggregator)express

Fluentd 的日誌收集配置

當前環境中:
轉發器的 fluentd 是運行在每一個主機的 docker,將每一個主機上其餘 docker 服務的日誌收集起來而後轉發到聚合器的 fluentd。
聚合器的 fluentd 是獨立主機上運行的兩個 docker,是將 docker 內部 24224 端口分別映射到主機的 24227 端口和 24228 端口。它將各個主機上轉發過來的日誌匹配過濾而後存儲到 elasticsearch 中。ruby

fluentd 在線測試工具:https://fluentular.herokuapp.com架構

定義 flunetd 鏡像

fluentd 這裏都是使用的官方的 docker 鏡像而後安裝的插件。默認的 fluentd 鏡像沒有我須要的 fluent-plugin-elasticsearch 插件和 fluent-plugin-forest 插件,因此須要在 fluentd 的鏡像的基礎上經過 Dockerfile 中執行以下命令安裝插件:
fluent-gem install fluent-plugin-elasticsearch && fluent-gem install fluent-plugin-forestapp

從新構建的 fluentd 鏡像上傳到本身的 harbor 倉庫中,而後經過 ansible 部署啓動 fluentd 的 docker 到各個主機上。socket

轉發器配置示例

<source>
  @type tail
  path /home/game/http_8001/logs/access.log,/home/game/http_8002/logs/access.log
  pos_file /home/game/access.log.pos
  tag game.http.host01
  #read_from_head true
  format /^(?<log>.*)/
</source>

<match game.**>
  @type forward
  <buffer>
     @type file
     path /home/game/td-gamex-buffer
     chunk_limit_size 128MB
     total_limit_size 8GB
     chunk_full_threshold 0.9
     compress text
     flush_mode default
     flush_interval 15s
     flush_thread_count 1
     delayed_commit_timeout 60
     overflow_action throw_exception
     retry_timeout 10m
   </buffer>
  send_timeout 60s
  recover_wait 10s
  heartbeat_interval 1s
  transport tcp
  <server>
    name aggregator01
    host 10.144.77.88
    port 24227
    weight 60
  </server>
  <server>
    name aggregator02
    host 10.144.77.88
    port 24228
    weight 60
  </server>

聚合器配置示例

<source>
  @type forward
  port 24224
</source>

<filter game.http.**>
  @type record_transformer
  enable_ruby
  <record>
    service ${tag_parts[1]}
    host ${tag_parts[2]}
  </record>
</filter>

<filter game.http.**>
  @type parser
  reserve_data yes
  key_name log
  <parse>
    @type regexp
    expression /^(?<remote>[^ ]*) - (?<server_id>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^\"]*?)(?: +\S*)?)?" (?<code>[^ ]*) (?<body_bytes_sent>[^ ]*) "(?<referer>[^\"]*)" "(?<agent>[^\"]*)" (?<response-time>[^ ]*) (?<uid>[^ ]*) "(?<header_dvt>[^\"]*)" (?<header_did>[^ ]*) (?<header_sv>[^ ]*) (?<header_v>[^ ]*) (?<body>[^ ]*)/ 
    time_format %d/%b/%Y:%H:%M:%S %z
  </parse>
</filter>

<match game.**>
  @type copy
  <store>
    @type forest
    subtype elasticsearch
    <buffer>
       @type file
       path /tmp/elastic-buffer-191
       total_limit_size 1024MB
       chunk_limit_size 16MB
       flush_mode interval
       flush_interval 5s
       flush_thread_count 8
    </buffer>

    <template>
      hosts 10.144.77.99:9200
      logstash_format true
      logstash_prefix ${tag_parts[1]}
    </template>
    <case game.http.**>
      logstash_prefix ${tag}
    </case>
    <case game.socket.**>
      logstash_prefix ${tag}
    </case>
  </store>
</match>
相關文章
相關標籤/搜索