0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DockerのMaxscaleでホストにマウントしたログをログローテーション (1)

Last updated at Posted at 2019-12-15

 現在の問題

supeervisorのログローテーションに対応してない

Dockerでcronを動かす方法について

今回は「2」です

1.コンテナ内でcronを直接起動する
2.コンテナ内でsupervisordからcronを起動する
3.ホストのcronからコンテナ内の対象プログラムを実行する

動作確認環境

AWS Workspaces
Amazon Linux2 (Time zone: Asia/Tokyo (JST, +0900))
Maxscale 2.4.4

ディレクトリ構成

home/username/logrotate
|--log
|  |--maxscale
|  |  |--maxscale.log
|--maxscale
|  |--maxscale_logrotate
|--maxscale-dockerfiles
|  |--Dockerfile
|  |--maxscale-cron

ファイル

maxscale-cron
*/1 * * * * root /usr/sbin/logrotate -f /etc/logrotate.d/maxscale_logrotate >> /var/log/cron.log 2>&1
logrotate/maxscale-dockerfiles/Dockerfile
FROM mariadb/maxscale:2.4.4

RUN apt-get update && apt-get -y install logrotate cron supervisor

RUN mv /etc/cron.daily/logrotate /etc/cron.hourly/logrotate
ADD maxscale-cron /etc/cron.d/maxscale-cron
RUN chmod 0644 /etc/cron.d/maxscale-cron
RUN touch /var/log/cron.log

RUN touch /etc/supervisord.conf
RUN echo '[supervisord]'  >> /etc/supervisord.conf
RUN echo 'nodaemon=true'  >> /etc/supervisord.conf
RUN echo 'logfile=/var/log/supervisor/supervisord.log'  >> /etc/supervisord.conf
RUN echo 'pidfile=/var/run/supervisord.pid'  >> /etc/supervisord.conf
RUN echo '[program:maxscale]' >> /etc/supervisord.conf
RUN echo 'command=/usr/bin/maxscale -d -U maxscale -l file'   >> /etc/supervisord.conf
RUN echo '[program:cron]'  >> /etc/supervisord.conf
RUN echo 'command=/usr/sbin/cron -f'  >> /etc/supervisord.conf
RUN echo 'autostart=true'  >> /etc/supervisord.conf
RUN echo 'autorestart=true'  >> /etc/supervisord.conf
RUN echo 'stdout_logfile=/var/log/cron.log'  >> /etc/supervisord.conf
RUN echo 'stderr_logfile=/var/log/cron.log'  >> /etc/supervisord.conf

CMD /usr/bin/supervisord -c /etc/supervisord.conf
logrotate/maxscale/maxscale_logrotate
/var/log/maxscale/maxscale.log {
  su maxscale maxscale
  rotate 3
  missingok
  compress
  delaycompress
  copytruncate
  sharedscripts
  hourly
  size 1K
  dateext
  dateformat %Y%m%d%H%M%S
  postrotate
    kill -USR1 `cat /var/run/maxscale/maxscale.pid`
  endscript
}

起動・動作確認

# Dockerイメージ作成
$ cd maxscale-dockerfiles
$ docker build -t maxscale-log:2.4.4 .

# ログディレクトリに権限設定
$ cd ..
$ chmod 777 log/maxscale

# logrotate.confのownerがrootじゃなかったためにずっとlogrotateが動いていなかった(cronでアラートも飛ばない)
# http://tyru.hatenablog.com/entry/20140507/logrotate_didnt_rotate_logs
$ sudo chown root:root maxscale\maxscale_logrotate

# MaxscaleのDockerコンテナ起動
$ docker run \
-d \
--name maxscale \
-v $(pwd)/log/maxscale:/var/log/maxscale \
-v $(pwd)/maxscale/maxscale_logrotate:/etc/logrotate.d/maxscale_logrotate \
maxscale-log:2.4.4

# 少ししてからログ確認
$ ls -l log/maxscale/

合計 12
-rw-r--r-- 1 chrony ssh_keys 187 12月 17 01:38 maxscale.log
-rw-r--r-- 1 chrony ssh_keys 649 12月 17 01:37 maxscale.log20191216163701.gz
-rw-r--r-- 1 chrony ssh_keys 187 12月 17 01:38 maxscale.log20191216163801

参考URL

Docker視点で見るSupervisorの使い方
Dockerでsupervisorを使う時によくハマる点まとめ
Docker + Cron環境を実現する3つの方法
dockerコンテナ上のuwsgiとnginxのログローテート
DockerfileでNginxの起動とログのローテーションまで
debian ベースの Docker コンテナで busybox の cron を実行
Docker で /etc/cron.d を使って cron を実行する
logrotateをテスト実行する

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?