LoginSignup
1
2

More than 5 years have passed since last update.

独自設定ファイルの fluentd コンテナを作成し、Fluentd logging driver で使ってみる

Posted at

検証で Fluentd logging driver コンテナを使う必要があり、独自の設定ファイル(flunetd.conf)のイメージを作成するまでをやったのでメモ。

設定ファイルを検証する

Test container loggers

まずはローカルで設定ファイルを検証する。
ドキュメント通りだがやってみる。

設定する flunetd 用の設定ファイルを作成。
Input されたものを標準出力する設定ファイルとする。

test.conf
 <source>
   @type forward
 </source>

 <match *>
   @type stdout
 </match>

以下でコンテナを起動させる。

$docker run -it -p 24224:24224 -v `pwd`/test.conf:/fluentd/etc/test.conf -e FLUENTD_CONF=test.conf fluent/fluentd:latest

別 terminal でコンテナを起動する

$docker run --log-driver=fluentd busybox echo hoge

この時に fluentd コンテナで以下が出力されれば OK.

2019-03-19 21:34:06.000000000 +0000 10e9f96207fe: {"source":"stdout","log":"hoge","container_id":"10e9f96207fec6ead0d5757457c0446c4320c3b2b5c8c92588be721005909609","container_name":"/stupefied_cartwright"}

ドキュメントに書いてある通り、source や コンテナ ID などのフィールドの追加も確認出来たので成功。

Image を作成する

設定ファイルが問題なさそうなのでイメージを作成する。

fluent/fluentd

作成方法は fluentd の DockerHub の「How to build your own image」に詳細をが記載されていたのでこちらを参考にやってみる。

# Create project directory.
mkdir custom-fluentd
cd custom-fluentd

# Download default fluent.conf. This file will be copied to the new image.
# VERSION is v1.3 or v0.12 like fluentd version and OS is alpine or debian.
# Full example is https://raw.githubusercontent.com/fluent/fluentd-docker-image/master/v0.12/debian-onbuild/fluent.conf
curl https://raw.githubusercontent.com/fluent/fluentd-docker-image/master/VERSION/OS-onbuild/fluent.conf > fluent.conf

# Create plugins directory. plugins scripts put here will be copied to the new image.
mkdir plugins

curl https://raw.githubusercontent.com/fluent/fluentd-docker-image/master/Dockerfile.sample > Dockerfile

次に flunetd.conf を先程検証したファイルに変更する。
最後に build する。

$docker build -t toshihirock/dump-fluentd .

問題ないか確認。

$docker run -it -p 24224:24224 toshihirock/dump-fluentd:latest
$docker run --log-driver=fluentd busybox echo hoge
hoge

これで同じように flunetd コンテナにログが出ることを確認。

DockerHub に push しておく。

$docker push toshihirock/dump-fluentd

なお、今回は利用していないが plugin を使う場合には Dockerfile において sudo gem install fluent-plugin-elasticsearchという感じで指定すれば良さそう。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2