ゲーム業界エンジニアの独り言

クライアント、サーバ、インフラなどなど興味あることなんでも紹介

【Docker】FluentdでとElasticsearchとRe:dashでログ管理

DockerでFluentdとElasticsearchとRe:dashを起動してログの収集と解析を行います。

Docker

dockerとdocker-composeを導入

CentOSであれば下記を参照 CentOS6.5にdockerとdocker-composeをインストール

前提

  • ログは行にJSON
  • Fluentd & Elasticsearchは同じサーバ上に存在

ログ例

{ "type": "chatlog", "name": "hoge1", "text": "hoge1" }
{ "type": "chatlog", "name": "hoge2", "text": "hoge2" }
{ "type": "chatlog", "name": "hoge3", "text": "hoge3" }
{ "type": "chatlog", "name": "hoge4", "text": "hoge4" }
{ "type": "chatlog", "name": "hoge5", "text": "hoge5" }

Fluentd & Elasticsearch

$ mkdir ~/workspace
$ cd ~/workspace

Fluentd

$ mkdir fluentd
$ cd fluentd
$ mkdir plugins
$ vi Dockerfile
$ vi fluent.conf
FROM fluent/fluentd:latest-onbuild

USER fluent

WORKDIR /home/fluent
ENV PATH /home/fluent/.gem/ruby/2.3.0/bin:$PATH
RUN gem install fluent-plugin-secure-forward
RUN gem install fluent-plugin-elasticsearch

EXPOSE 24284

CMD fluentd -c /fluentd/etc/fluent.conf -p /fluentd/plugins -vv
<source>
  type tail
  path /var/log/hoge/[log file] # 収集するログファイル名を指定
  tag json.hoge
  pos_file /var/log/hoge/[log file].pos # 収集するログファイル名を指定
  format json
</source>

<match json.**>
  type copy

  <store>
    type stdout
  </store>
  
  <store>
    type elasticsearch
    host localhost   # elasticsearchのホスト
    port 9200        # elasticsearchのポート
    logstash_format true
  </store>
</match>

Elasticsearch

$ mkdir elasticsearch
$ cd elasticsearch
$ vi Dockerfile
FROM elasticsearch

RUN bin/plugin install mobz/elasticsearch-head

EXPOSE 9200

CMD ["bin/elasticsearch", "-Des.insecure.allow.root=true"]

起動

$ cd ~/workspace
$ vi docker-compose.yml
elasticsearch:
  build: elasticsearch
  ports:
    - 9200:9200
fluentd:
  build: fluentd
  ports:
    - 24284:24284
  volumes:
    - [log folder]:/var/log/hoge # 収集するログフォルダを指定
$ docker-compose up

ElasticsearchのWebページ

http://[IP]:9200/_plugin/head/

Re:dash

詳しくは下記を参照 Re:dashをdockerで起動する

データソースを追加

Settings > DATA SOURCES > NEW DATA SOURCES

Base URLはこんな感じ

http://[IP]:9200

スクリーンショット 2016-07-22 12.23.42.png

Docker

Docker