centos

CentOS中Filebeat日志轮转设置

小樊
47
2025-04-12 17:00:04
栏目: 智能运维

在CentOS系统中,Filebeat的日志轮转可以通过配置systemd服务来实现。以下是详细的步骤:

1. 编辑Filebeat systemd服务文件

首先,找到并编辑Filebeat的systemd服务文件。通常位于 /etc/systemd/system/filebeat.service

sudo vi /etc/systemd/system/filebeat.service

2. 添加日志轮转配置

[Service] 部分添加以下内容来配置日志轮转:

[Service]
...
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=filebeat
Restart=on-failure
User=filebeat
Group=filebeat
Environment="FILEBEAT_CONFIG=/etc/filebeat/filebeat.yml"
ExecStart=/usr/share/filebeat/filebeat -e -c /etc/filebeat/filebeat.yml
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
PrivateTmp=true

3. 创建日志轮转配置文件

/etc/logrotate.d/ 目录下创建一个新的日志轮转配置文件,例如 filebeat

sudo vi /etc/logrotate.d/filebeat

添加以下内容:

/var/log/filebeat/*.log {
    daily
    missingok
    rotate 7
    compress
    notifempty
    create 0640 root root
}

解释:

4. 重新加载systemd配置并重启Filebeat服务

保存并退出编辑器后,重新加载systemd配置并重启Filebeat服务以应用更改。

sudo systemctl daemon-reload
sudo systemctl restart filebeat

5. 验证日志轮转

可以通过查看日志文件来验证日志轮转是否正常工作。

ls -l /var/log/filebeat/

你应该能看到按日期命名的日志文件,并且旧的日志文件已经被压缩。

6. 监控日志轮转

可以使用 journalctl 命令来监控Filebeat的日志输出。

sudo journalctl -u filebeat -f

这样可以实时查看Filebeat的日志,并确认日志轮转是否按预期进行。

通过以上步骤,你可以在CentOS系统中成功配置Filebeat的日志轮转。

0
看了该问题的人还看了