centos

centos filebeat日志轮转策略

小樊
50
2025-04-18 18:08:49
栏目: 智能运维

在CentOS系统上配置Filebeat的日志轮转可以通过以下几种方法实现:

使用logrotate工具

logrotate是Linux系统上常用的日志轮转工具,可以通过配置它来管理Filebeat的日志文件。以下是配置步骤:

  1. 安装logrotate(如果尚未安装):

    sudo yum install logrotate
    
  2. 创建或编辑logrotate配置文件: 为Filebeat的日志文件创建一个新的配置文件,例如 /etc/logrotate.d/filebeat,或者将其包含在一个通用的配置文件中。

    sudo vi /etc/logrotate.d/filebeat
    
  3. 配置logrotate: 在打开的配置文件中,添加以下内容:

    /var/log/filebeat/filebeat {
        daily rotate 7
        compress
        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:压缩旧的日志文件。
    • missingok:如果日志文件不存在,不会报错。
    • notifempty:如果日志文件为空,不进行轮转。
    • create 640 root adm:创建新的日志文件,权限为640,属主为root,属组为adm。
    • postrotateendscript:在轮转日志后执行的脚本,这里发送USR1信号给Filebeat进程,通知它重新打开日志文件。
  4. 测试logrotate配置

    sudo logrotate -f /etc/logrotate.d/filebeat
    
  5. 确保logrotate定时任务运行: logrotate通常由cron的daily任务自动运行。你可以检查 /etc/cron.daily/logrotate 文件,确保它存在并且没有被禁用。

使用Filebeat的配置文件

Filebeat自身也支持日志轮转配置,可以通过编辑Filebeat的配置文件来实现。以下是配置步骤:

  1. 编辑Filebeat配置文件: 打开Filebeat的配置文件 /etc/filebeat/filebeat.yml

    sudo vi /etc/filebeat/filebeat.yml
    
  2. 添加日志轮转配置: 在 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
    
  3. 重启Filebeat服务: 使配置生效:

    sudo systemctl restart filebeat
    

注意事项

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

0
看了该问题的人还看了