工具 & 效率17 分钟阅读
Apache Kafka 运维命令 100 条
本文按 Apache Kafka 4.3 的 KRaft 架构整理 100 条命令。Kafka 官方脚本位于发行包 bin/ 目录,示例集群地址为 localhost:9092,客户端配置为 config/client.properties。
2026年8月9日阅读—点赞—收藏—
dba100apache墨力计划kafka
100 条命令系列文章专栏
本文按 Apache Kafka 4.3 的 KRaft 架构整理 100 条命令。Kafka 官方脚本位于发行包 bin/ 目录,示例集群地址为 localhost:9092,客户端配置为 config/client.properties。
本文按 Apache Kafka 4.3 的 KRaft 架构整理 100 条命令。Kafka 官方脚本位于发行包 bin/ 目录,示例集群地址为 localhost:9092,客户端配置为 config/client.properties。
删除 Topic、修改分区、副本重分配、Leader 选举和消费者组位点会影响数据或服务,执行前应备份配置并确认目标范围。
1bin/kafka-broker-api-versions.sh --bootstrap-server localhost:90921KAFKA_CLUSTER_ID=$(bin/kafka-storage.sh random-uuid)1bin/kafka-storage.sh format -t "$KAFKA_CLUSTER_ID" -c config/server.properties1bin/kafka-storage.sh info -c config/server.properties1bin/kafka-server-start.sh config/server.properties1bin/kafka-server-start.sh -daemon config/server.properties1bin/kafka-server-stop.sh1systemctl status kafka --no-pager1journalctl -u kafka -n 200 --no-pager1ss -lntp '( sport = :9092 )'1bin/kafka-topics.sh --bootstrap-server localhost:9092 --list1bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic orders --partitions 6 --replication-factor 31bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic audit --partitions 6 --replication-factor 3 --config retention.ms=6048000001bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic orders1bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe1bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --under-replicated-partitions1bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --unavailable-partitions1bin/kafka-topics.sh --bootstrap-server localhost:9092 --alter --topic orders --partitions 121bin/kafka-topics.sh --bootstrap-server localhost:9092 --list --topic 'orders.*'1bin/kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic old-orders1bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic orders1bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic orders --property parse.key=true --property key.separator=:1bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic orders < messages.txt1bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic orders1bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic orders --from-beginning1bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic orders --group order-debug1bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic orders --from-beginning --max-messages 101bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic orders --property print.key=true --property key.separator=':'1bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic orders --property print.timestamp=true --property print.partition=true1bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic orders --partition 0 --offset 100 --max-messages 101bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list1bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group order-service1bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group order-service --members1bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group order-service --state1bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --delete --group old-group1bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group order-service --topic orders --reset-offsets --to-earliest1bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group order-service --topic orders --reset-offsets --to-earliest --execute1bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group order-service --topic orders --reset-offsets --to-latest --execute1bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group order-service --topic orders --reset-offsets --to-datetime 2026-07-27T00:00:00.000 --execute1bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group order-service --topic orders --reset-offsets --shift-by -100 --export1bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type topics --entity-name orders --describe1bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type topics --entity-name orders --alter --add-config retention.ms=2592000001bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type topics --entity-name orders --alter --delete-config retention.ms1bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type brokers --entity-name 1 --describe --all1bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type brokers --entity-name 1 --alter --add-config log.cleaner.threads=21bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type clients --describe1bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type clients --entity-name order-api --alter --add-config producer_byte_rate=104857601bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type users --entity-name appuser --alter --add-config consumer_byte_rate=104857601bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type users --entity-name appuser --describe1bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type users --entity-name appuser --alter --add-config 'SCRAM-SHA-512=[iterations=8192,password=REPLACE_ME]'1bin/kafka-get-offsets.sh --bootstrap-server localhost:9092 --topic orders --time latest1bin/kafka-get-offsets.sh --bootstrap-server localhost:9092 --topic orders --time earliest1bin/kafka-get-offsets.sh --bootstrap-server localhost:9092 --topic orders --time 17851104000001bin/kafka-dump-log.sh --files /data/kafka/orders-0/00000000000000000000.log --print-data-log1bin/kafka-dump-log.sh --files /data/kafka/orders-0/00000000000000000000.index1bin/kafka-dump-log.sh --files /data/kafka/orders-0/00000000000000000000.snapshot1bin/kafka-storage.sh info -c config/server.properties1bin/kafka-log-dirs.sh --bootstrap-server localhost:9092 --describe1bin/kafka-log-dirs.sh --bootstrap-server localhost:9092 --describe --broker-list 11bin/kafka-delete-records.sh --bootstrap-server localhost:9092 --offset-json-file delete-records.json1bin/kafka-reassign-partitions.sh --bootstrap-server localhost:9092 --topics-to-move-json-file topics.json --broker-list '1,2,3,4' --generate1bin/kafka-reassign-partitions.sh --bootstrap-server localhost:9092 --reassignment-json-file reassignment.json --execute1bin/kafka-reassign-partitions.sh --bootstrap-server localhost:9092 --reassignment-json-file reassignment.json --verify1bin/kafka-reassign-partitions.sh --bootstrap-server localhost:9092 --reassignment-json-file reassignment.json --execute --throttle 500000001bin/kafka-reassign-partitions.sh --bootstrap-server localhost:9092 --reassignment-json-file reassignment.json --cancel1bin/kafka-leader-election.sh --bootstrap-server localhost:9092 --election-type preferred --all-topic-partitions1bin/kafka-leader-election.sh --bootstrap-server localhost:9092 --election-type preferred --topic orders --partition 01bin/kafka-leader-election.sh --bootstrap-server localhost:9092 --election-type unclean --topic orders --partition 0可能造成数据丢失,仅用于明确接受风险的故障恢复。
1bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic orders1bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --under-min-isr-partitions1bin/kafka-metadata-quorum.sh --bootstrap-controller localhost:9093 describe --status1bin/kafka-metadata-quorum.sh --bootstrap-controller localhost:9093 describe --replication1bin/kafka-metadata-shell.sh --help1bin/kafka-dump-log.sh --cluster-metadata-decoder --files /data/kafka/__cluster_metadata-0/00000000000000000000.log1bin/kafka-acls.sh --bootstrap-server localhost:9092 --list1bin/kafka-acls.sh --bootstrap-server localhost:9092 --add --allow-principal User:appuser --operation Read --topic orders1bin/kafka-acls.sh --bootstrap-server localhost:9092 --add --allow-principal User:appuser --operation Read --group order-service1bin/kafka-acls.sh --bootstrap-server localhost:9092 --add --allow-principal User:appuser --producer --topic orders1bin/kafka-acls.sh --bootstrap-server localhost:9092 --add --allow-principal User:appuser --consumer --topic orders --group order-service1bin/kafka-acls.sh --bootstrap-server localhost:9092 --remove --allow-principal User:appuser --operation Read --topic orders1bin/kafka-producer-perf-test.sh --topic perf-test --num-records 1000000 --record-size 1024 --throughput -1 --producer-props bootstrap.servers=localhost:90921bin/kafka-producer-perf-test.sh --topic perf-test --num-records 100000 --record-size 1024 --throughput 10000 --producer-props bootstrap.servers=localhost:9092 acks=all1bin/kafka-consumer-perf-test.sh --bootstrap-server localhost:9092 --topic perf-test --messages 1000001bin/kafka-transactions.sh --bootstrap-server localhost:9092 list1bin/kafka-transactions.sh --bootstrap-server localhost:9092 describe --transactional-id order-tx1bin/kafka-share-groups.sh --bootstrap-server localhost:9092 --list1bin/kafka-share-groups.sh --bootstrap-server localhost:9092 --describe --group share-group1jcmd | grep -i kafka1jcmd KAFKA_PID GC.heap_info1jcmd KAFKA_PID Thread.print > kafka-threads.txt1bin/connect-standalone.sh config/connect-standalone.properties config/file-source.properties config/file-sink.properties1bin/connect-distributed.sh config/connect-distributed.properties1curl -s http://localhost:8083/connectors | jq1curl -s http://localhost:8083/connectors/my-source/status | jq1curl -fsS -X PUT http://localhost:8083/connectors/my-source/pause1curl -fsS -X PUT http://localhost:8083/connectors/my-source/resume1curl -fsS -X POST 'http://localhost:8083/connectors/my-source/restart?includeTasks=true&onlyFailed=true'1ss -lntp '( sport = :9999 )'1grep -Ei 'UnderReplicated|OfflinePartition|ERROR|FATAL' logs/server.log | tail -1001{ bin/kafka-broker-api-versions.sh --bootstrap-server localhost:9092; bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --under-replicated-partitions; bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list; } > kafka-check.txtKafka 运维应持续关注 ISR、副本不足、磁盘容量、消费者积压、Controller 仲裁和客户端错误。KRaft 环境不要混用旧 ZooKeeper 参数,所有重分配、位点重置和非干净选主都应先预览并具备回退方案。