Kafka集群完全分布式安装

一、上传、解压kafka压缩包

将kafka压缩包上传到Linux系统中,并进行解压

[root@hadoop1 software]# pwd
/home/software
[root@hadoop1 software]# ll
总用量 48352
drwxr-xr-x  8 uucp    143     4096 10月  8 11:41 jdk1.8
drwxr-xr-x  8 root  root      4096 11月 28 10:47 kafka_2.11-1.0.0
-rw-r--r--  1 root  root  49475271 11月 26 2017 kafka_2.11-1.0.0.tgz
drwxr-xr-x  7 root  root      4096 11月 28 14:56 kafka-manager-1.3.2.1
[root@hadoop1 software]#

二、修改配置文件

进入到kafka的目录下的conf目录下,修改server.properties文件

[root@hadoop1 config]# pwd
/home/software/kafka_2.11-1.0.0/config
[root@hadoop1 config]# ll
总用量 64
-rw-r--r-- 1 root root  906 10月 27 2017 connect-console-sink.properties
-rw-r--r-- 1 root root  909 10月 27 2017 connect-console-source.properties
-rw-r--r-- 1 root root 5807 10月 27 2017 connect-distributed.properties
-rw-r--r-- 1 root root  883 10月 27 2017 connect-file-sink.properties
-rw-r--r-- 1 root root  881 10月 27 2017 connect-file-source.properties
-rw-r--r-- 1 root root 1111 10月 27 2017 connect-log4j.properties
-rw-r--r-- 1 root root 2730 10月 27 2017 connect-standalone.properties
-rw-r--r-- 1 root root 1221 10月 27 2017 consumer.properties
-rw-r--r-- 1 root root 4727 10月 27 2017 log4j.properties
-rw-r--r-- 1 root root 1919 10月 27 2017 producer.properties
-rw-r--r-- 1 root root 6908 11月 28 10:44 server.properties
-rw-r--r-- 1 root root 1032 10月 27 2017 tools-log4j.properties
-rw-r--r-- 1 root root 1023 10月 27 2017 zookeeper.properties
[root@hadoop1 config]# vim server.properties

修改如下配置(port没有的话,需要自己添加)

broker.id=1

port=9092

log.dirs=/home/software/kafka_2.11-1.0.0/tmp

zookeeper.connect=hadoop1:2181,hadoop2:2181,hadoop3:2181

 

将kafka_2.11-1.0.0目录发到hadoop2和hadoop3对应的目录下

[root@hadoop1 software]# pwd
/home/software
[root@hadoop1 software]# scp -r kafka_2.11-1.0.0 root@hadoop2:/home/software/

修改server.properties文件,修改broker.id(分别为2和3,集群内的所有机器broker.id值不能一样)

三、启动

  • 启动zookeeper,在各个机器的zookeeper的bin目录下执行:./zkServer.sh start
[root@hadoop1 bin]# pwd
/home/software/zookeeper/bin
[root@hadoop1 bin]# ./zkServer.sh start
  • 启动kafka,在各个机器的kafka的bin目录下执行:./kafka-server-start.sh ../config/server.properties
[root@hadoop1 bin]# pwd
/home/software/kafka_2.11-1.0.0/bin
[root@hadoop1 bin]# ./kafka-server-start.sh ../config/server.properties

四、测试

在一个节点(本文中选择hadoop1节点)的kafka的bin目录下执行以下操作

  • 创建一个拥有2个副本的topic
[root@hadoop1 bin]# pwd
/home/software/kafka_2.11-1.0.0/bin
[root@hadoop1 bin]# ./kafka-topics.sh --create --zookeeper hadoop1:2181 --replication-factor 2 --partitions 1 --topic park

--replication-factor:副本的数量

--partition:分区数

--topic:主题

  • 查看主题
[root@hadoop1 bin]# pwd
/home/software/kafka_2.11-1.0.0/bin
[root@hadoop1 bin]# ./kafka-topics.sh --list --zookeeper hadoop1:2181
__consumer_offsets
park
  • 查看每个节点的信息

[root@hadoop1 bin]# pwd
/home/software/kafka_2.11-1.0.0/bin
[root@hadoop1 bin]# ./kafka-topics.sh --describe --zookeeper hadoop1:2181 --topic park
Topic:park    PartitionCount:1    ReplicationFactor:2    Configs:
    Topic: park    Partition: 0    Leader: 2    Replicas: 3,2    Isr: 2,3

可以看到,主题名为park;分区数量为1;副本数量为2;leader为2,对应hadoop2

  • 启动生产者,向topic发送消息

[root@hadoop1 bin]# pwd
/home/software/kafka_2.11-1.0.0/bin
[root@hadoop1 bin]# ./kafka-console-producer.sh --broker-list hadoop1:9092,hadoop2:9092,hadoop3:9092 --topic park

>

  • 启动消费者,从topic中消费消息

[root@hadoop1 bin]# pwd
/home/software/kafka_2.11-1.0.0/bin
[root@hadoop1 bin]# ./kafka-console-consumer.sh --zookeeper hadoop1:2181 --topic park
Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].

  • 生产消费过程

生产者每生产一条数据,消费者便消费一条数据

五、试验:容错性

将hadoop2的kafka停掉(可以使用kill -9 xxx或其他方法),然后查看节点信息

[root@hadoop2 bin]# pwd
/home/software/kafka_2.11-1.0.0/bin
[root@hadoop2 bin]# jps
1937 QuorumPeerMain
1993 Kafka
2319 Jps
[root@hadoop2 bin]# kill -9 1993
[root@hadoop2 bin]# ./kafka-topics.sh --describe --zookeeper hadoop1:2181 --topic park
Topic:park    PartitionCount:1    ReplicationFactor:2    Configs:
    Topic: park    Partition: 0    Leader: 3    Replicas: 3,2    Isr: 3
[root@hadoop2 bin]#

可以看到,此时leader变成了hadoop3

重新启动hadoop2的kafka,leader不变,仍然是hadoop3

[root@hadoop2 bin]# jps
1937 QuorumPeerMain
2874 Jps
2575 Kafka
[root@hadoop2 bin]# ./kafka-topics.sh --describe --zookeeper hadoop1:2181 --topic park
Topic:park    PartitionCount:1    ReplicationFactor:2    Configs:
    Topic: park    Partition: 0    Leader: 3    Replicas: 3,2    Isr: 3,2
[root@hadoop2 bin]#

可以看到,某一个机器宕机了之后,集群仍然正常工作,从而保证了kafka的容错性。

更多详细内容,请参考Kafka的官方文档:https://kafka.apache.org/documentation/

猜你喜欢

转载自blog.csdn.net/Johnson8702/article/details/84588095