Kafka入门(二):集群搭建

1. Kafka集群安装

官网下载Kafka或者点击这里下载,并解压到/usr/local/kafka/

tar -xzf kafka_2.12-2.3.0.tgz

Kafka使用ZooKeeper,可以使用Kafka下载包中的脚本启动一个单节点zookeeper。

bin/zookeeper-server-start.sh config/zookeeper.properties

搭建本地集群,共三个节点:节点1: 192.168.0.214:9092节点2: 192.168.0.214:9093节点3: 192.168.0.214:9094,配置节点1并启动如下:
拷贝Kafka配置文件:

cp config/server.properties config/server-9092.properties

修改以下参数:

broker.id=server-9092 
listeners=PLAINTEXT://:9092
log.dirs=/tmp/kafka-logs-9092

启动节点1

bin/kafka-server-start.sh config/server-9092.properties

同理配置节点2节点3并启动:

bin/kafka-server-start.sh config/server-9093.properties
bin/kafka-server-start.sh config/server-9094.properties

扩容和重新分区

假设现有3个集群集群:192.168.0.214:9092192.168.0.214:9093192.168.0.214:9094,新增一个节点192.168.0.214:9095,分配一个唯一的代理ID,启动服务:

bin/kafka-server-start.sh config/server-9095.properties

原有的topic并不会在新增的服务器分配任何数据,因此需要对topic重新分配分区,官网提供了分区重新分配工具用于手动重新分区:
查看需要重新分区的topic分区情况:

bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic topic-default
Topic:topic-default	PartitionCount:6	ReplicationFactor:1	Configs:
	Topic: topic-default	Partition: 0	Leader: 2	Replicas: 2	Isr: 2
	Topic: topic-default	Partition: 1	Leader: 0	Replicas: 0	Isr: 0
	Topic: topic-default	Partition: 2	Leader: 1	Replicas: 1	Isr: 1
	Topic: topic-default	Partition: 3	Leader: 2	Replicas: 2	Isr: 2
	Topic: topic-default	Partition: 4	Leader: 0	Replicas: 0	Isr: 0
	Topic: topic-default	Partition: 5	Leader: 1	Replicas: 1	Isr: 1

当前topic-default的topic有6个分区,但是只分配了3个分区[0,1,2],因此需要重新分区
1.确定要重新分区的topic:

> cat topics-to-move.json
{
  "topics": [{"topic": "topic-default"}],
  "version":1
}

2.生成候选分配

bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --topics-to-move-json-file topics-to-move.json --broker-list "0,1,2,3" --generate
Current partition replica assignment
{"version":1,
"partitions":[{"topic":"topic-default","partition":0,"replicas":[2],"log_dirs":["any"]},
				   {"topic":"topic-default","partition":2,"replicas":[1],"log_dirs":["any"]},
				   {"topic":"topic-default","partition":5,"replicas":[1],"log_dirs":["any"]},
				   {"topic":"topic-default","partition":1,"replicas":[0],"log_dirs":["any"]},
				   {"topic":"topic-default","partition":4,"replicas":[0],"log_dirs":["any"]},
				   {"topic":"topic-default","partition":3,"replicas":[2],"log_dirs":["any"]}]
}
Proposed partition reassignment configuration
{"version":1,
"partitions":[{"topic":"topic-default","partition":5,"replicas":[0],"log_dirs":["any"]},
				   {"topic":"topic-default","partition":2,"replicas":[1],"log_dirs":["any"]},
				   {"topic":"topic-default","partition":4,"replicas":[3],"log_dirs":["any"]},
				   {"topic":"topic-default","partition":1,"replicas":[0],"log_dirs":["any"]},
				   {"topic":"topic-default","partition":0,"replicas":[3],"log_dirs":["any"]},
				   {"topic":"topic-default","partition":3,"replicas":[2],"log_dirs":["any"]}]
}

Proposed partition reassignment configuration以下的json数据保存在json文件中(例如expand-cluster-reassignment.json)
3.重新分区:

bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file expand-cluster-reassignment.json --execute

4.验证结果

bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file expand-cluster-reassignment.json --verify
Status of partition reassignment: 
Reassignment of partition topic-default-0 completed successfully
Reassignment of partition topic-default-2 completed successfully
Reassignment of partition topic-default-5 completed successfully
Reassignment of partition topic-default-1 completed successfully
Reassignment of partition topic-default-4 completed successfully
Reassignment of partition topic-default-3 completed successfully

修改topic分区数

topic-default现有分区数

bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic topic-default
Topic:topic-default	PartitionCount:6	ReplicationFactor:1	Configs:
	Topic: topic-default	Partition: 0	Leader: 3	Replicas: 3	Isr: 3
	Topic: topic-default	Partition: 1	Leader: 0	Replicas: 0	Isr: 0
	Topic: topic-default	Partition: 2	Leader: 1	Replicas: 1	Isr: 1
	Topic: topic-default	Partition: 3	Leader: 2	Replicas: 2	Isr: 2
	Topic: topic-default	Partition: 4	Leader: 3	Replicas: 3	Isr: 3
	Topic: topic-default	Partition: 5	Leader: 0	Replicas: 0	Isr: 0

修改topic分片数,注意:只可以增加分区数,不可以减少分区数

bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic topic-default --partitions 7
Topic:topic-default	PartitionCount:7	ReplicationFactor:1	Configs:
	Topic: topic-default	Partition: 0	Leader: 3	Replicas: 3	Isr: 3
	Topic: topic-default	Partition: 1	Leader: 0	Replicas: 0	Isr: 0
	Topic: topic-default	Partition: 2	Leader: 1	Replicas: 1	Isr: 1
	Topic: topic-default	Partition: 3	Leader: 2	Replicas: 2	Isr: 2
	Topic: topic-default	Partition: 4	Leader: 3	Replicas: 3	Isr: 3
	Topic: topic-default	Partition: 5	Leader: 0	Replicas: 0	Isr: 0
	Topic: topic-default	Partition: 6	Leader: 1	Replicas: 1	Isr: 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值