Closed10
ECSonFargateでFluentbitを使って複数箇所にログを転送

まとめ

FireLensについて

ECSonEC2の場合はS3でfluentbitのカスタム定義を管理できる

aws公式のfluentbitコンテナイメージ

re:PostでのAWS回答

ローカルからS3への転送ができない
CloudWatchLogsは飛んだが、S3に飛ばない
ecs-on-fargate-fluent-bit-log_router-1 | [2023/09/09 08:50:46] [error] [output:s3:s3.0] PutObject request failed
ecs-on-fargate-fluent-bit-log_router-1 | [2023/09/09 08:50:46] [error] [output:s3:s3.0] Could not send chunk with tag 25a79636ade9
ecs-on-fargate-fluent-bit-log_router-1 | [2023/09/09 08:50:56] [error] [output:s3:s3.0] PutObject API responded with error='InvalidAccessKeyId', message='The AWS Access Key Id you provided does not exist in our records.'
コンテ内で認証情報を取得するコマンド
$ curl --max-time 5 169.254.170.2${AWS_CONTAINER_CREDENTIALS_RELATIVE_URI}
{"AccessKeyId":"ASIAUDHMHFW*********","Expiration":"2023-09-09T09:53:24Z","RoleArn":"","SecretAccessKey":"rX18f4bjD+dJP7u1wWjINa083**********","Token":"*********"}
取れてる。
重要: 環境変数 AWS_CONTAINER_CREDENTIALS_RELATIVE_URI は、コンテナ内の PID 1 プロセスでのみ使用できます。コンテナが複数のプロセスまたは初期化プロセス (ラッパースクリプト、起動スクリプト、スーパーバイザーなど) を実行している場合、PID 1 以外のプロセスでは環境変数を使用できません。これらのプロセスが AWS サービスにアクセスしようとすると、「アクセス拒否」エラーが発生する可能性があります。PID 1 以外のプロセスでも使用できるように環境変数を設定するには、環境変数を .profile ファイルにエクスポートします。たとえば、次のコマンドを実行して、コンテナイメージの Dockerfile 内の変数をエクスポートします。
bash-4.2# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 08:45 ? 00:00:00 /fluent-bit/bin/fluent-bit -e /fluent-bit/firehose.so -e /fluent-bit/cloudwatch.so -e /fluent-bit/kinesis.so -c /fluent-bit/etc/fluent-bit.conf
root 29 0 0 08:52 pts/0 00:00:00 bash
root 41 29 0 09:03 pts/0 00:00:00 ps -ef
debugで起動すると、CloudWatchLogがOUTに設定している時は以下の処理が出ずに、S3をOUTに定義してる時は出る
ecs-on-fargate-fluent-bit-log_router-1 | [2023/09/09 09:14:31] [debug] [aws_credentials] Initialized Env Provider in standard chain
ecs-on-fargate-fluent-bit-log_router-1 | [2023/09/09 09:14:31] [debug] [aws_credentials] Initialized AWS Profile Provider in standard chain
ecs-on-fargate-fluent-bit-log_router-1 | [2023/09/09 09:14:31] [debug] [aws_credentials] Not initializing EKS provider because AWS_ROLE_ARN was not set
ecs-on-fargate-fluent-bit-log_router-1 | [2023/09/09 09:14:31] [debug] [aws_credentials] Configuring HTTP provider with 169.254.170.2:80/creds
ecs-on-fargate-fluent-bit-log_router-1 | [2023/09/09 09:14:31] [debug] [aws_credentials] Initialized ECS Provider in standard chain
ecs-on-fargate-fluent-bit-log_router-1 | [2023/09/09 09:14:31] [debug] [aws_credentials] Initialized EC2 Provider in standard chain
ecs-on-fargate-fluent-bit-log_router-1 | [2023/09/09 09:14:31] [debug] [aws_credentials] Sync called on the http provider
ecs-on-fargate-fluent-bit-log_router-1 | [2023/09/09 09:14:31] [debug] [aws_credentials] Sync called on the EC2 provider
ecs-on-fargate-fluent-bit-log_router-1 | [2023/09/09 09:14:31] [debug] [aws_credentials] Init called on the env provider
ecs-on-fargate-fluent-bit-log_router-1 | [2023/09/09 09:14:31] [debug] [aws_credentials] Init called on the profile provider
ecs-on-fargate-fluent-bit-log_router-1 | [2023/09/09 09:14:31] [debug] [aws_credentials] Reading shared config file.
ecs-on-fargate-fluent-bit-log_router-1 | [2023/09/09 09:14:31] [debug] [aws_credentials] Shared config file /root/.aws/config does not exist
ecs-on-fargate-fluent-bit-log_router-1 | [2023/09/09 09:14:31] [debug] [aws_credentials] Reading shared credentials file.
ecs-on-fargate-fluent-bit-log_router-1 | [2023/09/09 09:14:31] [debug] [aws_credentials] Shared credentials file /root/.aws/credentials does not exist
ecs-on-fargate-fluent-bit-log_router-1 | [2023/09/09 09:14:31] [debug] [aws_credentials] Init called on the http provider
roug_routerコンテナにAWS権限がうまく付与されていないのが原因だった。

記事から自分の環境で確認すること
- log_routerのヘルチェックを設定したか?
-
SERVICE設定の追加
- ヘルスチェック
- フラッシュ間隔
- 停止待機時間

S3に関するメトリクスのissue

compression gzip設定しているのに圧縮されていない
公式のS3向け定義サンプルだとcompressionが入ってない
- "total_file_size": "1M",
- "upload_timeout": "1m",
- "use_put_object": "On",
- "retry_limit": "2"
fluentbitのマニュアル上のFargate用サンプルも圧縮していない
https://docs.fluentbit.io/manual/pipeline/outputs/s3#using-s3-without-persisted-disk
[OUTPUT]
Name s3
Match *
bucket your-bucket
region us-east-1
total_file_size 1M
upload_timeout 1m
use_put_object On
S3と他のFluentBit出力との違い
- FluentBitがログを受信すると設定に応じてメモリかファイルシステムにちゃんで保存する
- チャンクサイズは通常2MB程度
- ほとんどの出力は、チャンクをすぐに宛先に送る
- S3のユースケースは大きいファイルをアップロードすることであり、一般的には2MBよりはるかに大きい
- S3は独自のバッファリングシステムとデータをアップロードするための独自のコールバックを持つ
- S3は常に書き込み可能なファイルシステムを必要とする
- マルチアップロードの場合はstore_dirにデータをバッファリングする
- Fargateなど永続領域がない場合は、PutObjectAPIを使用して、ローカルバッファリングを避けて頻繁にデータを送信する

Flentbit S3 plugin
このスクラップは2024/04/05にクローズされました