在CentOS上设置Filebeat日志轮转,可以通过配置systemd的定时器来实现。以下是详细的步骤:
首先,确保你已经安装了Filebeat。如果还没有安装,可以使用以下命令进行安装:
sudo yum install filebeat
编辑Filebeat的配置文件 /etc/filebeat/filebeat.yml,确保日志输出路径正确。例如:
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
output.elasticsearch:
hosts: ["localhost:9200"]
CentOS使用 logrotate 工具来管理日志轮转。你需要为Filebeat创建一个 logrotate 配置文件。
创建一个新的配置文件 /etc/logrotate.d/filebeat:
sudo vi /etc/logrotate.d/filebeat
在文件中添加以下内容:
/var/log/filebeat/*.log {
daily
missingok
rotate 7
compress
notifempty
create 0640 root root
}
解释:
daily: 每天轮转一次日志。missingok: 如果日志文件不存在,不会报错。rotate 7: 保留7个轮转日志文件。compress: 压缩旧的日志文件。notifempty: 如果日志文件为空,不进行轮转。create 0640 root root: 创建新的日志文件,权限为0640,属主和属组为root。为了确保 logrotate 定时器能够正确触发,你需要配置一个systemd定时器。
创建一个新的systemd服务文件 /etc/systemd/system/filebeat-logrotate.service:
sudo vi /etc/systemd/system/filebeat-logrotate.service
在文件中添加以下内容:
[Unit]
Description=Rotate Filebeat logs
[Service]
Type=oneshot
ExecStart=/usr/sbin/logrotate /etc/logrotate.d/filebeat
创建一个新的systemd定时器文件 /etc/systemd/system/filebeat-logrotate.timer:
sudo vi /etc/systemd/system/filebeat-logrotate.timer
在文件中添加以下内容:
[Unit]
Description=Run Filebeat logrotate daily
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
启动并启用定时器,使其每天自动运行:
sudo systemctl daemon-reload
sudo systemctl start filebeat-logrotate.timer
sudo systemctl enable filebeat-logrotate.timer
你可以通过以下命令查看定时器的状态:
sudo systemctl list-timers --all | grep filebeat-logrotate
如果一切配置正确,你应该会看到定时器处于 active 状态。
通过以上步骤,你就可以在CentOS上成功设置Filebeat日志轮转。