LoginSignup
3
2

More than 5 years have passed since last update.

logrotateプロセスをdocker化

Last updated at Posted at 2017-08-04

インフラのMicroservice一貫でlogroateプロセスをdocker化してみました。

ファイル構成

ディレクトリ構成

Dockerfile
logrotate.conf
logrotate.d/hoge

Dockerfile

FROM sequenceiq/logrotate 
ADD logrotate.d /etc/logrotate.d/
ADD logrotate.conf /etc/logrotate.conf

logrotate.conf

weekly
su root
rotate 4
create
include /etc/logrotate.d

logrotate.d/hoge

/rails/shared/log/rails.log
/var/log/nginx.log
{
  daily
  rotate 7
  missingok
  compress
  ifempty
  delaycompress
  su root
  copytruncate
  sharedscripts
}

Docker image build&&push

私の場合ECRを使ってDockerイメージを管理してるので以下の手順を実行します。

ECRログイン

eval "$(aws ecr get-login --region ap-northeast-1)"

docker build

docker build -t xx.dkr.ecr.ap-northeast-1.amazonaws.com/logrotate .

docker push

docker push xx.dkr.ecr.ap-northeast-1.amazonaws.com/logrotate

docker run

以下のコマンドでdockerコンテナを立ち上げます。

docker run -d \
    -v /var/log:/var/log \
        -v /rails/shared/log:/rails/shared/log \
  -e CRON_EXPR="* * * * *" \
  xx.dkr.ecr.ap-northeast-1.amazonaws.com/logrotate

その他いろんななオプションが使えます。

docker run -d \
  -v /var/log:/var/log \
  -v /rails/shared/log:/rails/shared/log \
  -e "LOGS_DIRECTORIES=/var/log /var/log/nginx" \
  -e "LOGROTATE_COPIES=10" \
  -e "LOGROTATE_SIZE=10G" \
  -e "LOGROTATE_COMPRESSION=compress" \
  -e "LOGROTATE_INTERVAL=daily" \
  -e "LOGROTATE_DATEFORMAT=-%Y%m%d" \
  xx.dkr.ecr.ap-northeast-1.amazonaws.com/logrotate

ECSを利用してる場合

ECSを利用してる場合は以下の手順でtask-definitionが作れます。

task_def.jsonファイル

{
    "family": "logrotate",
    "containerDefinitions": [
        {
            "name": "logrotate",
            "image": "xx.dkr.ecr.ap-northeast-1.amazonaws.com/logrotate",
            "cpu": 0,
            "memoryReservation": 32,
             "environment": [
            {
              "name": "CRON_EXPR",
               "value": "* * * * *"
            }
            ],
             "mountPoints": [
               {
                 "sourceVolume": "common_log",
                 "containerPath": "/var/log"
               },
               {
                 "sourceVolume": "rails",
                 "containerPath": "/rails/shared/log"
               }
            ],
            "dockerLabels": {
                "title": "",
                "p-r": ""
            }
        }
    ],
     "volumes": [
    {
    "name": "common_log",
    "host": {
      "sourcePath": "/var/log"
      }
    },
    {
    "name": "rails",
    "host": {
      "sourcePath": "/rails/shared/log"
      }
    }
    ]
}

task-def作成

aws ecs register-task-definition --cli-input-json file://task_def.json

クラスターにdockerコンテナをアップする手順は省略します。

感想

案外にシンプルで簡単構築できました。

logrotateの設定をdockerのコードで管理できるようになりました!!
task-defのバージョン番号だけみれば最新のlogroate設定になってることを確認できるので
対応漏れ減らせそう

ただ、同じ設定なのにサーバごとにコンテナ立ち上げるのちょっとめんどい。。。各コンテナが落ちてないか監視も必要だろうし

場合によってはNFSやrsyncとかでログをすべて一箇所に集めてそこにlogrotate用dockerコンテナを立ち上げておくこともよいかもです。

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