Kafka日常运维命令总结

一、集群管理
  1. 前台启动Broker

    bash 复制代码
    bin/kafka-server-start.sh <path>/server.properties
    • 关闭方式:Ctrl + C
  2. 后台启动Broker

    bash 复制代码
    bin/kafka-server-start.sh -daemon <path>/server.properties
  3. 关闭Broker

    bash 复制代码
    bin/kafka-server-stop.sh

二、Topic管理
操作 命令
创建Topic bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --partitions 3 --replication-factor 3 --topic <topicname>
删除Topic bin/kafka-topics.sh --delete --bootstrap-server localhost:9092 --topic <topicname>
查询Topic列表 bin/kafka-topics.sh --bootstrap-server localhost:9092 --list
查询Topic详情 bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic <topicname>
增加分区数 bin/kafka-topics.sh --alter --bootstrap-server localhost:9092 --partitions 6 --topic <topicname>

注意 :新版本推荐使用--bootstrap-server替代--zookeeper


三、Consumer Groups管理
操作 命令
查询消费组列表 bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
查询消费组详情 bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group <groupname>
删除消费组 bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --delete --group <groupname>
  1. 重设消费位移

    bash 复制代码
    # 重置到最早位移
    bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group <groupname> --reset-offsets --to-earliest --execute
    
    # 重置到最新位移
    bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group <groupname> --reset-offsets --to-latest --execute
    
    # 重置到指定位移
    bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group <groupname> --reset-offsets --to-offset 2000 --execute

四、常用运维工具
  1. 生产者控制台

    bash 复制代码
    bin/kafka-console-producer.sh --broker-list localhost:9092 --topic <topicname>
    • 高级参数:
      --compression-codec lz4(压缩)
      --request-required-acks all(ACK机制)
      --message-send-max-retries 10(重试次数)
  2. 消费者控制台

    bash 复制代码
    bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic <topicname> --group <groupname> --from-beginning
  3. 性能测试工具

    bash 复制代码
    # 生产者性能测试
    bin/kafka-producer-perf-test.sh --topic <topic> --num-records 1000000 --record-size 1000
    
    # 消费者性能测试
    bin/kafka-consumer-perf-test.sh --topic <topic> --messages 1000000
  4. 获取Topic消息数

    bash 复制代码
    # 最新位移(总消息数)
    bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic <topic> --time -1
    
    # 最早位移
    bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic <topic> --time -2

五、分区管理
  1. Preferred Leader选举

    bash 复制代码
    bin/kafka-leader-election.sh --bootstrap-server localhost:9092 --election-type preferred --topic <topic> --partition 0
  2. 分区重分配

    bash 复制代码
    # 生成迁移计划
    bin/kafka-reassign-partitions.sh --bootstrap-server localhost:9092 --generate --topics-to-move-json-file plan.json
    
    # 执行迁移
    bin/kafka-reassign-partitions.sh --bootstrap-server localhost:9092 --execute --reassignment-json-file reassign.json
    
    # 验证进度
    bin/kafka-reassign-partitions.sh --bootstrap-server localhost:9092 --verify --reassignment-json-file reassign.json

六、高级运维
  1. 查看日志段元数据

    bash 复制代码
    bin/kafka-dump-log.sh --files 000000000000.log --print-data-log
  2. 跨集群复制(MirrorMaker)

    bash 复制代码
    bin/kafka-mirror-maker.sh --consumer.config consumer.conf --producer.config producer.conf --whitelist ".*"
  3. 查看__consumer_offsets

    bash 复制代码
    bin/kafka-console-consumer.sh --topic __consumer_offsets --formatter "kafka.coordinator.group.GroupMetadataManager\$OffsetsMessageFormatter"

关键注意事项:

  1. 版本兼容性

    • ≥ Kafka 2.2.x 推荐使用 --bootstrap-server 替代 --zookeeper
    • 删除Topic需确保 delete.topic.enable=true
  2. 生产环境建议

    • 分区重分配避免高峰时段操作
    • 重设位移前先停止消费者组
    • MirrorMaker需监控复制延迟
  3. 常用诊断技巧

    bash 复制代码
    # 检查消息积压(Lag)
    bin/kafka-consumer-groups.sh --describe --group <group> | awk '{print $6}'
    
    # 检查Controller状态
    bin/kafka-metadata-quorum.sh --status

完整文档参考:Apache Kafka Operations

相关推荐
小小管写大大码2 小时前
如何让vscode变得更智能?vscode接入claude实现自动编程
运维·ide·vscode·自动化·编辑器·ai编程·腾讯云ai代码助手
zhang133830890752 小时前
CG-09H 超声波风速风向传感器 加热型 ABS材质 重量轻 没有机械部件
大数据·运维·网络·人工智能·自动化
Fᴏʀ ʏ꯭ᴏ꯭ᴜ꯭.3 小时前
Keepalived VIP迁移邮件告警配置指南
运维·服务器·笔记
物联网软硬件开发-轨物科技3 小时前
【轨物洞见】告别“被动维修”!预测性运维如何重塑老旧电站的资产价值?
运维·人工智能
程序员允诺3 小时前
[DevOps实战] 彻底解决依赖地狱:如何编译全静态、可移植的 Xorriso 工具
运维·devops
酣大智4 小时前
接口模式参数
运维·网络·网络协议·tcp/ip
一只自律的鸡4 小时前
【Linux驱动】bug处理 ens33找不到IP
linux·运维·bug
!chen4 小时前
linux服务器静默安装Oracle26ai
linux·运维·服务器
莫大3304 小时前
2核2G云服务器PHP8.5+MySQL9.0+Nginx(LNMP)安装WordPress网站详细教程
运维·服务器·nginx
刚刚入门的菜鸟4 小时前
php-curl
运维·web安全·php