一、相关知识
备份因子最多为集群服务器数量,如果只有一台服务器,那么备份因子只能设置为1,三台kafka集群的话,备份因子可以设置为2或者3,
我们来看设置不同参数服务器的表现:
假设我们现有三台Kafka服务器集群,每个节点的broker.id分别为0/1/2,然后创建一个Topic,有三个分区,备份因子为3
[root@Basic kafka_2.11-1.1.0]# bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 3 --topic test
接下来查看集群节点状态
[root@Basic kafka_2.11-1.1.0]# bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test
Topic:test PartitionCount:3 ReplicationFactor:3 Configs:
Topic: test Partition: 0 Leader: 2 Replicas: 2,0,1 Isr: 2,0,1
Topic: test Partition: 1 Leader: 0 Replicas: 0,1,2 Isr: 0,1,2
Topic: test Partition: 2 Leader: 1 Replicas: 1,2,0 Isr: 1,2,0
PartitionCount: 3 表示有3个分区
ReplicationFactor: 3 表示复制因子为3
Topic: test Partition: 0 Leader: 2 Replicas: 2,0,1 Isr: 2,0,1
分区下标是从0开始的,也就是第一个分区P0
leader: 2 表示P0的Leader是在broker.id=2的集群节点上
Replicas: 2,0,1 列出了所有的副本节点,不管节点是否在服务中
Isr: 2,0,1 是正在服务中的节点,如果其中一个节点挂掉了,那么这里就会减少哪个挂掉节点的broker.id
上面描述的是,Topic test有三个分区,其中分区P0的Leader在broker.id=2的节点上,P0备份在broker.id=0,1,2 上面都有,然后分区P1的Leader在broker.id=0的节点上,P2的Leader在broker.id=1的节点上,另外两个节点上都有备份。
这是我自己花的图,有点丑,将就着看,不过意思很明确了。红色部分表示Leader分区,-R表示备份的follower分区。
那么如果设置分区为3,备份因子为2了。
[root@Basic kafka_2.11-1.1.0]# bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 2 --partitions 3 --topic test2
Created topic "test2".
[root@Basic kafka_2.11-1.1.0]# bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test2
Topic:test2 PartitionCount:3 ReplicationFactor:2 Configs:
Topic: test2 Partition: 0 Leader: 1 Replicas: 1,2 Isr: 1,2
Topic: test2 Partition: 1 Leader: 2 Replicas: 2,0 Isr: 2,0
Topic: test2 Partition: 2 Leader: 0 Replicas: 0,1 Isr: 0,1
二、Kafka集群
这里没有进行zk集群,zk集群的话,需要修改kafka server.properties里面的zk端口配置成集群。
这里按照官方演示代码,搭建一个分区,3个复制因子,单节点zk的伪服务器集群。
- 复制两个kafka服务的配置文件
[root@Basic kafka_2.11-1.1.0]# cp config/server.properties config/server-1.properties
[root@Basic kafka_2.11-1.1.0]# cp config/server.properties config/server-2.properties
- 修改配置参数
broker.id, listeners, log.dir
[root@Basic kafka_2.11-1.1.0]# cd config/
[root@Basic kafka_2.11-1.1.0]# vim config/server-1.properties
broker.id=1
listeners=PLAINTEXT://:9093
log.dir=/tmp/kafka-logs-1
[root@Basic kafka_2.11-1.1.0]# vim config/server-2.properties
broker.id=2
listeners=PLAINTEXT://:9094
log.dir=/tmp/kafka-logs-2
- 启动kafka服务
[root@Basic kafka_2.11-1.1.0]# bin/kafka-server-start.sh config/server.properties &
[root@Basic kafka_2.11-1.1.0]# bin/kafka-server-start.sh config/server-1.properties &
[root@Basic kafka_2.11-1.1.0]# bin/kafka-server-start.sh config/server-2.properties &
测试:
- 创建一个新的Topic, 由于有三个kafka进行集群,这里的复制因子设置为3
[root@Basic kafka_2.11-1.1.0]# bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic test_replicated_topic
WARNING: Due to limitations in metric names, topics with a period ('.') or underscore ('_') could collide. To avoid issues it is best to use either, but not both.
Created topic "test_replicated_topic".
- 查看集群状态
[root@Basic kafka_2.11-1.1.0]# bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test_replicated_topic
Topic:test_replicated_topic PartitionCount:1 ReplicationFactor:3 Configs:
Topic: test_replicated_topic Partition: 0 Leader: 0 Replicas: 0,1,2 Isr: 0,1,2
可以看到上面只有1个分区,leader分区是在broker.id=0的kafka集群节点上
- 创建一个生产者,连接到kafka集群中的某个节点9092
[root@Basic kafka_2.11-1.1.0]# bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test_replicated_topic
> hello, kafka cluster
- 创建3个消费者,分别连接到集群中的每个节点
[root@Basic kafka_2.11-1.1.0]# bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test_replicated_topic --from-beginning
hello, kafka cluster
[root@Basic kafka_2.11-1.1.0]# bin/kafka-console-consumer.sh --bootstrap-server localhost:9093 --topic test_replicated_topic --from-beginning
hello, kafka cluster
[root@Basic kafka_2.11-1.1.0]# bin/kafka-console-consumer.sh --bootstrap-server localhost:9094 --topic test_replicated_topic --from-beginning
hello, kafka cluster
可以看到每个消费者都能接收到信息
同理,我们创建几个生产者,在9093, 9094服务上,同样其他的消费者节点也能收到消息。
三、容灾测试
我们关掉leader分区的kafka,也就是端口为9092,配置文件为server.properties,broker.id=0的服务。
[root@Basic kafka_2.11-1.1.0]# jps -lm
5828 kafka.Kafka config/server-1.properties
5241 org.apache.zookeeper.server.quorum.QuorumPeerMain config/zookeeper.properties
5515 kafka.Kafka config/server.properties
6139 kafka.Kafka config/server-2.properties
8932 sun.tools.jps.Jps -lm
[root@Basic kafka_2.11-1.1.0]# kill -9 5515
查看集群状态
[root@Basic kafka_2.11-1.1.0]# bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test_replicated_topic
Topic:test_replicated_topic PartitionCount:1 ReplicationFactor:3 Configs:
Topic: test_replicated_topic Partition: 0 Leader: 2 Replicas: 0,1,2 Isr: 2,1
可以看到Leader分区所在broker.id=2的kafka服务节点了,并且Isr正常服务的节点只剩下broker.id=1/2了,broker.id=0的服务不可用了。
然后使用生产发送一条消息,由于9092挂掉了,我们连接9093操作。
[root@Basic kafka_2.11-1.1.0]# bin/kafka-console-producer.sh --broker-list localhost:9093 --topic test_replicated_topic
> test
[root@Basic kafka_2.11-1.1.0]# bin/kafka-console-consumer.sh --bootstrap-server localhost:9093 --topic test_replicated_topic --from-beginning
test
发现收发消息没有问题,说明集群中有节点出现问题,集群还能正常运行,并且排除掉出现问题的节点,重新选举Leader。
再次启动之前干掉的broker.id=0的kafka服务,然后查看Topic集群状态,发现Isr中已经再次加上了broker.id为0的节点。
[root@Basic kafka_2.11-1.1.0]# bin/kafka-server-start.sh config/server.properties &
[root@Basic kafka_2.11-1.1.0]# bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test_replicated_topic
Topic:test_replicated_topic PartitionCount:1 ReplicationFactor:3 Configs:
Topic: test_replicated_topic Partition: 0 Leader: 2 Replicas: 0,1,2 Isr: 2,1,0
测试发送接收没有问题,说明集群支持节点自动恢复。