Post

fluentdからs3への送信をローカルで検証したい

概要

dockerで fluentdminio を立てて、S3へのログの送信の検証を行う。

構成

. ├── docker-compose.yml ├── fluentd │ ├── Dockerfile │ └── conf │ └── fluentd.conf └── log └── development.log

fluentdは v0.12 系を使用しています。(バージョンによってconfの書き方が異なる)

GitHub - fluent/fluent-plugin-s3: Amazon S3 input and output plugin for Fluentd

設定

docker

FROM fluent/fluentd:v0.12.40 ADD conf/fluentd.conf /fluentd/etc/ # for v0.12 RUN gem install fluent-plugin-s3 -v "~> 0.8" --no-document RUN mkdir -p /var/log/fluent/s3 && \ mkdir -p /var/log/td-agent && \ chmod 777 /var/log/*

docker-compose

version: '3' services: fluentd: build: fluentd/ ports: - "24224:24224" volumes: - ./fluentd/conf:/fluentd/etc - ./log:/var/log/rails environment: FLUENTD_CONF: fluentd.conf depends_on: - miniomc minio: image: minio/minio:RELEASE.2020-01-25T02-50-51Z volumes: - minio_volume:/data ports: - "9090:9000" environment: MINIO_ACCESS_KEY: minio_access_key MINIO_SECRET_KEY: minio_secret_key command: server /data miniomc: image: minio/mc:RELEASE.2020-01-25T03-02-19Z depends_on: - minio entrypoint: > /bin/sh -c " until (/usr/bin/mc config host add myminio http://minio:9000 minio_access_key minio_secret_key) do echo '...waiting...' && sleep 1; done; /usr/bin/mc mb myminio/fluentdtest --region=ap-northeast-1; /usr/bin/mc policy set public myminio/fluentdtest; exit 0; " volumes: minio_volume:

fluentdのconf

<source> @type forward </source> <source> @type tail format none path /var/log/rails/development.log pos_file /var/log/td-agent/rails.log.pos tag log </source> <match log> @type s3 aws_key_id minio_access_key aws_sec_key minio_secret_key s3_bucket fluentdtest s3_endpoint http://minio:9000/ s3_region ap-northeast-1 path logs/ # This prefix is added to each file force_path_style true # This prevents AWS SDK from breaking endpoint URL s3_object_key_format %{path}%{hostname}_%{time_slice}_%{index}.%{file_extension} buffer_path /var/log/fluent/s3 time_slice_format %Y%m%d-%H time_slice_wait 10m utc format single_value flush_at_shutdown </match> <match fluent.info.**> @type stdout </match> <match **> @type copy <store> @type file path /home/fluent/logs/debug </store> <store> @type stdout </store> </match>

ログ

log/development.log には文字を適当に書き込む

Send Apache Logs to Minio - Fluentd

td-agent でログ全文は S3 へ保存しつつ、特定文字列がマッチした場合 slack に通知する方法 - akms道東

tips

bucket名にアンダースコアが含まれている

バケットの生成に失敗するのでアンダースコアは使用しない

miniomc_1 | mc: <ERROR> Unable to make bucket `myminio/fluentd_test`. Bucket name contains invalid characters miniomc_1 | mc: <ERROR> Unable to set policy `public` for `myminio/fluentd_test`. The specified bucket does not exist.

indexはapiを通して実現しているのでお金がかかる

Fluentdのs3_object_key_formatに%{index}を使わないほうがいい - grep Tips *

タグ名を使用してpathを生成したい

${tag[1]}

Fluentd v0.14 では fluent-plugin-forest が不要な話 - Usual Software Engineer

This post is licensed under CC BY 4.0 by the author.