Ich habe einen Docker-Container, der nginx ausführt und Protokolle in /var/log/nginx
.__ schreibt. Logrotate ist im Docker-Container installiert und die logrotate-Konfigurationsdatei für nginx ist ordnungsgemäß eingerichtet. Die Protokolle werden jedoch nicht automatisch durch Logrotate gedreht. Das manuelle Erzwingen der Protokolldrehung zum Drehen der Protokolle über logrotate -f /path/to/conf-file
funktioniert wie erwartet.
Meine Schlussfolgerung ist, dass der Cron nicht durch etwas ausgelöst wird, aber ich kann den Grund dafür nicht finden.
Hier ist das Dockerfile für den Docker-Container, auf dem nginx ausgeführt wird:
FROM nginx:1.11
# Remove sym links from nginx image
RUN rm /var/log/nginx/access.log
RUN rm /var/log/nginx/error.log
# Install logrotate
RUN apt-get update && apt-get -y install logrotate
# Copy MyApp nginx config
COPY config/nginx.conf /etc/nginx/nginx.conf
#Copy logrotate nginx configuration
COPY config/logrotate.d/nginx /etc/logrotate.d/
Und die docker-compose -Datei:
version: '2'
services:
nginx:
build: ./build
restart: always
ports:
- "80:80"
- "443:443"
volumes:
- ./auth:/etc/nginx/auth
- ./certs:/etc/nginx/certs
- ./conf:/etc/nginx/conf
- ./sites-enabled:/etc/nginx/sites-enabled
- ./web:/etc/nginx/web
- nginx_logs:/var/log/nginx
logging:
driver: "json-file"
options:
max-size: "100m"
max-file: "1"
volumes:
nginx_logs:
networks:
default:
external:
name: my-network
Hier ist der Inhalt von: /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
dateext
missingok
rotate 30
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
prerotate
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi \
endscript
postrotate
[ -s /run/nginx.pid ] && kill -USR1 `cat /run/nginx.pid`
endscript
}
Inhalt von /etc/cron.daily/logrotate
#!/bin/sh
test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf
Inhalt von /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly
# keep 4 weeks worth of backlogs
rotate 4
# create new (empty) log files after rotating old ones
create
# uncomment this if you want your log files compressed
#compress
# packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own wtmp, or btmp -- we'll rotate them here
/var/log/wtmp {
missingok
monthly
create 0664 root utmp
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0660 root utmp
rotate 1
}
# system-specific logs may be configured here
Kann mir jemand in die richtige Richtung weisen, warum Nginx-Protokolle nicht automatisch durch Logrotate gedreht werden?
EDIT
Ich kann die Ursache des Problems auf den Cron-Dienst zurückführen, der nicht auf dem Container ausgeführt wird. Eine mögliche Lösung besteht darin, einen Weg zu finden, mit dem der Container gleichzeitig den Nginx- und den Cron-Service ausführen kann.
Wie in der Bearbeitung meiner Frage festgestellt, bestand das Problem darin, dass CMD
von nginx:1.11
nur den Prozess nginx
gestartet hat. Um dies zu umgehen, fügen Sie den folgenden Befehl in meine Docker-Datei ein
CMD service cron start && nginx -g 'daemon off;'
Dies startet nginx, da nginx:1.11
es startet und den cron-Dienst startet.
Die Dockerfile würde ungefähr so aussehen:
FROM nginx:1.11
# Remove sym links from nginx image
RUN rm /var/log/nginx/access.log
RUN rm /var/log/nginx/error.log
# Install logrotate
RUN apt-get update && apt-get -y install logrotate
# Copy MyApp nginx config
COPY config/nginx.conf /etc/nginx/nginx.conf
#Copy logrotate nginx configuration
COPY config/logrotate.d/nginx /etc/logrotate.d/
# Start nginx and cron as a service
CMD service cron start && nginx -g 'daemon off;'
ich habe eine Methode aus this link gefunden. Ihre Kernidee ist use_logrotate
außerhalb und conf ist wie folgt:
$ Sudo vi /etc/logrotate.d/test
/home/test/logs/*log {
rotate 90
missingok
ifempty
sharedscripts
compress
postrotate
docker exec -it nginx-test systemctl reload nginx > /dev/null 2>/dev/null || true
endscript
}