![systemd-journald 不会在运行 Docker 的系统上保留日志](https://linux22.com/image/207109/systemd-journald%20%E4%B8%8D%E4%BC%9A%E5%9C%A8%E8%BF%90%E8%A1%8C%20Docker%20%E7%9A%84%E7%B3%BB%E7%BB%9F%E4%B8%8A%E4%BF%9D%E7%95%99%E6%97%A5%E5%BF%97.png)
大约每隔一小时,systemd-journald 就会删除任何早于大约一小时的日志,而不是将它们保存到 /var/log/journal/ 中。我在journal.conf中设置了storage=persistent。每当日志旋转时,它们都不会被刷新。
systemd-journald 覆盖以在 dmesg 中显示调试日志:
pi@######:/etc/systemd/system/systemd-journald.service.d$ cat override.conf
[Service]
Environment=SYSTEMD_LOG_LEVEL=debug
这些是删除时 dmesg 中出现的日志:
[Tue Jun 14 11:13:33 2022] systemd-journald[2020]: /run/log/journal/e00bd6223a7245d0aa6e19e0ab12a024/system.journal: Journal header limits reached or header out-of-date, rotating.
[Tue Jun 14 11:13:33 2022] systemd-journald[2020]: Rotating...
[Tue Jun 14 11:13:33 2022] systemd-journald[2020]: Reserving 8448 entries in hash table.
[Tue Jun 14 11:13:33 2022] systemd-journald[2020]: Vacuuming...
[Tue Jun 14 11:13:33 2022] systemd-journald[2020]: Deleted archived journal /run/log/journal/e00bd6223a7245d0aa6e19e0ab12a024/system@d30583b2cbf04d2aab3e5927a175ef5e-0000000000065a39-0005e163dbe0ff99.journal (4.6M).
[Tue Jun 14 11:13:33 2022] systemd-journald[2020]: Vacuuming done, freed 4.6M of archived journals from /run/log/journal/e00bd6223a7245d0aa6e19e0ab12a024.
我不明白的一件主要事情是,当我向journalctl发送命令时,为什么它在journalctl中显示日志已停止?您可以在下面的日期看到它停止并且再也没有开始。我猜这就是日志没有被刷新的原因?
系统日志日志:
pi@#######:~$ date
Wed Jun 15 11:06:27 SAST 2022
pi@#######:~$ sudo journalctl -u systemd-journald | cat
-- Logs begin at Mon 2022-01-10 06:56:38 SAST, end at Wed 2022-06-15 11:06:34 SAST. --
Jan 10 06:56:39 ####### systemd-journald[2019]: Journal started
Jan 10 06:56:39 ####### systemd-journald[2019]: Runtime Journal (/run/log/journal/e00bd6223a7245d0aa6e19e0ab12a024) is 4.6M, max 37.1M, 32.4M free.
-- Reboot --
Jan 10 06:56:39 ####### systemd-journald[2011]: Journal started
Jan 10 06:56:39 ####### systemd-journald[2011]: Runtime Journal (/run/log/journal/e00bd6223a7245d0aa6e19e0ab12a024) is 4.6M, max 37.1M, 32.4M free.
Jan 10 06:56:39 ####### systemd-journald[2011]: Runtime Journal (/run/log/journal/e00bd6223a7245d0aa6e19e0ab12a024) is 4.6M, max 37.1M, 32.4M free.
-- Reboot --
Jan 10 06:56:39 ####### systemd-journald[2019]: Runtime Journal (/run/log/journal/e00bd6223a7245d0aa6e19e0ab12a024) is 4.6M, max 37.1M, 32.4M free.
Jun 13 12:16:53 ####### systemd-journald[2019]: Journal stopped
Jun 13 12:16:53 ####### systemd-journald[7765]: Journal started
Jun 13 12:16:53 ####### systemd-journald[7765]: Runtime Journal (/run/log/journal/e00bd6223a7245d0aa6e19e0ab12a024) is 18.5M, max 37.1M, 18.5M free.
Jun 13 12:16:53 ####### systemd-journald[7765]: Time spent on flushing to /var/log/journal/e00bd6223a7245d0aa6e19e0ab12a024 is 455.515ms for 8948 entries.
Jun 13 12:16:53 ####### systemd-journald[7765]: System Journal (/var/log/journal/e00bd6223a7245d0aa6e19e0ab12a024) is 24.0M, max 900.0M, 876.0M free.
Jun 13 12:19:39 ####### systemd-journald[7765]: Journal stopped
Jun 13 12:19:39 ####### systemd-journald[8468]: Journal started
Jun 13 12:19:39 ####### systemd-journald[8468]: Runtime Journal (/run/log/journal/e00bd6223a7245d0aa6e19e0ab12a024) is 4.6M, max 37.1M, 32.4M free.
Jun 13 12:19:39 ####### systemd-journald[8468]: Time spent on flushing to /var/log/journal/e00bd6223a7245d0aa6e19e0ab12a024 is 4.480ms for 15 entries.
Jun 13 12:19:39 ####### systemd-journald[8468]: System Journal (/var/log/journal/e00bd6223a7245d0aa6e19e0ab12a024) is 32.0M, max 900.0M, 867.9M free.
Jun 13 12:19:49 ####### systemd-journald[8468]: Journal stopped
Jun 13 12:19:49 ####### systemd-journald[8648]: Journal started
Jun 13 12:19:49 ####### systemd-journald[8648]: Runtime Journal (/run/log/journal/e00bd6223a7245d0aa6e19e0ab12a024) is 4.6M, max 37.1M, 32.4M free.
Jun 13 12:19:49 ####### systemd-journald[8648]: Time spent on flushing to /var/log/journal/e00bd6223a7245d0aa6e19e0ab12a024 is 6.845ms for 54 entries.
Jun 13 12:19:49 ####### systemd-journald[8648]: System Journal (/var/log/journal/e00bd6223a7245d0aa6e19e0ab12a024) is 32.0M, max 900.0M, 867.9M free.
Jun 13 12:22:28 ####### systemd-journald[8648]: Journal stopped
-- Reboot --
Jan 10 06:56:39 ####### systemd-journald[2014]: Journal started
Jan 10 06:56:39 ####### systemd-journald[2014]: Runtime Journal (/run/log/journal/e00bd6223a7245d0aa6e19e0ab12a024) is 4.6M, max 37.1M, 32.4M free.
Jan 10 06:56:39 ####### systemd-journald[2014]: Runtime Journal (/run/log/journal/e00bd6223a7245d0aa6e19e0ab12a024) is 4.6M, max 37.1M, 32.4M free.
Jun 13 15:57:22 ####### systemd-journald[2014]: Journal stopped
Jun 13 15:57:22 ####### systemd-journald[6463]: Journal started
Jun 13 15:57:22 ####### systemd-journald[6463]: Runtime Journal (/run/log/journal/e00bd6223a7245d0aa6e19e0ab12a024) is 27.8M, max 37.1M, 9.2M free.
Jun 13 15:57:22 ####### systemd-journald[6463]: Time spent on flushing to /var/log/journal/e00bd6223a7245d0aa6e19e0ab12a024 is 2.085184s for 16277 entries.
Jun 13 15:57:22 ####### systemd-journald[6463]: System Journal (/var/log/journal/e00bd6223a7245d0aa6e19e0ab12a024) is 48.0M, max 900.0M, 851.9M free.
Jun 13 15:57:42 ####### systemd-journald[6463]: Journal stopped
Jun 13 15:57:42 ####### systemd-journald[6532]: Journal started
Jun 13 15:57:42 ####### systemd-journald[6532]: Runtime Journal (/run/log/journal/e00bd6223a7245d0aa6e19e0ab12a024) is 4.6M, max 37.1M, 32.4M free.
Jun 13 15:57:42 ####### systemd-journald[6532]: Time spent on flushing to /var/log/journal/e00bd6223a7245d0aa6e19e0ab12a024 is 5.834ms for 19 entries.
Jun 13 15:57:42 ####### systemd-journald[6532]: System Journal (/var/log/journal/e00bd6223a7245d0aa6e19e0ab12a024) is 48.0M, max 900.0M, 851.9M free.
Jun 13 16:20:10 ####### systemd-journald[6532]: Journal stopped
-- Reboot --
Jun 14 13:06:17 ####### systemd-journald[2020]: Journal stopped
Jun 14 13:06:17 ####### systemd-journald[333498]: Journal started
Jun 14 13:06:17 ####### systemd-journald[333498]: Runtime Journal (/run/log/journal/e00bd6223a7245d0aa6e19e0ab12a024) is 37.1M, max 37.1M, 0B free.
Jun 14 13:06:17 ####### systemd-journald[333498]: Time spent on flushing to /var/log/journal/e00bd6223a7245d0aa6e19e0ab12a024 is 3.509167s for 26919 entries.
Jun 14 13:06:17 ####### systemd-journald[333498]: System Journal (/var/log/journal/e00bd6223a7245d0aa6e19e0ab12a024) is 82.0M, max 900.0M, 817.9M free.
Jun 14 13:06:30 ####### systemd-journald[333498]: Journal stopped
Jun 14 13:06:30 ####### systemd-journald[333538]: Journal started
Jun 14 13:06:30 ####### systemd-journald[333538]: Runtime Journal (/run/log/journal/e00bd6223a7245d0aa6e19e0ab12a024) is 4.6M, max 37.1M, 32.4M free.
Jun 14 13:06:30 ####### systemd-journald[333538]: Time spent on flushing to /var/log/journal/e00bd6223a7245d0aa6e19e0ab12a024 is 4.822ms for 15 entries.
Jun 14 13:06:30 ####### systemd-journald[333538]: System Journal (/var/log/journal/e00bd6223a7245d0aa6e19e0ab12a024) is 82.0M, max 900.0M, 817.9M free.
Jun 14 14:22:28 ####### systemd-journald[333538]: Journal stopped
我们使用符号链接,以便日志可以存储在硬盘驱动器而不是 SSD 上以保存读/写:
pi@######:~$ ls -ltr /var/log
lrwxrwxrwx 1 root syslog 17 Jan 10 06:57 /var/log -> /mnt/usbdrive/log
machine-id 已填充并且在启动时不会更改:
pi@######:~$ cat /etc/machine-id
e00bd6223a7245d0aa6e19e0ab12a024
/etc/systemd/journal.conf 文件设置如下:
[Journal]
Storage=persistent
#Compress=yes
#Seal=yes
#SplitMode=uid
#SyncIntervalSec=5m
#RateLimitIntervalSec=30s
#RateLimitBurst=10000
SystemMaxUse=900M
#SystemKeepFree=
SystemMaxFileSize=50M
#SystemMaxFiles=100
#RuntimeMaxUse=
#RuntimeKeepFree=
#RuntimeMaxFileSize=
#RuntimeMaxFiles=100
#MaxRetentionSec=
#MaxFileSec=1month
ForwardToSyslog=no
#ForwardToKMsg=no
#ForwardToConsole=no
#ForwardToWall=yes
#TTYPath=/dev/console
#MaxLevelStore=debug
#MaxLevelSyslog=debug
#MaxLevelKMsg=notice
#MaxLevelConsole=info
#MaxLevelWall=emerg
#LineMax=48K
#ReadKMsg=yes
/var/log/journal权限:
pi@######:/var/log$ ls -ltr journal
total 4
drwxr-xr-x 2 root systemd-journal 4096 Jun 14 13:06 e00bd6223a7245d0aa6e19e0ab12a024
pi@######:~$ ls -ld /var/log/journal/
drwxr-xr-x 3 root systemd-journal 4096 Jun 13 12:16 /var/log/journal/
Docker 版本:
pi@######:~$ docker --version
Docker version 20.10.7, build 20.10.7-0ubuntu5~20.04.2
pi@######:~$ docker-compose --version
docker-compose version 1.25.0,
docker-compose.yml 中的 Docker 日志驱动程序
logging:
driver: journald
options:
tag: "########"
硬件平台是Odroid HC4(pi用户只是一个遗留的东西)操作系统:Ubuntu 20.04
内核版本:
pi@#######:~$ uname -r
4.9.277-83