在CentOS系统上配置Filebeat的日志轮转可以通过以下两种主要方式实现:使用系统的 logrotate
工具或通过Filebeat自身的配置。以下是详细的配置方法:
安装logrotate(如果尚未安装):
sudo yum install logrotate
创建或编辑logrotate配置文件:
/var/log/filebeat/filebeat.log
。sudo vi /etc/logrotate.d/filebeat
/etc/logrotate.d/syslog
,并添加Filebeat的配置:/var/log/filebeat/filebeat {
daily rotate 7
compress
delaycompress
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
: 压缩旧的日志文件。delaycompress
: 延迟压缩,直到下一次轮转。missingok
: 如果日志文件丢失,不会报错。notifempty
: 如果日志文件为空,不进行轮转。create 640 root adm
: 创建新的日志文件时设置权限和所有者。postrotate
和 endscript
: 在轮转日志后执行的脚本。这里发送USR1信号给Filebeat进程,通知它重新打开日志文件。测试logrotate配置:
sudo logrotate -f /etc/logrotate.d/filebeat
这将强制执行一次日志轮转,并显示处理的结果。
确保logrotate定时任务运行:
/etc/cron.daily/logrotate
文件,确保它存在并且没有被禁用。编辑Filebeat配置文件:
/etc/filebeat/filebeat.yml
:sudo vi /etc/filebeat/filebeat.yml
添加日志轮转配置:
filebeat.inputs
部分添加以下内容:filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/filebeat/*.log
fields:
log_type: filebeat
json.keys_under_root: true
json.add_error_key: true
ignore_older: 7d
ignore_older: 7d
配置表示忽略7天以前的日志文件。重启Filebeat服务:
sudo systemctl restart filebeat
通过以上两种方法,你可以在CentOS系统上成功配置Filebeat的日志轮转策略。