零からの

ゆるーく綴るブログ

FluentdからElasticsearchへ転送する

前回のエントリーで標準出力できるところまで確認したので、同じ内容をElasticsearchに登録できるようにしてみた。

環境

Fluentdだけでなく、ElasticsearchやKibanaのコンテナを登録する。

.
├── docker
│   └── fluentd
│       ├── Dockerfile
│       └── etc
│           └── fluent.conf
└── docker-compose.yml

docker/fluentd/Dockerfile

ほぼDocker公式通り。(3. Customize Dockerfile to install plugins (optional))

FROM fluent/fluentd:v1.11-1

# Use root account to use apk
USER root

# below RUN includes plugin as examples elasticsearch is not required
# you may customize including plugins as you wish
RUN apk add --no-cache --update --virtual .build-deps \
        sudo build-base ruby-dev \
 && sudo gem install fluent-plugin-elasticsearch \
 && sudo gem sources --clear-all \
 && apk del .build-deps \
 && rm -rf /tmp/* /var/tmp/* /usr/lib/ruby/gems/*/cache/*.gem

COPY docker/fluentd/etc/fluent.conf /fluentd/etc/

USER fluent

docker/fluentd/etc/fluent.conf

elasticsearchプラグインを参照。

logstash_formatをtrueにした場合は、logstash-YYYY.MM.DDとなるがlogstash_prefixで任意の値も設定可能。(ここではhelloとした)

<source>
  @type dummy
  dummy {"hello":"world"}
  tag hello_world
</source>

<match **>
  @type elasticsearch
  host elasticsearch
  port 9200
  logstash_format true
  logstash_prefix hello
</match>

docker-compose.yml

version: '3'
services:
  fluentd:
    build:
      context: .
      dockerfile: docker/fluentd/Dockerfile
    environment:
      - TZ=Asia/Tokyo
    volumes:
      - ./docker/fluentd/etc:/fluentd/etc
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.8.0
    environment:
      - TZ=Asia/Tokyo
      - discovery.type=single-node
    ports:
      - 9200:9200
  kibana:
    image: docker.elastic.co/kibana/kibana:7.8.0
    environment:
      - TZ=Asia/Tokyo
    ports:
      - 5601:5601

実行

Dockerを起動させてみる。

[warn]: #0 Could not communicate to Elasticsearch, resetting connection and trying again. Connection refused - connect(2) for 172.18.0.2:9200 (Errno::ECONNREFUSED)のようなログが出力された場合はESへの転送に失敗している。

docker-compose up -d

確認

Kibanaで確認する。

http://localhost:5601にアクセスする。

Discover > Create index pattern > Index patternにhelloと入力する。

以下の画像のように「hello-YYYY.MM.DD」と表示されていればOK。 Time Filter field nameは@timestampを選択して、インデックスを作成する。

f:id:kzp:20200724234932p:plain
Create Index Pattern

データも登録・表示することが確認できる。

f:id:kzp:20200724235013p:plain
Discover

最後に

ESへの登録、Kibanaでの表示まで確認することができた。

次はアプリログを取り込むことを確認していく。