fluent-plugin-kinesis-firehoseでAmazon Kinesis Firehoseにログを転送する

2015.10.11

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

2015/10/12 23:55更新
td-agent 2では`gem`コマンドの実行を`td-agent-gem`で行うのが推奨されるため、`fluent-gem`を差し替えました。

ども、大瀧です。
AWSのカンファレンスイベント、re:Invent 2015で発表&ローンチされたAmazon Kinesis FirehoseはKinesisによるストリーム処理をより手軽に利用できる期待の新サービスです。

ただ、FirehoseのDelivery Streamにログデータを送信するためにはFirehose APIを直接叩くか、AWS Kinesis Agentを導入する方法しかありませんでした。本日、@winebarrelさん作のfluent-plugin-kinesis-firehoseが公開され、FluentdからFirehoseにログデータを送信できるようなったので、早速試してみました!

動作確認環境

  • OS/AMI : Amazon Linux 2015.09(HVM) ami-9a2fb89a
  • td-agent : バージョン 2.2.1
  • fluent-plugin-kinesis-firehose : バージョン0.0.1

1. インストール

今回はFluentdディストリビューションのtd-agentのインストールをFluentdのドキュメントの通り、以下のように実行します。

$ curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent2.sh | sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   662  100   662    0     0   4493      0 --:--:-- --:--:-- --:--:--  4503
This script requires superuser access to install rpm packages.
You will be prompted for your password by sudo.
読み込んだプラグイン:priorities, update-motd, upgrade-helper
treasuredata/latest/x86_64                                                                                                | 2.9 kB     00:00
treasuredata/latest/x86_64/primary_db                                                                                     | 9.8 kB     00:00

aws-cli.noarch                                                           1.8.7-1.25.amzn1                                            amzn-updates
grep.x86_64                                                              2.20-1.15.amzn1                                             amzn-updates
initscripts.x86_64                                                       9.03.40-2.31.amzn1                                          amzn-updates
python27-botocore.noarch                                                 1.2.5-1.30.amzn1                                            amzn-updates
yum-plugin-priorities.noarch                                             1.1.31-29.26.amzn1                                          amzn-updates
yum-plugin-upgrade-helper.noarch                                         1.1.31-29.26.amzn1                                          amzn-updates
yum-utils.noarch                                                         1.1.31-29.26.amzn1                                          amzn-updates
読み込んだプラグイン:priorities, update-motd, upgrade-helper
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ td-agent.x86_64 0:2.2.1-0.el2015 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

=================================================================================================================================================
 Package                         アーキテクチャー              バージョン                              リポジトリー                         容量
=================================================================================================================================================
インストール中:
 td-agent                        x86_64                        2.2.1-0.el2015                          treasuredata                         57 M

トランザクションの要約
=================================================================================================================================================
インストール  1 パッケージ

総ダウンロード容量: 57 M
インストール容量: 199 M
Downloading packages:
td-agent-2.2.1-0.el2015.x86_64.rpm                                                                                        |  57 MB     00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  インストール中          : td-agent-2.2.1-0.el2015.x86_64                                                                                   1/1
adding 'td-agent' group...
adding 'td-agent' user...
Installing default conffile...
prelink detected. Installing /etc/prelink.conf.d/td-agent-ruby.conf ...
Configure td-agent to start, when booting up the OS...
  検証中                  : td-agent-2.2.1-0.el2015.x86_64                                                                                   1/1

インストール:
  td-agent.x86_64 0:2.2.1-0.el2015

完了しました!
$

fluent-plugin-kinesis-firehoseはRubyGemsに登録済みなので、td-agent-gemで導入しました。

$ sudo td-agent-gem install fluent-plugin-kinesis-firehose
WARN: Unresolved specs during Gem::Specification.reset:
      json (>= 1.4.3)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.
Fetching: jmespath-1.1.3.gem (100%)
Successfully installed jmespath-1.1.3
Fetching: aws-sdk-core-2.1.29.gem (100%)
Successfully installed aws-sdk-core-2.1.29
Fetching: aws-sdk-resources-2.1.29.gem (100%)
Successfully installed aws-sdk-resources-2.1.29
Fetching: aws-sdk-2.1.29.gem (100%)
Successfully installed aws-sdk-2.1.29
Fetching: fluent-plugin-kinesis-firehose-0.0.1.gem (100%)
Successfully installed fluent-plugin-kinesis-firehose-0.0.1
Parsing documentation for aws-sdk-2.1.29
Installing ri documentation for aws-sdk-2.1.29
Parsing documentation for aws-sdk-core-2.1.29
Installing ri documentation for aws-sdk-core-2.1.29
Parsing documentation for aws-sdk-resources-2.1.29
Installing ri documentation for aws-sdk-resources-2.1.29
Parsing documentation for fluent-plugin-kinesis-firehose-0.0.1
Installing ri documentation for fluent-plugin-kinesis-firehose-0.0.1
Parsing documentation for jmespath-1.1.3
Installing ri documentation for jmespath-1.1.3
Done installing documentation for aws-sdk, aws-sdk-core, aws-sdk-resources, fluent-plugin-kinesis-firehose, jmespath after 4 seconds
5 gems installed
$

これでインストールはOKです。

2. Fluentdの設定

今回はApacheログをFirehoseに転送します。td-agentの設定ファイル/etc/td-agent/td-agent.confを以下のように設定しました。

/etc/td-agent/td-agent.conf(抜粋)

  :
<source>
  type tail
  format apache
  path /var/log/httpd/access_log
  tag kinesis.data
</source>
  :
<match kinesis.data>
  @type kinesis_firehose

  delivery_stream_name takipone-test

  #profile ...
  #credentials_path ...
  #aws_key_id ...
  #aws_sec_key ...
  region us-west-2
  #endpoint ...

  #data_key data (default: nil)

  # Put a data_key value if data_key is set
  # {... "data":"xxx" ...}
  # Put a record as JSON if data_key is not set

  #append_new_line true

  #include_time_key false
  #include_tag_key false

  flush_interval 1s
</match>

delivery_stream_nameにFirehoseのDelivery Stream名を指定します。IAMロールが未設定であれば、適宜AWS APIキーの設定を追加してください。Kinesis Streamsに比べ設定項目が非常に少ないのが特徴的ですね。

また、td-agentからApacheのログファイルが参照できるように、パーミッションを調整しておきます。

$ sudo chmod 2750 /var/log/httpd/
$ sudo chgrp td-agent /var/log/httpd/

これで準備OKです。

3. 動作確認

Apache、td-agentをそれぞれ起動し、abでApacheにアクセスしてみます。

$ sudo service td-agent start
Starting td-agent:                                         [  OK  ]
$ sudo service httpd start
Starting httpd:                                            [  OK  ]
$ ab -n 10000 http://localhost/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
  :
$

S3を確認してみると。。。

fluentd-firehose

アクセスログが転送されていました! 2ファイルになっているのはFirehoseのBuffer Clearのタイミングに差しかかり、後半のログが次のBuffer Clearに持ち越されたためのようです。スクリーンショットには入っていないですが、[Last Modified]のタイムスタンプがほぼ5分ずれており、Buffer Intervalの300sに合致することがわかります。

まとめ

FluentdのFirehoseプラグインで簡単にログファイルをFirehose経由でS3に転送する様子をご紹介しました。元々FluentdにはS3プラグインがありますが、Firehoseを経由することでFirehoseの持つスケーラビリティやバッファリング、データ圧縮などの付加機能が利用できます。FluentdからS3へのログ転送の有力な選択肢として要注目です!

参考URL