Kafka-0.10.0.0 集群高可靠实验
记录实验过程之前,先谈一谈学习Kafka过程的心得。
大数据包含一个生态,需众多组件配合使用。逐个学习各个组件非常费力,想摸索出一种快速入门的方法,可能是每个学习大数据的同学都想要的。
我的方法是,每个组件遵循以下步骤:
(1)介绍文档,Getting Start,Introduction等,通常1个小时内可以搞定。
(2)安装配置,下载Tar包,配置Config。半小时。
(3)用官方Api,实现几个Demo。1个小时。
这样,入门过程差不多花上半天时间。但是学习kafka时,把精力放在了zookeeper的机制原理上,大大延长了整个实验的时间。
感谢kelgon发布的实验教程,我的实验过程也是按照这位牛人的文章做的,在此感谢。
一、zookeeper安装
实验环境:三台计算机(用本机docker安装镜像,OS:ubuntu 16.04)
- hbase:172.18.0.71
- datanode2:172.18.0.12
- datanode3:172.18.0.13
解压zookeeper 3.4.9 到/opt/zookeeper文件夹中
- 配置zoo.cfg,每台zookeeper采用同样的配置文件
dataDir=/opt/zookeeper/zootmp
##存放zookeeper 全部数据
clientPort=2181 #zookeeper 默认端口
server.0=172.18.0.71:4001:4002 #配置zookeeper 集群
server.1=172.18.0.12:4001:4002
server.2=172.18.0.13:4001:4002
##4001是node通信使用的端口,4002是node选举使用的端口
2.在dataDir中,创建myid文件,写入此台机器的变化,对应server.X。
##myid
0
3.启动命令。
bin/zkServer.sh start
zookeeper状态和高可靠
bin/zkServer.sh status
命令输出
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Mode: follower/leader
证明zookeeper依靠zoo.cfg配置文件中集群配置,实现了主从模式。
关闭leader节点
bin/zkServer.sh stop
查看其它节点状态,一个follow节点会变成leader节点。
至此,完成zookeeper的安装、集群配置以及集群可靠性验证。
二、Kafka安装
解压kafka-0.10.0.0到/opt/kafka路径中,3个主机组成Kafka集群。
- hbase:172.18.0.71
- datanode2:172.18.0.12
- datanode3:172.18.0.13
1.修改配置文件config/server.properties
broker.id=0 ##每一个kafka 设置单独id
advertised.listeners=PLAINTEXT://hbase:9092
##hbase填写域名或ip地址,kafka监听该端口。
log.dirs=/opt/kafka/kafkatmp
##kafka保存数据的位置
zookeeper.connect=hbase:2181,datanode2:2181,datanode3:2181
##zookeeper地址,集群用逗号分隔
num.partitions=1 ##默认情况topic的partion个数
2.启动kafka
bin/kafka-server-start.sh -daemon config/server.properties
创建topic,设置10个partition
- kafka自带系统工具创建topic
bin/kafka-topics.sh --create \
--zookeeper 172.18.0.12:2181\
--replication-factor 2 \ ##2个副本
--partitions 10 \ ##10个partition
--topic exam2 ##
2.查看topic信息
Topic:exam2 PartitionCount:10 ReplicationFactor:2 Configs:
Topic: exam2 Partition: 0 Leader: 0 Replicas: 0,1 Isr: 0,1
Topic: exam2 Partition: 1 Leader: 1 Replicas: 1,2 Isr: 1,2
Topic: exam2 Partition: 2 Leader: 2 Replicas: 2,0 Isr: 0,2
Topic: exam2 Partition: 3 Leader: 0 Replicas: 0,2 Isr: 0,2
Topic: exam2 Partition: 4 Leader: 1 Replicas: 1,0 Isr: 0,1
Topic: exam2 Partition: 5 Leader: 2 Replicas: 2,1 Isr: 1,2
Topic: exam2 Partition: 6 Leader: 0 Replicas: 0,1 Isr: 0,1
Topic: exam2 Partition: 7 Leader: 1 Replicas: 1,2 Isr: 1,2
Topic: exam2 Partition: 8 Leader: 2 Replicas: 2,0 Isr: 0,2
Topic: exam2 Partition: 9 Leader: 0 Replicas: 0,2 Isr: 0,2
10个partition分布到3个broker上,每个partition拥有2个副本。
三、Kafka可靠性验证
1.关闭broker0
bin/kafka-server-stop.sh
2.查看topic信息
Topic:exam2 PartitionCount:10 ReplicationFactor:2 Configs:
Topic: exam2 Partition: 0 Leader: 1 Replicas: 0,1 Isr: 1
Topic: exam2 Partition: 1 Leader: 1 Replicas: 1,2 Isr: 2,1
Topic: exam2 Partition: 2 Leader: 2 Replicas: 2,0 Isr: 2
Topic: exam2 Partition: 3 Leader: 2 Replicas: 0,2 Isr: 2
Topic: exam2 Partition: 4 Leader: 1 Replicas: 1,0 Isr: 1
Topic: exam2 Partition: 5 Leader: 2 Replicas: 2,1 Isr: 1,2
Topic: exam2 Partition: 6 Leader: 1 Replicas: 0,1 Isr: 1
Topic: exam2 Partition: 7 Leader: 1 Replicas: 1,2 Isr: 2,1
Topic: exam2 Partition: 8 Leader: 2 Replicas: 2,0 Isr: 2
Topic: exam2 Partition: 9 Leader: 2 Replicas: 0,2 Isr: 2
由于关闭了broker0,所以partition-0的leader由broker0变为broker1,lsr同步节点也去掉了broker0。
partion3 6 9发生同样的变化。
- 重启broker0,查看topic信息
Topic:exam2 PartitionCount:10 ReplicationFactor:2 Configs:
Topic: exam2 Partition: 0 Leader: 1 Replicas: 0,1 Isr: 1,0
Topic: exam2 Partition: 1 Leader: 1 Replicas: 1,2 Isr: 2,1
Topic: exam2 Partition: 2 Leader: 2 Replicas: 2,0 Isr: 2,0
Topic: exam2 Partition: 3 Leader: 2 Replicas: 0,2 Isr: 2,0
Topic: exam2 Partition: 4 Leader: 1 Replicas: 1,0 Isr: 1,0
Topic: exam2 Partition: 5 Leader: 2 Replicas: 2,1 Isr: 1,2
Topic: exam2 Partition: 6 Leader: 1 Replicas: 0,1 Isr: 1,0
Topic: exam2 Partition: 7 Leader: 1 Replicas: 1,2 Isr: 2,1
Topic: exam2 Partition: 8 Leader: 2 Replicas: 2,0 Isr: 2,0
Topic: exam2 Partition: 9 Leader: 2 Replicas: 0,2 Isr: 2,0
此时,lsr节点已经恢复broker0的同步规则。
4.触发负载均衡
partition的读写都是leader broker负责。当重启broker后,虽然加入了数据同步节点,但partition的leader没有发生转换,可以通过命令触发负载均衡。
bin/kafka-preferred-replica-election.sh --zookeeper hbase:2181
Topic:exam2 PartitionCount:10 ReplicationFactor:2 Configs:
Topic: exam2 Partition: 0 Leader: 0 Replicas: 0,1 Isr: 1,0
Topic: exam2 Partition: 1 Leader: 1 Replicas: 1,2 Isr: 2,1
Topic: exam2 Partition: 2 Leader: 2 Replicas: 2,0 Isr: 2,0
Topic: exam2 Partition: 3 Leader: 0 Replicas: 0,2 Isr: 2,0
Topic: exam2 Partition: 4 Leader: 1 Replicas: 1,0 Isr: 1,0
Topic: exam2 Partition: 5 Leader: 2 Replicas: 2,1 Isr: 1,2
Topic: exam2 Partition: 6 Leader: 0 Replicas: 0,1 Isr: 1,0
Topic: exam2 Partition: 7 Leader: 1 Replicas: 1,2 Isr: 2,1
Topic: exam2 Partition: 8 Leader: 2 Replicas: 2,0 Isr: 2,0
Topic: exam2 Partition: 9 Leader: 0 Replicas: 0,2 Isr: 2,0
结果上看,partition 0 3 6 9的leader换成了broker0,实现了重新选举。
四、总结
本文通过搭建Kafka集群,验证了kafka系统中topic
broker
leader
Replicas
等概念。
通过手动关闭进程的方式,实现了kafka高可用性、负载均衡。