Carpe Diem

備忘録

fluentdをローカルで簡単に検証する

概要

ローカルでサクッとfluentdの検証をしたい時に毎回調べ直していたのでメモ。

設定ファイル

fluentd.confという名前で設定を用意します。
forwardされたログをstdoutに出力します。

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

<match *.*>
  @type stdout
</match>

dockerでローカル起動

先程のfluentd.confディレクトリを調べておき、docker volumeとしてマウントします。

$ pwd
/Users/jun06t/fluentd

そしてFLUENTD_CONFという環境変数にファイル名を指定して起動します。

$ docker run -d \
  --name fluentd \
  -p 24224:24224 -p 24224:24224/udp \
  -v /Users/jun06t/fluentd:/fluentd/etc \
  -e FLUENTD_CONF=fluentd.conf \
  fluent/fluentd:v1.2.5

起動時のログにfluentd.confの内容が入っていればOKです。

$ docker logs fluentd 
2018-08-30 13:09:29 +0000 [info]: parsing config file is succeeded path="/fluentd/etc/fluentd.conf"
2018-08-30 13:09:30 +0000 [info]: using configuration file: <ROOT>
  <source>
    @type forward
    port 24224
    bind "0.0.0.0"
  </source>
  <match *.*>
    @type stdout
  </match>
</ROOT>
2018-08-30 13:09:30 +0000 [info]: starting fluentd-1.2.5 pid=6 ruby="2.4.4"
2018-08-30 13:09:30 +0000 [info]: spawn command to main:  cmdline=["/usr/bin/ruby", "-Eascii-8bit:ascii-8bit", "/usr/bin/fluentd", "-c", "/fluentd/etc/fluentd.conf", "-p", "/fluentd/plugins", "--under-supervisor"]
2018-08-30 13:09:30 +0000 [info]: gem 'fluentd' version '1.2.5'
2018-08-30 13:09:30 +0000 [info]: adding match pattern="*.*" type="stdout"
2018-08-30 13:09:30 +0000 [info]: adding source type="forward"
2018-08-30 13:09:30 +0000 [info]: #0 starting fluentd worker pid=16 ppid=6 worker=0
2018-08-30 13:09:30 +0000 [info]: #0 listening port port=24224 bind="0.0.0.0"
2018-08-30 13:09:30 +0000 [info]: #0 fluentd worker is now running worker=0
2018-08-30 13:09:30.770056835 +0000 fluent.info: {"worker":0,"message":"fluentd worker is now running worker=0"}

検証

ログ転送元

dockerのfluentd log driverを使って検証してみます。

$ docker run -it --name logging-driver-test \
  --log-driver=fluentd \
  --log-opt fluentd-address=localhost:24224 \
  --log-opt tag="docker.{{.Name}}" \
  --rm \
  alpine sh

stdoutに流れるようにコマンドを入力してみます。

$ echo "hoge"
hoge

転送先のfluentdコンテナ

$ docker logs -f fluentd
2018-08-30 13:16:39.000000000 +0000 docker.logging-driver-test: {"container_name":"/logging-driver-test","source":"stdout","log":"/ # \u001b[6n\r/ # echo 'hoge'\u001b[J\u0007\r","container_id":"c7f7b65e2ec100f4af0d2a361912f327a71570b2a1ad34e8dd47b2002e644d6a"}
2018-08-30 13:16:39.000000000 +0000 docker.logging-driver-test: {"source":"stdout","log":"hoge\r","container_id":"c7f7b65e2ec100f4af0d2a361912f327a71570b2a1ad34e8dd47b2002e644d6a","container_name":"/logging-driver-test"}

ちゃんとhogeが出力されてることがわかります。

まとめ

dockerを使うことでインストールしなくてもサクッとfluentdの検証ができました。

ソース