解决方法(限制服务内存使用率)
1、修改rsyslogd服务配置文件
nano /usr/lib/systemd/system/rsyslog.service
在Service配置中添加MemoryAccounting=yes,MemoryMax=80M,MemoryHigh=8M三项如下所示。
[Unit] Description=System Logging Service ;Requires=syslog.socket Wants=network.target network-online.target After=network.target network-online.target Documentation=man:rsyslogd(8) Documentation=http://www.rsyslog.com/doc/ [Service] Type=notify EnvironmentFile=-/etc/sysconfig/rsyslog ExecStart=/usr/sbin/rsyslogd -n $SYSLOGD_OPTIONS Restart=on-failure UMask=0066 StandardOutput=null Restart=on-failure MemoryAccounting=yes MemoryMax=80M MemoryHigh=8M [Install] WantedBy=multi-user.target ;Alias=syslog.service
通常情况下rsyslogd大小只有5M,所以将内存上限设置为8M,然后将绝对内存限制为80M。
2、重启服务
systemctl daemon-reload systemctl restart rsyslog
根本原因
查看rsyslog输出的日志/var/log/
路径 | 描述 |
---|---|
/var/log/messages | 服务信息日志(记录linux操作系统常见的服务信息和错误信息) |
/var/log/secure | 系统的登陆日志(记录用户和工作组的变化情况,是系统安全日志,用户的认证登陆情况 |
/var/log/maillog | 邮件日志 |
/var/log/cron | 定时任务 |
/var/log/boot.log | 系统启动日志 |
发现/var/log/messages有几个G的日志。查看日志内容发现rsyslog把Journal的log都进行的输出和汇总。
当容器越多是,log也就会也多,内存占用也就越多。
同时也可能导致systemd-journald内存占用过高
1、修改Journal的配置/etc/systemd/journald.conf
把Storage改为none,如下
# This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # # Entries in this file show the compile time defaults. # You can change settings by editing this file. # Defaults can be restored by simply deleting this file. # # See journald.conf(5) for details. [Journal] Storage=none #Compress=yes #Seal=yes #SplitMode=uid #SyncIntervalSec=5m #RateLimitInterval=30s #RateLimitBurst=1000 SystemMaxUse=16M #SystemKeepFree= #SystemMaxFileSize= #RuntimeMaxUse= #RuntimeKeepFree= #RuntimeMaxFileSize= #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
2、重启生效
systemctl restart systemd-journald
3、Storage选项扩展
通过查看man手册,#man 5 journald.conf 你会发现,Storage=的值可以是volatile,persistent, autoandnone,但是,默认的是auto,
- volatile代表日志只存在内存中,即/run/log/journal/
- persistent代表日志只存在磁盘中,即/var/log/journal/
- auto代表日志存在磁盘中,或者内存中,这个取决于你是否创建/var/log/journal/目录!!这个也算是一个坑吧,看来大家都需要手动mkdir -p /var/log/journal/,systemctl restart systemd-journald来解放自己的内存了!!!
- none,表示,日志不保留,全部drop,只有当你决定不使用systemd-journald的时候,你可以使用!