在CentOS系统中,Filebeat的日志轮转策略可以通过两种主要方式实现:使用系统级的 logrotate
工具,或者通过Filebeat自身的配置。以下是详细的设置步骤:
logrotate
工具进行日志轮转安装 logrotate
(如果尚未安装):
sudo yum install logrotate
创建或编辑 logrotate
配置文件:
sudo vi /etc/logrotate.d/filebeat
/etc/logrotate.d/syslog
,并添加Filebeat的配置。配置 logrotate
:
在打开的配置文件中,添加以下内容:
/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
: 创建新的日志文件,权限为640,属主为root,属组为adm。postrotate
和 endscript
:在轮转日志后执行的脚本。这里发送USR1信号给Filebeat进程,通知它重新打开日志文件。测试 logrotate
配置:
你可以手动测试 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
这个配置表示将Filebeat的日志输出到 /var/log/filebeat/filebeat.log
文件中,每天创建一个新的日志文件,每个文件最大为100MB,最多保留5个文件。
重启Filebeat服务: 使配置生效:
sudo systemctl restart filebeat
通过以上方法,你可以在CentOS系统中成功配置Filebeat的日志轮转。