如何为Traefik启用日志轮转?日志轮转是否为内置功能?
Great question! Let's start with the key point straight from the docs: Traefik does NOT have built-in log rotation functionality. As the official notes state, it expects external tools like logrotate (a standard utility on most Linux systems) to handle log rotation and management.
Now, let's walk through exactly how to set this up for both Traefik's core logs and your access.log:
1. Using logrotate (For Traefik Running Directly on Host)
Step 1: Verify logrotate is installed
First, check if logrotate is already on your system:
logrotate --version
If it's missing, install it via your package manager:
- Debian/Ubuntu:
sudo apt install logrotate - RHEL/CentOS/Fedora:
sudo dnf install logrotate
Step 2: Create a Traefik Log Rotation Config File
Create a dedicated config file for Traefik in the logrotate.d directory (this is where system-wide log rotation rules live):
sudo nano /etc/logrotate.d/traefik
Paste in the following configuration (adjust paths and settings to match your Traefik setup):
/var/log/traefik/*.log { daily # Rotate logs every day missingok # Don't throw an error if a log file is missing rotate 7 # Keep 7 days of rotated logs compress # Compress old logs with gzip delaycompress # Delay compression until the next rotation cycle notifempty # Skip rotation if the log file is empty create 0640 root adm # Create new log files with correct permissions postrotate # Send USR1 signal to Traefik to reopen log files (critical!) kill -USR1 $(cat /var/run/traefik.pid) endscript }
Let's break down the important bits:
- The
postrotateblock is essential: Traefik holds open file handles to its logs, so sending theUSR1signal tells it to close and reopen the log files. This ensures it writes to the new, rotated file instead of the old one thatlogrotatehas renamed. - Adjust the path
/var/log/traefik/*.logto match where your Traefik logs (includingaccess.log) are stored.
Step 3: Test the Configuration
Run a dry run to make sure your config works without making changes:
sudo logrotate -d /etc/logrotate.d/traefik
If no errors appear, run the rotation manually once to verify:
sudo logrotate -f /etc/logrotate.d/traefik
2. Log Rotation for Traefik Running in Docker
If you're running Traefik in a container, you have two main options:
Option A: Mount Logs to Host and Use logrotate
If you've mounted Traefik's log directory from the host into the container (e.g., -v /var/log/traefik:/var/log/traefik in your docker run command), you can use the exact same logrotate setup as above. Just ensure the host path in the config matches your mount point.
Option B: Use Docker's Built-In Log Rotation
Docker has native support for log rotation when using the json-file log driver (the default). To enable it:
- Edit or create the Docker daemon config file:
sudo nano /etc/docker/daemon.json
- Add the following configuration (adjust
max-sizeandmax-fileto your needs):
{ "log-driver": "json-file", "log-opts": { "max-size": "10m", "max-file": "5" } }
max-size: Maximum size of a single log file before rotation (e.g.,10mfor 10MB)max-file: Number of rotated log files to keep
- Restart Docker to apply the changes:
sudo systemctl restart docker
This will handle rotation for all Docker containers (including Traefik) using the json-file driver.
Key Recap
- Traefik does not include built-in log rotation—you must use external tools like
logrotateor Docker's native logging features. - For host-based Traefik deployments,
logrotateis the most flexible solution, and theUSR1signal is critical to ensure Traefik switches to new log files after rotation. - For Docker deployments, either mount logs to the host and use
logrotate, or leverage Docker's built-in rotation for simpler setup.
内容的提问来源于stack exchange,提问作者Robert Lachner

