Filebeat日志轮转实践
一 核心概念与适用场景
二 轮转Filebeat自身日志的两种方式
方式A 使用 logrotate(推荐)
/var/log/filebeat/*.log {
daily
missingok
rotate 7
compress
notifempty
create 0640 root root
sharedscripts
postrotate
# 优先使用 kill -HUP,若PID文件不存在则忽略
kill -HUP $(cat /var/run/filebeat/filebeat.pid 2>/dev/null) 2>/dev/null || true
endscript
}
systemctl restart filebeat(注意短暂中断)。sudo logrotate -f /etc/logrotate.d/filebeatls -l /var/log/filebeat/cat /etc/cron.daily/logrotate(logrotate 通常由每日 cron 自动运行)。方式B 使用 Filebeat 内置日志保留(仅影响 filebeat 自身日志)
logging:
to_files: true
files:
path: /var/log/filebeat
name: filebeat
keepfiles: 7
level: info
sudo systemctl restart filebeat。该方式不涉及信号或外部轮转工具,由 Filebeat 按数量保留旧日志。三 采集端对业务日志轮转的推荐配置
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
ignore_older: 168h
close_inactive: 5m
clean_inactive: 24h
output.elasticsearch:
hosts: ["localhost:9200"]
四 常见问题与排查
sudo logrotate -f /etc/logrotate.d/filebeattail -f /var/log/filebeat/filebeat 与 journalctl -u filebeat -fcat /etc/cron.daily/logrotate。