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のnginxとRedisでホストにマウントしたログをログローテーション

Last updated at Posted at 2019-12-12

AWS Workspaces
Amazon Linux2
nginx 1.16.1
Redis 5.0.7
blacklabelops/logrotate

mkdir -p log/nginx
mkdir -p log/redis
chmod 777 log/redis

docker run -d \
  --name nginx \
  -p 81:80 \
  -v $(pwd)/log/nginx:/var/log/nginx \
  nginx:1.16.1

docker run -d \
  --name redis \
  -p 6379:6379 \
  -v $(pwd)/log/redis:/var/log/redis \
  redis:5.0.7 bash -c "mkdir -p /var/log/redis && touch /var/log/redis/redis.log && redis-server --logfile /var/log/redis/redis.log"

docker run -d \
  --name logrotate \
  -v $(pwd)/log/nginx:/var/log/nginx \
  -v $(pwd)/log/redis:/var/log/redis \
  -e "LOGS_DIRECTORIES=/var/log/nginx/*.log /var/log/redis/*.log" \
  -e "LOGROTATE_COMPRESSION=compress" \
  -e "LOGROTATE_INTERVAL=hourly" \
  -e "LOGROTATE_CRONSCHEDULE=*/1 * * * * *" \
  -e "LOGROTATE_SIZE=10K" \
  -e "LOGROTATE_COPIES=3" \
  -e "LOGROTATE_DATEFORMAT=%Y%m%d%H%M%S" \
  blacklabelops/logrotate

ログローテーション動作確認
nginxはブラウザでアクセスの繰り返し
Redisはdocker restart redisの繰り返し

ls log/nginx

access.log
access.log20191212171100
access.log20191212170255.gz
error.log
error.log20191212171056
error.log20191212170254.gz

ls log/redis

redis.log
redis.log20191212171228
redis.log20191212170728.gz

logrotateのDockerコンテナ内に作成されていたファイル

copytruncateを使っています。
ファイルをrenameではなくcopyして、元のファイルの「内容」を
消すというものです。
コピー→削除の間に若干のタイムラグがあるので頻繁に書き込みが
行われている場合にはデータの欠損が発生する可能性があります。

usr/bin/logrotate.d/logrotate.conf
# deactivate mail
nomail

# move the log files to another directory?

/var/log/nginx/access.log {
 su root root
 rotate 3
 missingok
 compress
 delaycompress
 copytruncate
 hourly
 size 10K
 dateext
 dateformat %Y%m%d%H%M%S
}
/var/log/nginx/error.log {
 su root root
 rotate 3
 missingok
 compress
 delaycompress
 copytruncate
 hourly
 size 10K
 dateext
 dateformat %Y%m%d%H%M%S
}
/var/log/redis/redis.log {
 su root root
 rotate 3
 missingok
 compress
 delaycompress
 copytruncate
 hourly
 size 10K
 dateext
 dateformat %Y%m%d%H%M%S
}
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?