rsyslog内存占用高
起因
k8s集群节点,内存分布不均。查看详细k8s服务负载发现并不高。到具体节点机查看。发现rsyslog服务占用32%内存。
查询内存占用前10进程
ps aux | grep -v PID | sort -rn -k +4 | head
处理方法一
直接重启rsyslog服务 释放内存
systemctl restart rsyslog
优选方法二
查询了资料,rsyslog旧版本会存在内存泄露问题。故对系统rsyslog服务做升级操作。再重启
yum install rsyslog -y
systemctl restart rsyslog
处理方法三
还有一种更稳妥的方案,直接通过systemd限制rsyslog内存使用。
修改rsyslogd服务配置文件
vim /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
重启服务
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=
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的时候,你可以使用!
参考文章
https://www.kaisawind.com/2019/09/24/2019-09-25-linux2/
https://www.jianshu.com/p/f649e65382f6
方法三出处:https://www.cnblogs.com/taosiyu/p/12987808.html
扩展
须要看看为什么journal那么多日志。
# 查看全部
journalctl -l
# 查看实时
journalctl -f
发现一直在输出docker错误日志。
systemd: Created slice libcontainer_9042_systemd_test_default.slice.
问题大致原因是由于将 cgroup-driver 设置为 systemd 后引起的,但是这些错误信息不会影响容器指标。
处理方法:
https://www.cnblogs.com/caidingyu/p/12160105.html
彻底解决。 升级docker版本和containerd版本。原因是旧版本dokcer和systemd兼容性有问题。
docker-ce-19.03.5
docker-ce-cli-19.03.5
containerd.io-1.2.10
systemd: (rpm -qa | grep systemd, 版本<219-67.el7需要升级)