在Linux上清理Kafka日志可通过以下方式操作:
配置Kafka参数
修改server.properties
文件(路径:$KAFKA_HOME/config/
),设置日志保留策略:
log.retention.hours
:按小时保留日志(如设为168保留7天)。log.cleanup.policy
:设为delete
(默认)或compact
(压缩保留)。log.segment.bytes
:控制单个日志分段大小(默认1GB)。使用Linux工具定时清理
/etc/logrotate.d/kafka
,添加配置:/path/to/kafka/logs/*.log {
daily
rotate 7
compress
missingok
notifempty
}
通过logrotate
自动轮转、压缩日志。#!/bin/sh
find /path/to/kafka/logs/ -mtime +30 -name "*.log" -exec rm -rf {} \;
执行crontab -e
,添加0 0 * * * /path/to/script.sh
(每天凌晨执行)。Kafka自带工具(需手动执行)
使用kafka-log-dirs.sh
脚本清理日志(需指定--bootstrap-server
和--delete
参数),但需注意该工具主要用于清理数据日志,非系统日志。
注意:修改配置后需重启Kafka服务生效,且清理前建议先备份重要日志。