在Linux上配置Filebeat的日志轮转,可以通过以下步骤实现:
创建或编辑Filebeat的systemd服务文件:
打开或创建/etc/systemd/system/filebeat.service文件,并确保它包含以下内容:
[Unit]
Description=Filebeat log sharding and shipping
After=syslog.target network.target
[Service]
Type=simple
ExecStart=/usr/share/filebeat/filebeat -e -c /etc/filebeat/filebeat.yml
Restart=on-failure
User=filebeat
Group=filebeat
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=filebeat
[Install]
WantedBy=multi-user.target
创建日志轮转配置文件:
在/etc/logrotate.d/目录下创建一个新的文件,例如filebeat:
sudo nano /etc/logrotate.d/filebeat
添加以下内容:
/var/log/filebeat/*.log {
daily
missingok
rotate 7
compress
notifempty
create 0640 root adm
}
解释:
daily: 每天轮转日志。missingok: 如果日志文件丢失,不会报错。rotate 7: 保留7天的日志文件。compress: 压缩旧的日志文件。notifempty: 如果日志文件为空,则不轮转。create 0640 root adm: 创建新的日志文件,权限为0640,属主为root,属组为adm。重新加载systemd配置并重启Filebeat:
sudo systemctl daemon-reload
sudo systemctl restart filebeat
创建日志轮转配置文件:
在/etc/logrotate.d/目录下创建一个新的文件,例如filebeat:
sudo nano /etc/logrotate.d/filebeat
添加以下内容:
/var/log/filebeat/*.log {
daily
missingok
rotate 7
compress
notifempty
create 0640 root adm
}
手动触发日志轮转:
sudo logrotate -f /etc/logrotate.d/filebeat
这将强制立即应用日志轮转配置。
确保Filebeat正在运行并且日志文件正在被正确轮转:
sudo systemctl status filebeat
ls -l /var/log/filebeat/
你应该能看到按日期命名的日志文件,并且旧的日志文件已经被压缩。
通过以上步骤,你可以在Linux上成功配置Filebeat的日志轮转。