Kafka故障恢复操作指南
在进行故障恢复前,需先通过以下步骤定位问题根源,避免盲目操作:
systemctl status kafka
(Debian/CentOS)查看Kafka服务是否运行。若未运行,尝试启动服务(systemctl start kafka
),并观察启动日志确认是否成功。/var/log/kafka/
(默认路径)或config/server.properties
中log.dirs
指定的目录。通过tail -f server.log
实时查看最新日志,定位错误信息(如InconsistentClusterIdException
、Port already in use
、Disk full
等)。systemctl status zookeeper
)。若Zookeeper未启动,启动后重启Kafka服务。ping <broker_ip>
测试节点间连通性,用netstat -tuln | grep 9092
(默认端口)检查Kafka端口是否被占用。确保防火墙允许Kafka端口(ufw allow 9092/tcp
)。config/server.properties
中的关键配置:broker.id
(唯一标识)、listeners
(监听地址,如PLAINTEXT://0.0.0.0:9092
)、advertised.listeners
(客户端连接的地址,如PLAINTEXT://broker1:9092
)、zookeeper.connect
(Zookeeper地址,如broker1:2181,broker2:2181
)、log.dirs
(日志目录,需有足够磁盘空间)。UnderReplicatedPartitions
告警)。server.properties
中移除故障Broker的broker.id
);kafka-reassign-partitions.sh
生成副本迁移计划(将故障Broker上的所有副本迁移到其他健康Broker);--execute
),并监控UnderReplicatedPartitions
指标(降至0表示迁移完成);systemctl stop kafka
),从配置中彻底移除该节点,并更新所有客户端的bootstrap.servers
(移除故障Broker地址)。kafka-configs.sh --bootstrap-server <healthy_broker> --entity-type topics --entity-name <topic> --alter --add-config 'unclean.leader.election.enable=true'
);kafka-leader-election.sh
手动触发新Leader选举;--alter --delete-config 'unclean.leader.election.enable'
),避免后续数据不一致;kafka-topics.sh --describe
显示Leader: -1
),导致生产/消费失败。kafka-topics.sh --describe --topic <topic> --bootstrap-server <broker>
),确认ISR是否为空;replica.lag.time.max.ms
内副本追上Leader);kafka-leader-election.sh --bootstrap-server <broker> --topic <topic> --partition <partition> --election-type preferred
)。Corrupt index file
、Invalid record size
或Record is corrupt
。systemctl stop kafka
);log.dirs
下受损的分区目录(如/data/kafka-logs/topic-partition
);kafka-dump-log.sh
检查Segment文件(--files <segment.log> --print-data-log --verify-index-only
),尝试删除损坏的.index
、.timeindex
文件(Broker重启后会自动重建);.log
文件损坏,使用kafka-dump-log.sh
定位损坏点(如--max-message-size
),删除损坏的Segment及后续文件(rm <segmentBase>*.log *.index *.timeindex
);kafka-topics.sh --describe
)。InconsistentClusterIdException
(集群ID不匹配)或Metadata corruption
。rm -rf /data/zookeeper/data/version-2/*
,需备份);__cluster_metadata
Topic(kafka-dump-log.sh --files /data/kafka-logs/__cluster_metadata-0/00000000000000000000.log > kraft-metadata-backup.log
);__cluster_metadata
Topic的日志文件;--whitelist '.*'
同步所有Topic),故障时切换bootstrap.servers
指向灾备集群,确保业务连续性。rsync
或NFS每日备份log.dirs
中的分区数据到S3/NFS(保留7天);zkCli.sh get /kafka/config/topics
)或KRaft元数据(kafka-dump-log.sh
);--consumer.config backup.properties --producer.config target.properties
)。UnderReplicatedPartitions
、ISR Shrinks
、Disk Space
),设置告警阈值(如UnderReplicatedPartitions > 0
时触发短信告警),及时发现潜在问题。