当前位置: 首页 > news >正文

Kafka日常运维命令总结

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

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

    bin/kafka-server-start.sh -daemon <path>/server.properties
    
  3. 关闭Broker

    bin/kafka-server-stop.sh
    

二、Topic管理
操作命令
创建Topicbin/kafka-topics.sh --create --bootstrap-server localhost:9092 --partitions 3 --replication-factor 3 --topic <topicname>
删除Topicbin/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. 重设消费位移
    # 重置到最早位移
    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. 生产者控制台

    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. 消费者控制台

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

    # 生产者性能测试
    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消息数

    # 最新位移(总消息数)
    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选举

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

    # 生成迁移计划
    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. 查看日志段元数据

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

    bin/kafka-mirror-maker.sh --consumer.config consumer.conf --producer.config producer.conf --whitelist ".*"
    
  3. 查看__consumer_offsets

    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. 常用诊断技巧

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

完整文档参考:Apache Kafka Operations

http://www.dtcms.com/a/264049.html

相关文章:

  • 第4课:Flask请求与响应对象深度解析
  • 【Python】Flask网页
  • React Native 0.79.4 中 [RCTView setColor:] 崩溃问题完整解决方案
  • JavaEE初阶第六期:解锁多线程,从 “单车道” 到 “高速公路” 的编程升级(四)
  • 无法将“pytest”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
  • NLP——RNN变体LSTM和GRU
  • 【Linux】进程
  • ELK日志分析系统(filebeat+logstash+elasticsearch+kibana)
  • Pycharm安装第三方库
  • 【实战】 容器中Spring boot项目 Graphics2D 画图中文乱码解决方案
  • 脑机新手指南(二十一)基于 Brainstorm 的 MEG/EEG 数据分析(上篇)
  • ChatGPT + GitHub Copilot + Cursor 实战提升编程效率
  • Oracle 常用函数
  • WPF中Style和Template异同
  • 【CodeTop】每日练习 2025.7.1
  • 使用 Conda 工具链创建 Poetry 本地虚拟环境全记录——基于《Python 多版本与开发环境治理架构设计》
  • 迅为高情性6TOPS算力的RK3576开发板NPU rknn-model-zoo例程演示
  • Windows VMWare Centos Docker部署Springboot + mybatis + MySql应用
  • Windows版minio下载安装使用教程
  • 最大子数组和-力扣
  • 微软开源GitHub Copilot Chat,AI编程领域迎新突破
  • ChatGPT、DeepSeek等大语言模型助力高效办公、论文与项目撰写、数据分析、机器学习与深度学习建模
  • 康养休闲旅游服务实训室建设方案:数字化赋能与全链条实训创新
  • 在vscode中进行git推送拉取的详细方法
  • InnoDB索引
  • 深入解析NumPy的核心函数np.array()
  • ip网络基础
  • k8s一键部署tongweb企业版7049m6(by why+lqw)
  • 计网学习笔记第1章 计算机网络体系结构(灰灰题库)
  • 智能通信领域论文投稿常见问题与解决方案——基于数百篇CA检索稿件的实证分析