在CentOS系统上,Filebeat的日志轮转可以通过配置logrotate工具来实现。以下是设置Filebeat日志轮转的步骤:
安装logrotate(如果尚未安装):
sudo yum install logrotate
创建或编辑logrotate配置文件:
Filebeat的日志通常位于/var/log/filebeat/filebeat
。你可以为这个文件创建一个单独的logrotate配置文件,或者将其包含在一个通用的配置文件中。
创建一个新的配置文件:
sudo vi /etc/logrotate.d/filebeat
或者编辑现有的配置文件,例如/etc/logrotate.d/syslog
,并添加Filebeat的配置。
配置logrotate: 在打开的配置文件中,添加以下内容:
/var/log/filebeat/filebeat {
daily
rotate 7
compress
missingok
notifempty
create 640 root adm
postrotate
if [ -f /var/run/filebeat/filebeat.pid ]; then
kill -USR1 `cat /var/run/filebeat/filebeat.pid`
fi
endscript
}
解释:
daily
: 每天轮转日志。rotate 7
: 保留7个轮转的日志文件。compress
: 压缩旧的日志文件。missingok
: 如果日志文件不存在,不会报错。notifempty
: 如果日志文件为空,不进行轮转。create 640 root adm
: 创建新的日志文件,权限为640,属主为root,属组为adm。postrotate
和 endscript
: 在轮转日志后执行的脚本。这里发送USR1信号给Filebeat进程,通知它重新打开日志文件。测试logrotate配置: 你可以手动测试logrotate配置是否正确:
sudo logrotate -f /etc/logrotate.d/filebeat
这将强制执行一次日志轮转,并显示处理的结果。
确保logrotate定时任务运行:
logrotate通常由cron的daily任务自动运行。你可以检查/etc/cron.daily/logrotate
文件,确保它存在并且没有被禁用。
通过以上步骤,你就可以在CentOS系统上成功设置Filebeat的日志轮转。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>