kafka部署配置及常用命令总结(运维必备)

学术   2024-09-05 21:00   浙江  

推荐大家关注一个公众号
点击上方 "Linux中文社区关注,星标或者置顶
21点00分准时推送,第一时间送达

责编:中文妹 | 来源:马哥Linux运维

链接:https://www.cnblogs.com/v-fan/p/15353060.html

上一篇:还在用Xshell?你out了!!
大家好,我是中文妹。

kafka部署配置及常用命令总结#

部署配置#

1.准备部署包(自行下载)
2.配置zk

vim conf/zoo.cfg

dataDir=/data/vfan/zk/data/dataLogDir=/data/vfan/zk/logs/startLogDir=/data/vfan/zk/logs/clientPort=2181maxClientCnxns=0initLimit=5syncLimit=2server.1=10.61.194.34:2801:3801server.2=10.61.199.15:2802:3802server.3=10.61.202.16:2803:3803# server.A=B:C:D  其中A是一个数字,代表这是第几号服务器;B是服务器的IP地址;C表示服务器与群集中的“领导者”交换信息的端口;当领导者失效后,D表示用来执行选举时服务器相互通信的端口snapCount=20autopurge.snapRetainCount =3autopurge.purgeInterval =1

zk集群配置如上,如果是单台,从server.1开始往下都注释即可

启动zk

bin/zkServer.sh start## ps 检查进程
 
2.配置kafka


vim kafka/config/server.propertiesbroker.id=1listeners=PLAINTEXT://10.153.204.28:9092num.network.threads=3num.io.threads=8socket.send.buffer.bytes=102400socket.receive.buffer.bytes=102400socket.request.max.bytes=104857600log.dirs=/data/vfan/kfk/logs/# 当topic不存在系统自动创建时的分区数num.partitions=3# 当topic不存在系统自动创建时的副本数default.replication.factor=3# offset topic的replicas数量offsets.topic.replication.factor=3# 每个数据目录的线程数,用于启动时的日志恢复和关闭时的刷新num.recovery.threads.per.data.dir=1# 事务主题的复制因子transaction.state.log.replication.factor=3# 覆盖事务主题的min.insync.replicas配置transaction.state.log.min.isr=3log.retention.hours=168log.segment.bytes=1073741824log.retention.check.interval.ms=300000zookeeper.connect=10.61.194.34:2181,10.61.199.15:2181,10.61.202.16zookeeper.connection.timeout.ms=6000group.initial.rebalance.delay.ms=0

集群模式时,修改每个配置文件的 broker.id listeners 即可,zookeeper.connect若为单机就写一个

启动kafka

bin/kafka-server-start.sh -daemon config/server.properties## ss -tnlp|grep 9092 检查端口

 

常用命令总结#

topic相关
## 查看所有topic./kafka-topics.sh --zookeeper localhost:2181 --list 
## 查看所有topic详情(副本、分区、ISR等)./kafka-topics.sh --zookeeper localhost:2181 --describe
## 查看某个topic详情./kafka-topics.sh --zookeeper localhost:2181 --describe --topic test
## 创建topic,3副本 3分区./kafka-topics.sh --zookeeper localhost:2181 --create --topic test --replication-factor 3 --partitions 3
## 调整分区数量./kafka-topics.sh --alter --zookeeper localhost:2181 --topic test --partitions 3
## 删除topic,需要将参数设置为delete.topic.enable=true,如果还是删不了则删除kafka中的所有分区log,及通过zk客户端删除./kafka-topics.sh --zookeeper localhost:2181 --delete --topic test
## 查看topic各个分区的消息数量./kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --time -1 --topic test

 

模拟kafka生产消费
## 生产./kafka-console-producer.sh --broker-list 10.153.204.28:9092 --topic test
## 消费,--from-beginning参数表示从头开始./kafka-console-consumer.sh --bootstrap-server 10.153.204.28:9092 --topic test --from-beginning

此处需要注意,生产者和测试者指定的broker必须和配置文件中zookeeper.connect和listeners中的地址一至,如写localhost生产者会类似如下信息:

WARN [Producer clientId=console-producer] Connection to node -1 could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)

消费者会报错类似错误:

WARN [Consumer clientId=consumer-1, groupId=console-consumer-8350] Connection to node -1 could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)

 

消费者相关
## 显示所有消费者./kafka-consumer-groups.sh --new-consumer --bootstrap-server localhost:9092 --list
## 获取某消费者消费某个topic的offset./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group test-consumer
## 调整消费者对某个topic的offset,发生阻塞等情况时可使用.kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group groupName --reset-offsets --to-offset 1000 --topic topicName --execute

 

调整默认分区副本数
## 配置文件中指定默认分区 副本数num.partitions=3 ;当topic不存在系统自动创建时的分区数default.replication.factor=3 ;当topic不存在系统自动创建时的副本数offsets.topic.replication.factor=3 ;表示kafka的内部topic consumer_offsets副本数,默认为1

 

调整topic分区副本数

目前 guoqing 的topic副本和分区都为1

./kafka-topics.sh --zookeeper localhost:2181 --describe --topic guoqingTopic:guoqing PartitionCount:1  ReplicationFactor:1 Configs:  Topic: guoqing  Partition: 0  Leader: 1 Replicas: 1 Isr: 1

 

将分区数调整为3

## 扩容./kafka-topics.sh --alter --zookeeper localhost:2181 --topic guoqing --partitions 3WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affectedAdding partitions succeeded!
## 检查./kafka-topics.sh --zookeeper localhost:2181 --describe --topic guoqingTopic:guoqing PartitionCount:3 ReplicationFactor:1 Configs: Topic: guoqing Partition: 0 Leader: 1 Replicas: 1 Isr: 1 Topic: guoqing Partition: 1 Leader: 2 Replicas: 2 Isr: 2 Topic: guoqing Partition: 2 Leader: 3 Replicas: 3 Isr: 3

注意:分区数只能增加,不能减少

 

将副本数调整为3,首先准备json文件,格式如下:


vim guoqing.json{    "version": 1,     "partitions": [        {            "topic": "guoqing",             "partition": 0,             "replicas": [                1,                 2,                 3            ]        },        {            "topic": "guoqing",             "partition": 1,             "replicas": [                2,                 1,                 3            ]        },        {            "topic": "guoqing",             "partition": 2,             "replicas": [                3,                 2,                 1            ]        }    ]} 执行调整命令./kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file /tmp/guoqing.json --executeCurrent partition replica assignment
{"version":1,"partitions":[{"topic":"guoqing","partition":0,"replicas":[1],"log_dirs":["any"]},{"topic":"guoqing","partition":2,"replicas":[3],"log_dirs":["any"]},{"topic":"guoqing","partition":1,"replicas":[2],"log_dirs":["any"]}]}
Save this to use as the --reassignment-json-file option during rollbackSuccessfully started reassignment of partitions.

 

检查调整进度

./kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file /tmp/guoqing.json --verifyStatus of partition reassignment:Reassignment of partition guoqing-0 completed successfullyReassignment of partition guoqing-1 completed successfullyReassignment of partition guoqing-2 completed successfully

 

检查调整后的状态

./kafka-topics.sh --zookeeper localhost:2181 --describe --topic guoqingTopic:guoqing PartitionCount:3  ReplicationFactor:3 Configs:  Topic: guoqing  Partition: 0  Leader: 1 Replicas: 1,2,3 Isr: 1,2,3  Topic: guoqing  Partition: 1  Leader: 2 Replicas: 2,1,3 Isr: 2,1,3  Topic: guoqing  Partition: 2  Leader: 3 Replicas: 3,2,1 Isr: 3,2,1

你还有什么想要补充的吗?

上周,又劝退十几个了。。。

ChatGPT 4.0 国内直接用 !!!

最后给大家推荐一个ChatGPT 4.0国内网站,是我们团队一直在使用的,我们对接是OpenAI官网的账号,给大家打造了一个一模一样ChatGPT,很多粉丝朋友现在也都通过我拿这种号,价格不贵,关键还有售后。

一句话说明:用官方一半价格的钱,一句话说明:用跟官方 ChatGPT4.0 一模一样功能,无需魔法,无视封号,不必担心次数不够。

最大优势:可实现会话隔离!突破限制:官方限制每个账号三小时可使用40次4.0本网站可实现次数上限之后,手动切换下一个未使用的账号【相当于一个4.0帐号,同享受一百个账号轮换使用权限】

为了跟上AI时代我干了一件事儿,我创建了一个知识星球社群:ChartGPT与副业。想带着大家一起探索ChatGPT和新的AI时代

有很多小伙伴搞不定ChatGPT账号,于是我们决定,凡是这三天之内加入ChatPGT的小伙伴,我们直接送一个正常可用的永久ChatGPT独立账户。

不光是增长速度最快,我们的星球品质也绝对经得起考验,短短一个月时间,我们的课程团队发布了8个专栏、18个副业项目

简单说下这个星球能给大家提供什么:


1、不断分享如何使用ChatGPT来完成各种任务,让你更高效地使用ChatGPT,以及副业思考、变现思路、创业案例、落地案例分享。

2、分享ChatGPT的使用方法、最新资讯、商业价值。

3、探讨未来关于ChatGPT的机遇,共同成长。

4、帮助大家解决ChatGPT遇到的问题。

5、提供一整年的售后服务,一起搞副业

星球福利:

1、加入星球4天后,就送ChatGPT独立账号。

2、邀请你加入ChatGPT会员交流群。

3、赠送一份完整的ChatGPT手册和66个ChatGPT副业赚钱手册。

其它福利还在筹划中... 不过,我给你大家保证,加入星球后,收获的价值会远远大于今天加入的门票费用 !

本星球第一期原价399,目前属于试运营,早鸟价149,每超过50人涨价10元,星球马上要来一波大的涨价,如果你还在犹豫,可能最后就要以更高价格加入了。。

早就是优势。建议大家尽早以便宜的价格加入!

  声明:本文部分素材转载自互联网,如有侵权立即删除 。


往期精彩



喜欢本文的朋友们,欢迎长按下图,关注订阅号Linux中文社区


收看更多精彩内容

Linux中文社区
号主来自世界500强,主要分享Linux干货、数据库等,一起学习成长!
 最新文章