【ELK】【Linux】搭建【kafka】集群
*前提,已经搭建好zookeeper集群,搭建步骤参见:http://xixuefeng.top/archives/859
Linux:CentOS 7.3
kafka:kafka_2.12-1.0.0.tgz
0:概念
- 主题(Topic):一个主题类似新闻中的体育、娱乐、教育等分类概念,在实际工程中通常一个业务一个主题。
- 分区(Partition):一个Topic中的消息数据按照多个分区组织,分区是kafka消息队列组织的最小单位,一个分区可以看作是一个FIFO( First Input First Output的缩写,先入先出队列)的队列。
1:下载kafka软件(下载二进制,不要下载kafka源文件)
http://kafka.apache.org/downloads.html
2:上传kafka软件至各个节点,步骤略
3:各个节点分别解tar包
|
1 2 3 4 5 6 7 8 |
## 以节点1为例 [root@node1 soft]# tar -xvf kafka_2.12-1.0.0.tgz -C /usr/local/ ...... [root@node1 soft]# [root@node1 soft]# cd /usr/local/kafka_2.12-1.0.0/ [root@node1 kafka_2.12-1.0.0]# ls bin config libs LICENSE NOTICE site-docs [root@node1 kafka_2.12-1.0.0]# |
4:kafka配置文件名称及位置
我们只关注server.properties文件即可,虽然这里包含了zookeeper的配置文件,因为我们zookeeper已经搭建完成,所以其他配置文件可以暂时忽略(也建议zookeeper单独搭建,不用kafka自带的)
|
1 2 3 4 5 6 7 8 |
[root@node1 config]# pwd /usr/local/kafka_2.12-1.0.0/config [root@node1 config]# [root@node1 config]# ls connect-console-sink.properties connect-file-sink.properties connect-standalone.properties producer.properties zookeeper.properties connect-console-source.properties connect-file-source.properties consumer.properties server.properties connect-distributed.properties connect-log4j.properties log4j.properties tools-log4j.properties [root@node1 config]# |
5:编辑kafka配置文件server.properties
|
1 2 3 4 5 6 7 8 9 10 11 |
1:Kafka 集群包含一个或多个服务器,这种服务器被称为 broker,集群中,每个节点的值必须唯一 broker.id=1 2:监听,这里据说只能用IP listeners = PLAINTEXT://192.168.10.5:9092 3:日志目录,默认是在/tmp目录下,我们改为我们指定的目录,并将权限改为777 log.dirs=/data/kafka-logs 4:zookeeper的IP及端口 zookeeper.connect=192.168.10.5:2181,192.168.10.6:2181,192.168.10.7:2181 |
将如上四个信息修改完毕后,可以将这个配置文件拷贝至其他几个节点,同时要修改相应的broker.id及监听IP
6:三个节点分别启动kafka
|
1 2 3 4 5 6 7 8 |
[root@node1 kafka_2.12-1.0.0]# pwd /usr/local/kafka_2.12-1.0.0 [root@node1 kafka_2.12-1.0.0]# ls bin config libs LICENSE logs NOTICE site-docs [root@node1 kafka_2.12-1.0.0]# [root@node1 kafka_2.12-1.0.0]# sh bin/kafka-server-start.sh config/server.properties ## 或者后台运行,在尾部加上一个& [root@node1 kafka_2.12-1.0.0]# sh bin/kafka-server-start.sh config/server.properties & |
7:创建topic
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
## 创建topic [root@node1 kafka_2.12-1.0.0]# bin/kafka-topics.sh --zookeeper node3:2181 --create --topic t1 --partitions 6 --replication-factor 2 Created topic "t1". [root@node1 kafka_2.12-1.0.0]# –topic指定topic name –partitions指定分区数,这个参数需要根据broker数和数据量决定,正常情况下,每个broker上两个partition最好; –replication-factor指定partition的replicas数,建议设置为2; ## 查看所有topic列表 [root@node1 kafka_2.12-1.0.0]# bin/kafka-topics.sh --zookeeper node1:2181 --list t1 [root@node1 kafka_2.12-1.0.0]# ## 查看指定topic信息 bin/kafka-topics.sh --zookeeper node1:2181 --describe --topic t1 [root@node1 kafka_2.12-1.0.0]# bin/kafka-topics.sh --zookeeper node1:2181 --describe --topic t1 Topic:t1 PartitionCount:6 ReplicationFactor:2 Configs: Topic: t1 Partition: 0 Leader: 1 Replicas: 1,2 Isr: 1,2 Topic: t1 Partition: 1 Leader: 2 Replicas: 2,3 Isr: 2,3 Topic: t1 Partition: 2 Leader: 3 Replicas: 3,1 Isr: 3,1 Topic: t1 Partition: 3 Leader: 1 Replicas: 1,3 Isr: 1,3 Topic: t1 Partition: 4 Leader: 2 Replicas: 2,1 Isr: 2,1 Topic: t1 Partition: 5 Leader: 3 Replicas: 3,2 Isr: 3,2 [root@node1 kafka_2.12-1.0.0]# ## 增加topic分区数,为topic t2 增加3个分区 [root@node1 kafka_2.12-1.0.0]# [root@node1 kafka_2.12-1.0.0]# bin/kafka-topics.sh --zookeeper node1:2181 --create --topic t2 --partitions 3 --replication-factor 2 Created topic "t2". [root@node1 kafka_2.12-1.0.0]# bin/kafka-topics.sh --zookeeper node1:2181 --describe --topic t2 Topic:t2 PartitionCount:3 ReplicationFactor:2 Configs: Topic: t2 Partition: 0 Leader: 2 Replicas: 2,3 Isr: 2,3 Topic: t2 Partition: 1 Leader: 3 Replicas: 3,1 Isr: 3,1 Topic: t2 Partition: 2 Leader: 1 Replicas: 1,2 Isr: 1,2 [root@node1 kafka_2.12-1.0.0]# bin/kafka-topics.sh --zookeeper node1:2181 --list t1 t2 [root@node1 kafka_2.12-1.0.0]# [root@node1 kafka_2.12-1.0.0]# bin/kafka-topics.sh --zookeeper node1:2181 --alter --topic t2 --partitions 6 WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected Adding partitions succeeded! [root@node1 kafka_2.12-1.0.0]# [root@node1 kafka_2.12-1.0.0]# bin/kafka-topics.sh --zookeeper node1:2181 --describe --topic t2 Topic:t2 PartitionCount:6 ReplicationFactor:2 Configs: Topic: t2 Partition: 0 Leader: 2 Replicas: 2,3 Isr: 2,3 Topic: t2 Partition: 1 Leader: 3 Replicas: 3,1 Isr: 3,1 Topic: t2 Partition: 2 Leader: 1 Replicas: 1,2 Isr: 1,2 Topic: t2 Partition: 3 Leader: 2 Replicas: 2,3 Isr: 2,3 Topic: t2 Partition: 4 Leader: 3 Replicas: 3,1 Isr: 3,1 Topic: t2 Partition: 5 Leader: 1 Replicas: 1,2 Isr: 1,2 [root@node1 kafka_2.12-1.0.0]# ================================================================================ ## 删除topic,慎用,只会删除zookeeper中的元数据,消息文件须手动删除 bin/kafka-run-class.sh kafka.admin.DeleteTopicCommand --zookeeper node01:2181 --topic t_cdr |