LoginSignup
2
1

More than 1 year has passed since last update.

FluentdでEC2からS3へログを転送

Last updated at Posted at 2021-09-30

これは何

FluentdでEC2からS3へログを転送したので、設定ファイルの書き方など書き残しました。

要件
Amazon Linux 2
td-agent4
old stableだし、3でもよかったけど、なんとなく4を選択

他にこの件で書かれている方がすでにいらっしゃって、参考にさせていただきました。
自分はハマったポイントが何点かあったので、そちらも含めてまとめます。

・S3 credentials
・configファイル設定で、もともとあるmatch directiveを削除しなければならなかった。

やってみた

S3 credentials用意

/// S3 credentials用意
IAMロール設定。EC2へアタッチ。
という方法だと、credentialsのエラーが出て進まなかったので、AmazonS3FullAccessを与えたIAMユーザーのAccess key,Secret keyを利用。

httpd install

yum install httpd
systemctl start httpd

Fluentd install

/// Fluentd install
$ curl -L https://toolbelt.treasuredata.com/sh/install-amazon2-td-agent4.sh | sh

Installed:
  td-agent.x86_64 0:4.2.0-1.amzn2

Complete!

Installation completed. Happy Logging!
↑どうでもいいですが、このノリ好きです

一応バックアップ

/// 一応バックアップ(後々ハマったので、しておいてよかったです)

cd /etc/td-agent/
cp td-agent.conf td-agent.conf.org

関連プラグインのインストール

///関連プラグインのインストール

/usr/sbin/td-agent-gem install fluent-plugin-s3
/usr/sbin/td-agent-gem install fluent-plugin-forest
/usr/sbin/td-agent-gem install fluent-plugin-ec2-metadata

権限変更

/// 権限変更
cd /var/log/
chmod 750 httpd/
chown  root.td-agent  httpd/
ls -ld httpd/

chmod  640 messages
chown  root.td-agent  messages
ls -ld messages

設定ファイルの編集

/// 設定ファイルの編集
vi /etc/td-agent/td-agent.conf

/match
で、該当の行を消去

最下行に以下追記
<source>
  @type tail
  path /var/log/messages
  tag td.messages.access
  pos_file /var/log/td-agent/messages.pos
  format syslog
</source>

<match td.messages.access>
  @type s3
  aws_key_id 
  aws_sec_key 
  s3_bucket バケットの名前
  s3_region ap-northeast-1(利用リージョンに合わせて、任意)
  time_slice_format %Y%m%d%H%M
</match>
  <buffer>
    @type file
    path /var/log/td-agent/s3
    timekey 3600  # 1 hour
    timekey_wait 10m
    chunk_limit_size 256m
  </buffer>

実行ユーザーをrootにする

/// 実行ユーザーをrootにする

vi /usr/lib/systemd/system/td-agent.service

[Service]
User=td-agent
Group=td-agent

↓変更

[Service]
User=root
Group=root

restart

/// 再起動

sudo systemctl restart td-agent.service

test

/// test

logger test

or

ab -n 100 -c 10 http://localhost/

転送成功

スクリーンショット 2021-09-30 19.05.13.png

参考

https://github.com/fluent/fluent-plugin-s3/issues/362
https://stackoverflow.com/questions/67365930/failed-to-flush-the-buffer-fluentd
https://teratail.com/questions/288682
https://docs.fluentd.org/how-to-guides/apache-to-s3
https://techgekka.hateblo.jp/entry/2016/11/08/195406
https://www.tweeeety.blog/entry/20131211/1386728410
https://dev.classmethod.jp/articles/fluentd-s3/
https://stackoverflow.com/questions/33182868/fluentd-s3-output-plugin-configuration
https://qiita.com/xishan/items/81736cb504b7b1ddf59a
https://qiita.com/y-araki-qiita/items/de6638ec7d32d09017c5
https://qiita.com/katuemon/items/7105c24271c07ce7b412
https://qiita.com/VTRyo/items/a4695aeec4fec8220fbb

参考にさせていただきました。ありがとうございます。

2
1
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
2
1