拓扑结构介绍:
192.168.43.60 kafka01
192.168.43.61 kafka02
192.168.43.62 kafka03
OS : CentOS Linux release 7.7.1908 (Core)
JAVA : jdk-8u162-linux-x64.tar.gz
Kafka : kafka_2.11-2.0.0 .tgz
Zookeeper : zookeeper-3.4.12.tar.gz
一、系统基本配置(所有节点执行)
1.基本软件安装
[root@kafka01 ~]# yum install -y wget curl vim net-tools ntp
2.关闭SELINUX及防火墙及/etc/hosts修改
[root@kafka01 ~]# grep '^SELINUX' /etc/selinux/config
SELINUX=disabled
SELINUXTYPE=targeted
[root@kafka01 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[root@kafka01 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.43.60 kafka01
192.168.43.61 kafka02
192.168.43.62 kafka03
[root@kafka01 ~]#
3.开启NTP服务
[root@kafka01 ~]# grep '^server' /etc/ntp.conf
server ntp.aliyun.com
[root@kafka01 ~]# systemctl enable ntpd
[root@kafka01 ~]# systemctl start ntpd
[root@kafka01 ~]#
二、基础环境的配置(所有节点执行)
1.jdk1.8的安装
[root@kafka01 opt]# cd /opt
[root@kafka01 opt]# ls
jdk-8u162-linux-x64.tar.gz kafka_2.11-2.0.0 .tgz zookeeper-3.4.12.tar.gz
[root@kafka01 opt]# tar -xf jdk-8u162-linux-x64.tar.gz
[root@kafka01 opt]# ls
jdk1.8.0_162 kafka_2.11-2.0.0 .tgz
jdk-8u162-linux-x64.tar.gz zookeeper-3.4.12.tar.gz
[root@kafka01 opt]# ls jdk1.8.0_162/
bin javafx-src.zip man THIRDPARTYLICENSEREADME-JAVAFX.txt
COPYRIGHT jre README.html THIRDPARTYLICENSEREADME.txt
db lib release
include LICENSE src.zip
[root@kafka01 opt]# mv jdk1.8.0_162 /usr/local/
[root@kafka01 opt]# ls
jdk-8u162-linux-x64.tar.gz kafka_2.11-2.0.0 .tgz zookeeper-3.4.12.tar.gz
[root@kafka01 opt]# vim /etc/profile
[root@kafka01 opt]# tail -n 4 /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_162
export JRE_HOME=/usr/local/jdk1.8.0_162/jre
export CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
[root@kafka01 opt]# source /etc/profile
[root@kafka01 opt]# java -version
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
[root@kafka01 opt]#
2.zookeeper的配置
[root@kafka01 opt]# tar -xf zookeeper-3.4.12.tar.gz
[root@kafka01 opt]# ls
jdk-8u162-linux-x64.tar.gz zookeeper-3.4.12
kafka_2.11-2.0.0 .tgz zookeeper-3.4.12.tar.gz
[root@kafka01 opt]# mv zookeeper-3.4.12 /usr/local/
[root@kafka01 opt]# vim /etc/profile
[root@kafka01 opt]# tail -n 2 /etc/profile
export ZK_HOME=/usr/local/zookeeper-3.4.12
export PATH=$ZK_HOME/bin:$PATH
[root@kafka01 opt]# source /etc/profile
[root@kafka01 opt]# cp /usr/local/zookeeper-3.4.12/conf/zoo_sample.cfg /usr/local/zookeeper-3.4.12/conf/zoo.cfg
[root@kafka01 opt]#
[root@kafka01 opt]# vim /usr/local/zookeeper-3.4.12/conf/zoo.cfg
[root@kafka01 opt]# tail -n 4 /usr/local/zookeeper-3.4.12/conf/zoo.cfg
dataDir=/usr/local/zookeeper-3.4.12/data
server.1=192.168.43.60:2888:3888
server.2=192.168.43.61:2888:3888
server.3=192.168.43.62:2888:3888
[root@kafka01 opt]#
[root@kafka01 opt]# mkdir /usr/local/zookeeper-3.4.12/data
--------------------------------------------------
说明:zookeeper三个端口作用:
2181:对cline端提供服务
2888:集群内机器通讯使用(Leader监听此端口)
3888:选举leader时机器之间通信使用
--------------------------------------------------
3.创建myid文件,在myid文件中添加本机的 server ID
kafka01上:
[root@kafka01 opt]# echo "1" > /usr/local/zookeeper-3.4.12/data/myid
[root@kafka01 opt]#
kafka02上:
[root@kafka02 opt]# echo "2" > /usr/local/zookeeper-3.4.12/data/myid
[root@kafka02 opt]#
kafka03上:
[root@kafka03 opt]# echo "3" > /usr/local/zookeeper-3.4.12/data/myid
[root@kafka03 opt]#
4.启动zookeeper
kafka01上:
[root@kafka01 opt]# /usr/local/zookeeper-3.4.12/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.12/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@kafka01 opt]# /usr/local/zookeeper-3.4.12/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.12/bin/../conf/zoo.cfg
Mode: follower
kafka02上:
[root@kafka02 opt]# /usr/local/zookeeper-3.4.12/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.12/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@kafka02 opt]# /usr/local/zookeeper-3.4.12/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.12/bin/../conf/zoo.cfg
Mode: follower
kafka03上:
[root@kafka03 opt]# /usr/local/zookeeper-3.4.12/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.12/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@kafka03 opt]# /usr/local/zookeeper-3.4.12/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.12/bin/../conf/zoo.cfg
Mode: leader
三、搭建Kafka集群
1.安装kafka
[root@kafka01 opt]# ls
jdk-8u162-linux-x64.tar.gz zookeeper-3.4.12.tar.gz
kafka_2.11-2.0.0 .tgz zookeeper.out
[root@kafka01 opt]# tar -xf kafka_2.11-2.0.0\ .tgz
[root@kafka01 opt]# ls
jdk-8u162-linux-x64.tar.gz kafka_2.11-2.0.0 .tgz zookeeper.out
kafka_2.11-2.0.0 zookeeper-3.4.12.tar.gz
[root@kafka01 opt]# mv kafka_2.11-2.0.0 kafka
[root@kafka01 opt]# ls
jdk-8u162-linux-x64.tar.gz kafka_2.11-2.0.0 .tgz zookeeper.out
kafka zookeeper-3.4.12.tar.gz
[root@kafka01 opt]# mv kafka /usr/local/
[root@kafka01 opt]# vim /etc/profile
[root@kafka01 opt]# source /etc/profile
[root@kafka01 opt]# tail -n 2 /etc/profile
export KAFKA_HOME=/usr/local/kafka
PATH=${KAFKA_HOME}/bin:$PATH
[root@kafka01 opt]#
2.修改kafka配置文件
kafka01上:
[root@kafka01 opt]# vim /usr/local/kafka/config/server.properties
[root@kafka01 opt]# grep '^listeners' /usr/local/kafka/config/server.properties
listeners=PLAINTEXT://192.168.43.60:9092
[root@kafka01 opt]# grep '^zookeeper.connect' /usr/local/kafka/config/server.properties
zookeeper.connect=192.168.43.60:2181,192.168.43.61:2181,192.168.43.62:2181
zookeeper.connection.timeout.ms=6000
[root@kafka01 opt]# grep '^broker.id' /usr/local/kafka/config/server.properties
broker.id=1
kafka02上:
[root@kafka02 opt]# vim /usr/local/kafka/config/server.properties
[root@kafka02 opt]# grep '^listeners' /usr/local/kafka/config/server.properties
listeners=PLAINTEXT://192.168.43.61:9092
[root@kafka02 opt]# grep '^zookeeper.connect' /usr/local/kafka/config/server.properties
zookeeper.connect=192.168.43.60:2181,192.168.43.61:2181,192.168.43.62:2181
zookeeper.connection.timeout.ms=6000
[root@kafka02 opt]# grep '^broker.id' /usr/local/kafka/config/server.properties
broker.id=2
kafka03上:
[root@kafka03 opt]# vim /usr/local/kafka/config/server.properties
[root@kafka03 opt]# grep '^listeners' /usr/local/kafka/config/server.properties
listeners=PLAINTEXT://192.168.43.62:9092
[root@kafka03 opt]# grep '^zookeeper.connect' /usr/local/kafka/config/server.properties
zookeeper.connect=192.168.43.60:2181,192.168.43.61:2181,192.168.43.62:2181
zookeeper.connection.timeout.ms=6000
[root@kafka03 opt]# grep '^broker.id' /usr/local/kafka/config/server.properties
broker.id=3
3.启动kafka(所有节点执行)
[root@kafka01 opt]# /usr/local/kafka/bin/kafka-server-start.sh -daemon /usr/local/kafka/config/server.properties
[root@kafka01 tmp]# netstat -anultp | grep 9092
tcp6 0 0 192.168.43.60:9092 :::* LISTEN 12695/java
tcp6 0 0 192.168.43.60:9092 192.168.43.62:41296 ESTABLISHED 12695/java
tcp6 0 0 192.168.43.60:51166 192.168.43.60:9092 ESTABLISHED 12695/java
tcp6 0 0 192.168.43.60:9092 192.168.43.61:51912 ESTABLISHED 12695/java
tcp6 0 0 192.168.43.60:47752 192.168.43.61:9092 ESTABLISHED 12695/java
tcp6 0 0 192.168.43.60:54300 192.168.43.62:9092 ESTABLISHED 12695/java
tcp6 0 0 192.168.43.60:9092 192.168.43.60:51166 ESTABLISHED 12695/java
[root@kafka01 tmp]# netstat -anultp | grep 2181
tcp6 0 0 :::2181 :::* LISTEN 11460/java
tcp6 0 0 192.168.43.60:46754 192.168.43.60:2181 ESTABLISHED 12695/java
tcp6 0 0 192.168.43.60:2181 192.168.43.60:46762 TIME_WAIT -
tcp6 0 0 192.168.43.60:2181 192.168.43.60:46754 ESTABLISHED 11460/java
4.创建一个topic
[root@kafka01 tmp]# /usr/local/kafka/bin/kafka-topics.sh --create --zookeeper 192.168.43.60:2181,192.168.43.61:2181,192.168.43.62:2181 --replication-factor 3 --partitions 1 --topic test-topic
Created topic "test-topic".
[root@kafka01 tmp]# /usr/local/kafka/bin/kafka-topics.sh --describe --zookeeper 192.168.43.60:2181 --topic test-topic
Topic:test-topic PartitionCount:1 ReplicationFactor:3 Configs:
Topic: test-topic Partition: 0 Leader: 1 Replicas: 1,3,2 Isr: 1,3,2
[root@kafka01 tmp]#
5.创建生产者与消费者进行测试
生产者:
[root@kafka01 tmp]# kafka-console-producer.sh --broker-list 192.168.43.60:9092,192.168.43.61:9092,192.168.43.62:9092 --topic test-topic
>12313
>hello,this is test kafka.
>good.
>it runs ok.
>hello,kakka
>this is pony Ma.
>ok,bye.
>exit
>exit()
>bye
>quit
>^C[root@kafka01 tmp]#
[root@kafka01 tmp]#
消费者:
[root@kafka01 ~]# kafka-console-consumer.sh --bootstrap-server 192.168.43.60:9092,192.168.43.61:9092,192.168.43.62:9092 --from-beginning --topic test-topic
12313
hello,this is test kafka.
good.
it runs ok.
hello,kakka
this is pony Ma.
ok,bye.
exit
exit()
bye
quit
^CProcessed a total of 11 messages
[root@kafka01 ~]#
参阅:
https://www.jianshu.com/p/bdd9608df6b3
https://www.cnblogs.com/sujing/p/10960832.html