Docker - 搭建日誌監控系統

項目中常用集中日誌收集工具

Logstash 是一個開源數據收集引擎,具有實時管道功能。Logstash 可以動態地將來自不同數據源的數據統一起來,並將數據標準化到你所選擇的目的地。

作爲 Beats 家族的一員,Filebeat 是一個輕量級的日誌傳輸工具,它的存在正彌補了 Logstash 的缺點:Filebeat 作爲一個輕量級的日誌傳輸工具可以將日誌推送到中心 Logstash。

Fluentd 創建的初衷主要是儘可能的使用 JSON 作爲日誌輸出,所以傳輸工具及其下游的傳輸線不需要猜測子字符串裏面各個字段的類型。這樣,它爲幾乎所有的語言都提供庫,這也意味着,我們可以將它插入到我們自定義的程序中。

使用 Docker-Compose 搭建 EFK 收集中心

  1. 創建 docker-compose.yml

新建一個 efk 目錄,然後進入目錄下:

version: '3'
services:
  web:
    image: httpd
    ports:
      - "80:80"
    links:
      - fluentd
    logging:
      driver: "fluentd"
      options:
        fluentd-address: localhost:24224
        tag: httpd.access

  fluentd:
    build: ./fluentd
    volumes:
      - ./fluentd/conf:/fluentd/etc
    links:
      - "elasticsearch"
    ports:
      - "24224:24224"
      - "24224:24224/udp"

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.10.2
    environment:
      - "discovery.type=single-node"
    expose:
      - "9200"
    ports:
      - "9200:9200"

  kibana:
    image: kibana:7.10.1
    links:
      - "elasticsearch"
    ports:
      - "5601:5601"
  1. 創建 fluentd 鏡像以及配置 config 與插件

新建 fluentd/Dockerfile

FROM fluent/fluentd:v1.12.0-debian-1.0
USER root
RUN ["gem""install""fluent-plugin-elasticsearch""--no-document""--version""4.3.3"]
USER fluent

新建 fluentd/conf/fluent.conf

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

<match *.**>
  @type copy

  <store>
    @type elasticsearch
    host elasticsearch
    port 9200
    logstash_format true
    logstash_prefix fluentd
    logstash_dateformat %Y%m%d
    include_tag_key true
    type_name access_log
    tag_key @log_name
    flush_interval 1s
  </store>

  <store>
    @type stdout
  </store>
</match>
  1. 啓動服務

docker-compose up
  1. 多次請求 httpd 服務生成日誌

$ curl localhost:80
  1. 驗證日誌收集

打開瀏覽器訪問 http://localhost:5601

初始化創建 fluentd-* 索引

創建索引

此時可以看到 Httpd 生成的日誌已經被收集

log

使用 fluentd 收集關鍵點

  1. 如何指定 fluentd 驅動
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/xSs9CtVoU0M1biSflWiAbA