rsyslogd内存占用很高解决方案

  1. 1. 概述
  2. 2. 限制服务内存使用率
  3. 3. 根本原因

1. 概述

CentOS系统中,内存占用一度超过99%, 调查发现rsyslogd内存占用率超过了30%,达到了2.5G。

2. 限制服务内存使用率

初步推测rsyslogd有内存泄漏(或者无限读写文件)。通过限制服务内存使用率的方式,使服务正常运行。

systemd.resource-control(金步国翻译)

名称 描述
MemoryAccounting 若设为”yes”则表示为此单元开启内存占用统计。 注意,这同时也隐含的开启了该单元 所属的 slice 以及父 slice 内所有单元的内存占用统计。 此选项的默认值由 DefaultMemoryAccounting= 决定
MemoryHigh 尽可能限制该单元中的进程最多可以使用多少内存。 虽然这是一个允许被突破的柔性限制,但是突破限制后,进程的运行速度将会大打折扣, 并且系统将会尽可能尽快回收超出的内存。此选项的目的在于柔性限制内存使用量。选项值可以是以字节为单位的绝对内存大小(可以使用以1024为基数的 K, M, G, T 后缀), 也可以是以百分比表示的相对内存大小(相对于系统的全部物理内存), 还可以设为特殊值 “infinity” 表示不作限制。
MemoryMax 绝对刚性的限制该单元中的进程最多可以使用多少内存。 这是一个不允许突破的刚性限制,触碰此限制会导致进程由于内存不足而被强制杀死。 建议将 MemoryHigh= 用作主要的内存限制手段, 而将 MemoryMax= 用作不可突破的底线。选项值可以是以字节为单位的绝对大小(可以使用以1024为基数的 K, M, G, T 后缀), 也可以是以百分比表示的相对大小(相对于系统的全部物理内存), 还可以设为特殊值 “infinity” 表示不作限制。

修改rsyslogd服务配置文件/usr/lib/systemd/system/rsyslog.service

[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。

重启服务

systemctl daemon-reload
systemctl restart rsyslog

3. 根本原因

查看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也就会也多,内存占用也就越多。

消减输出的日志,将log级别定义为err级别

修改rsyslogd配置文件/etc/rsyslog.conf

*.err;mail.none;authpriv.none;cron.none /var/log/messages

重启服务

systemctl daemon-reload
systemctl restart rsyslog

修改Journal的配置/etc/systemd/journald.conf

#  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=auto
#Compress=yes
#Seal=yes
#SplitMode=uid
#SyncIntervalSec=5m
#RateLimitInterval=30s
#RateLimitBurst=1000
SystemMaxUse=8G
#SystemKeepFree=
SystemMaxFileSize=10M
#RuntimeMaxUse=
#RuntimeKeepFree=
#RuntimeMaxFileSize=
MaxRetentionSec=1month
#MaxFileSec=1month
ForwardToSyslog=no
#ForwardToKMsg=no
#ForwardToConsole=no
ForwardToWall=no
#TTYPath=/dev/console
#MaxLevelStore=debug
#MaxLevelSyslog=debug
#MaxLevelKMsg=notice
#MaxLevelConsole=info
#MaxLevelWall=emerg
#LineMax=48K

重启服务

systemctl restart systemd-journald

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 wind.kaisa@gmail.com

💰

×

Help us with donation