5
8

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コンテナ上のuwsgiとnginxのログローテート

Last updated at Posted at 2018-03-28

はじめに

コンテナ上にあるAppサーバ(uwsgi)とWebサーバ(nginx)のログローテートを設定する際にいろいろハマったためそのメモ

実行環境

Ubuntu 16.04 LTS
docker 18.03-ce
uwsgi 2.0.17
nginx 1.13.5

ハマったところ

コンテナのcronでうまく動かなかった。
crontabの時間を変更する際、2分以上先の時間を設定する必要があった。
※wikiに書いてありました。(https://ja.wikipedia.org/wiki/Crontab)
ログの開き直しの設定

設定

Appサーバ(uwsgi)

uwsgi.ini
[uwsgi]
module = main
callable = app
master = true
processes = 1
socket = /tmp/uwsgi.sock
chmod-socket = 666
vacuum = true
die-on-term = true
buffer-size = 32768

logto = /var/log/uwsgi/%n.log

touch-reload = /tmp/reload.trigger
touch-logreopen=/tmp/logreopen.trigger

/etc/logrotate.d/uwsgiには以下のように記述

"/home/myname/docker-compose/log/uwsgi/uwsgi.log" {
  missingok
  notifempty
  daily
  rotate 7
  dateext
  dateformat _%Y%m%d
  create 644 root root
  compress
  delaycompress
  notifempty
  sharedscripts
  postrotate
      touch /mnt/hdd/docker-data/logreopen.trigger
  endscript
}

以下のようにボリュームのマウントをしています。

ホスト コンテナ
/mnt/hdd/docker-data /tmp
/home/myname/docker-compose/log/uwsgi /var/log/uwsgi

Webサーバー(nginx)

default.conf
server {
    listen 80;
    server_name _;

    access_log /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:///tmp/uwsgi.sock;
  }
}

/etc/logrotate.d/nginxには以下のように記述

"/home/myname/docker-compose/log/nginx/access.log" "/home/myname/docker-compose/log/nginx/error.log" {
  missingok
  notifempty
  daily
  rotate 7
  dateext
  dateformat _%Y%m%d
  create 644 root root
  compress
  delaycompress
  notifempty
  sharedscripts
  postrotate
      docker kill -s USR1 dockercompose_web_1
  endscript
}

以下のようにボリュームのマウントをしています。

ホスト コンテナ
/mnt/hdd/docker-data /tmp
/home/myname/docker-compose/log/nginx /var/log/nginx

確認

/var/lib/logrotate/statusの中にある対象のログの日付を過去にします。

対象ログ
/home/myname/docker-compose/log/uwsgi/uwsgi.log
/home/myname/docker-compose/log/nginx/access.log
/home/myname/docker-compose/log/nginx/error.log

logrotateをデバッグ実行してみます。

/usr/sbin/logrotate -d /etc/logrotate.conf

結果はuwsgi.log、access.log、error.logの部分のみ抜粋

rotating pattern: "/home/myname/docker-compose/log/uwsgi/uwsgi.log" after 1 days (7 rotations)
empty log files are not rotated, old logs are removed
switching euid to 0 and egid to 108
considering log /home/myname/docker-compose/log/uwsgi/uwsgi.log
log needs rotating
rotating log /home/myname/docker-compose/log/uwsgi/uwsgi.log, log->rotateCount is 7
Converted ' %Y%m%d' -> '%Y%m%d'
dateext suffix '20180328'
glob pattern '
[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding logs to compress failed
glob finding old rotated logs failed
renaming /home/myname/docker-compose/log/uwsgi/uwsgi.log to /home/myname/docker-compose/log/uwsgi/uwsgi.log_20180328
creating new /home/myname/docker-compose/log/uwsgi/uwsgi.log mode = 0644 uid = 0 gid = 0
running postrotate script
running script with arg "/home/myname/docker-compose/log/uwsgi/uwsgi.log" : "
touch /mnt/hdd/docker-data/logreopen.trigger
"
switching euid to 0 and egid to 0

rotating pattern: "/home/myname/docker-compose/log/nginx/access.log" "/home/myname/docker-compose/log/nginx/error.log" after 1 days (7 rotations)
empty log files are not rotated, old logs are removed
switching euid to 0 and egid to 108
considering log /home/myname/docker-compose/log/nginx/access.log
log needs rotating
considering log /home/myname/docker-compose/log/nginx/error.log
log needs rotating
rotating log /home/myname/docker-compose/log/nginx/access.log, log->rotateCount is 7
Converted ' %Y%m%d' -> '%Y%m%d'
dateext suffix '20180328'
glob pattern '
[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding logs to compress failed
glob finding old rotated logs failed
rotating log /home/myname/docker-compose/log/nginx/error.log, log->rotateCount is 7
Converted ' %Y%m%d' -> '%Y%m%d'
dateext suffix '20180328'
glob pattern '
[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding logs to compress failed
glob finding old rotated logs failed
renaming /home/myname/docker-compose/log/nginx/access.log to /home/myname/docker-compose/log/nginx/access.log_20180328
creating new /home/myname/docker-compose/log/nginx/access.log mode = 0644 uid = 0 gid = 0
renaming /home/myname/docker-compose/log/nginx/error.log to /home/myname/docker-compose/log/nginx/error.log_20180328
creating new /home/myname/docker-compose/log/nginx/error.log mode = 0644 uid = 0 gid = 0
running postrotate script
running script with arg "/home/myname/docker-compose/log/nginx/access.log" "/home/myname/docker-compose/log/nginx/error.log" : "
docker kill -s USR1 dockercompose_web_1
"
switching euid to 0 and egid to 0

設定は大丈夫そうなので、強制実行してみます。

/etc/cron.daily/logrotate

ログディレクトリを見ると、正しくローテートされてます。
また、Appサーバのアプリにブラウザからアクセスすると、ローテート後のログファイルにログが書き込まれましたので、uwsgi、nginxともにログファイルが開きなおされていることが確認できました!

参考

dockerコンテナ内のnginxにログを開き直させる方法(logrotate)
nginx に USR1 シグナルを送ってログを開き直す

5
8
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
5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?