0
0

More than 3 years have passed since last update.

AWSでwordpressを動かす6_td-agentの導入

Posted at

本記事のゴール

td-agentを用いてマスターサーバにapacheログを集積し、s3へ送信する

S3にバケットを用意

設定 内容
パブリックアクセス 全てブロック
Lifecycle 直近30日以上は削除

EC2->S3へのアクセス確認

シークレットキーはセキュリティ上使用したくないので、S3へのアクセスロールをアタッチする
疎通確認

# aws s3 ls s3://バケット名

ローカル用のログローテート設定

とりあえず1日ごと30日間のローテート

# chmod 655 /var/log/httpd
# vi /etc/logrotate.d/httpd
# logrotate -f /etc/logrotate.d/httpd
# logrotate /etc/logrotate.conf
/etc/logrotate.d/httpd
/var/log/httpd/*log {
    daily
    rotate 30
    missingok
    notifempty
    sharedscripts
    delaycompress
    create 644 apache apache
    postrotate
        /bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true
    endscript
}

td-agentのインストール

※マスターサーバのみ、事前に24224ポートのインバウンドを許可する

fluentd公式からインストールコマンドを実行
https://docs.fluentd.org/installation/install-by-rpm#amazon-linux

# curl -L https://toolbelt.treasuredata.com/sh/install-amazon2-td-agent3.sh | sh
# cp /etc/td-agent/td-agent.conf /etc/td-agent/td-agent.conf.org
# vi /etc/td-agent/td-agent.conf
# systemctl enable td-agent
# systemctl restart td-agent
/etc/td-agent/td-agent.conf(マスターサーバ)
<source>
    @type forward
    port 24224
</source>

<filter {httpd.access,httpd.error}>
  type record_transformer
  <record>
    host ${hostname}
  </record>
</filter>

<source>
  @type tail
  path /var/log/httpd/access_log
  format none
  pos_file /var/log/td-agent/httpd-access.pos
  tag httpd.access
</source>

<source>
  @type tail
  path /var/log/httpd/error_log
  format none
  pos_file /var/log/td-agent/httpd-error.pos
  tag httpd.error
</source>

<match {httpd.access,httpd.access.slave}>
  @type s3
  s3_bucket hogehoge-bucket-name
  s3_region us-west-2
  time_slice_format %Y%m%d%H
  path httpd_log/access/
  buffer_type file
  buffer_chunk_limit 10m
  buffer_queue_limit 10m
  flush_interval 1h
  flush_at_shutdown true
  buffer_path /var/log/td-agent/httpd/buffer1/*.buffer
</match>

<match {httpd.error,httpd.error.slave}>
  @type s3
  s3_bucket hogehoge-bucket-name
  s3_region us-west-2
  time_slice_format %Y%m%d%H
  path httpd_log/error/
  buffer_type file
  buffer_chunk_limit 10m
  buffer_queue_limit 10m
  flush_interval 1h
  flush_at_shutdown true
  buffer_path /var/log/td-agent/httpd/buffer2/*.buffer
</match> 
/etc/td-agent/td-agent.conf(スレーブサーバ)
<filter {httpd.access.slave,httpd.access.slave}>
  type record_transformer
  <record>
    host ${hostname}
  </record>
</filter>

<source>
  @type tail
  path /var/log/httpd/access_log
  format none
  pos_file /var/log/td-agent/httpd-access.pos
  tag httpd.access.slave
</source>

<source>
  @type tail
  path /var/log/httpd/error_log
  format none
  pos_file /var/log/td-agent/httpd-error.pos
  tag httpd.error.slave
</source>

<match httpd.access.slave>
  <server>
    host 123.45.67.89
    port 24224
  </server>
  @type forward
</match>

<match httpd.error.slave>
  <server>
    host 123.45.67.89
    port 24224
  </server>
  @type forward
</match>

適当なログを流して、S3にログが送られることが確認できればOK

0
0
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
0
0