在CentOS上,Filebeat的日志轮转策略可以通过两种主要方式实现:使用系统的logrotate
工具或通过Filebeat自身的配置。以下是详细的配置方法:
安装logrotate(如果尚未安装):
sudo yum install logrotate
创建或编辑logrotate配置文件:
为Filebeat的日志文件创建一个新的logrotate配置文件,或者将其包含在一个通用的配置文件中。通常,Filebeat的日志文件位于 /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定时任务运行:
logrotate通常由cron的daily任务自动运行。你可以检查 /etc/cron.daily/logrotate
文件,确保它存在并且没有被禁用。
编辑Filebeat配置文件:
打开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的日志轮转策略。