【Fluentd】ログ出力先をS3とする設定の作成【EC2】
ec2にインストールしたFluentdを使って指定のS3バケット内にmesseagesログとsecureログを
別々に出力したい。出力する際ログの出力先のフォルダ構造をEC2のタグとメタデータから取得するように設定したつもりが想定通りの動作にならない。
変数を使用しない場合はディレクトリの生成もログも問題なく出力される。
設定は全てroot権限で実施 EC2からS3のアクセス権限もポリシーで許可してある状態
conf設定のどの部分を修正すれば想定通りのフォルダ構造にできてログが出力されるのか
ご教授いただきたいです。
■.出力先バケット
s3://proxy-test-2023
■.収集対象のログ
/var/log/messeages
/var/log/secure
■.ログの出力先のフォルダ構造をEC2のタグとメタデータから取得するように設定する
/var/log/messages←収集先
⇒ s3://proxy-test-2023/logs/[TagKey:EnvのValue]/messages/[TagKey:ServiceNameのValue]/[TagKey:RoleのValue]/[年]/[月]/[日]/[EC2のインスタンスID]
/var/log/secure←収集先
⇒ s3://proxy-test-2023/logs/[TagKey:EnvのValue]/secure/[TagKey:ServiceNameのValue]/[TagKey:RoleのValue]/[年]/[月]/[日]/[EC2のインスタンスID]/
実施内容
curl -L https://toolbelt.treasuredata.com/sh/install-amazon2-td-agent4.sh | sh ←td-agent(fluentd)をインストール
td-agent-gem --version ←バージョン確認
3.1.6
■. pluginのインストール
td-agent-gem install fluent-plugin-s3
td-agent-gem install fluent-plugin-ec2-metadata
バージョン
fluent-plugin-ec2-metadata (0.1.3)
fluent-plugin-s3 (1.7.2)
■. 時間の設定
cp /etc/localtime /etc/localtime.old
cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
service syslog restart
■. aws configureの設定
aws configure
AWS Access Key ID :いつもの
AWS Secret Access Key :いつもの
Default region name :us-west-2
■. rootに変更する
vi /usr/lib/systemd/system/td-agent.service
[Unit]
Description=td-agent: Fluentd based data collector for Treasure Data
Documentation=https://docs.treasuredata.com/display/public/PD/About+Treasure+Data%%27s+Server-Side+Agent
After=network-online.target
Wants=network-online.target
[Service]
User=root ←ここをルートにする
Group=root ←ここをルートにする
LimitNOFILE=65536
■. td-agent.confの設定
# read messages logs
<source>
@type tail
<parse>
@type none
</parse>
path /var/log/messages
pos_file /var/log/td-agent/tmp/messages.pos
tag log.messages
</source>
# read secure logs
<source>
@type tail
<parse>
@type none
</parse>
path /var/log/secure
pos_file /var/log/td-agent/tmp/secure.pos
tag log.secure
</source>
# EC2 metadata
<filter log.**>
@type ec2_metadata
output_tag ${tag}_metadata
<record>
s3_path ${tagset['Env']}/${tagset['ServiceName']}/${tagset['Role']}/%Y/%m/%d/${record['instance_id']}/${tag[1]}/
</record>
</filter>
# send to S3
<match s3output.**>
@type s3
s3_bucket proxy-test-2023
s3_region us-west-2
path logs/${s3_path}
#time_slice_format %Y/%m/%d
s3_object_key_format %{path}/%{time_slice}/%{index}.%{file_extension}
<format>
@type single_value
</format>
<buffer tag,time>
@type file
path /var/log/td-agent/s3/${tag}
timekey 60s
timekey_wait 30
timekey_use_utc true
timekey_zone Asia/Tokyo
</buffer>
</match>
■. 再起動等
systemctl daemon-reload ←デーモンをリロード
td-agent --dry-run -c /etc/td-agent/td-agent.conf ← dry-runで確認
systemctl restart td-agent ←実行
0 likes