赞
踩
① 安装JDK的第一步就是下载JDK 1.8的安装包,可以进入Oracle官网页面进行下载。https://www.oracle.com/java/technologies/downloads,我们选择的安装包是 jdk-12.0.1_linux-x64_bin.tar.gz
② 将其先复制至/opt/java目录下,并解压:
[root@localhost java]# tar -zvxf jdk-12.0.1_linux-x64_bin.tar.gz
[root@localhost java]# ll
drwxr-xr-x. 9 root root 107 11月 20 21:43 jdk-12.0.1
-rw-r--r--. 1 root root 189981475 11月 20 21:42 jdk-12.0.1_linux-x64_bin.tar.gz
③ 然后配置JDK的环境变量,修改/etc/profile文件并向其中添加如下配置:
export JAVA_HOME=/opt/java/jdk-12.0.1
export PATH=$PATH:$JAVA_HOME/bin
④ 执行source /etc/profile命令使配置生效,通过 java-version 命令验证JDK 是否已经安装配置成功。如果安装配置成功,则会正确显示出 JDK 的版本信息:
# 让环境变量生效
[root@localhost java]# source /etc/profile
# 查看java的版本
[root@localhost java]# java -version
java version "19.0.1" 2022-10-18
Java(TM) SE Runtime Environment (build 19.0.1+10-21)
Java HotSpot(TM) 64-Bit Server VM (build 19.0.1+10-21, mixed mode, sharing)
# 查看java的安装目录
[root@localhost java]# which java
/opt/java/jdk-19.0.1/bin/java
ZooKeeper是安装Kafka集群的必要组件,Kafka通过ZooKeeper来实施对元数据信息的管理,包括集群、broker、主题、分区等内容。
① 安装ZooKeeper的第一步也是下载相应的安装包,安装包可以从官网https://zookeeper.apache.org/中获得,我们使用的安装包是zookeeper-3.4.14.tar.gz
② 将其复制到/opt/zookeeper目录下,然后解压缩:
[root@localhost opt]# cd zookeeper/
[root@localhost zookeeper]# ll
-rw-r--r--. 1 root root 37676320 11月 20 21:50 zookeeper-3.4.14.tar.gz
[root@localhost zookeeper]# tar -zvxf zookeeper-3.4.14.tar.gz
[root@localhost zookeeper]# ll
drwxr-xr-x. 14 2002 2002 4096 3月 7 2019 zookeeper-3.4.14
-rw-r--r--. 1 root root 37676320 11月 20 21:50 zookeeper-3.4.14.tar.gz
[root@localhost zookeeper]# pwd
/opt/zookeeper
③ 修改 ZooKeeper 的配置文件:
[root@localhost zookeeper]# cd zookeeper-3.4.14
[root@localhost zookeeper-3.4.14]# cd conf/
[root@localhost conf]# ll
-rw-rw-r--. 1 2002 2002 535 3月 7 2019 configuration.xsl
-rw-rw-r--. 1 2002 2002 2161 3月 7 2019 log4j.properties
-rw-rw-r--. 1 2002 2002 922 3月 7 2019 zoo_sample.cfg
# 将文件名 zoo_sample.cfg 修改为 zoo.cfg
[root@localhost conf]# mv zoo_sample.cfg zoo.cfg
[root@localhost conf]# vi zoo.cfg
# 将dataDir修改为下面的目录
dataDir=/opt/zookeeper/zookeeper-3.4.14/data
# 创建/opt/zookeeper/zookeeper-3.4.14/data目录
[root@localhost conf]# cd /opt/zookeeper/zookeeper-3.4.14
[root@localhost zookeeper-3.4.14]# mkdir data
④ Zookeeker 客户端和服务端的启动:
[root@localhost zookeeper-3.4.14]# cd bin
[root@localhost bin]# ll
-rwxr-xr-x. 1 2002 2002 232 3月 7 2019 README.txt
-rwxr-xr-x. 1 2002 2002 1937 3月 7 2019 zkCleanup.sh
-rwxr-xr-x. 1 2002 2002 1056 3月 7 2019 zkCli.cmd
-rwxr-xr-x. 1 2002 2002 1534 3月 7 2019 zkCli.sh
-rwxr-xr-x. 1 2002 2002 1759 3月 7 2019 zkEnv.cmd
-rwxr-xr-x. 1 2002 2002 2919 3月 7 2019 zkEnv.sh
-rwxr-xr-x. 1 2002 2002 1089 3月 7 2019 zkServer.cmd
-rwxr-xr-x. 1 2002 2002 6773 3月 7 2019 zkServer.sh
-rwxr-xr-x. 1 2002 2002 996 3月 7 2019 zkTxnLogToolkit.cmd
-rwxr-xr-x. 1 2002 2002 1385 3月 7 2019 zkTxnLogToolkit.sh
# 启动Zookeeper服务端
[root@localhost bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
# 查看Zookeeper启动状态
[root@localhost bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: standalone
# 启动Zookeeper客户端
[root@localhost bin]# ./zkCli.sh
# 退出Zookeeper客户端
[zk: localhost:2181(CONNECTED) 0] quit
Quitting...
# 停止zookeeper服务
[root@localhost bin]# ./zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/zookeeper-3.4.14/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
# 启动Zookeeper服务端
[root@localhost bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@localhost bin]#
① 在安装完JDK和ZooKeeper之后,就可以执行Kafka broker的安装了,首先也是从官网http://kafka.apache.org/downloads中下载安装包,示例中选用按照包的是kafka_2.12-2.2.1.tgz
② 将其复制至/opt/kafka/目录下并进行解压缩:
[root@localhost bin]# cd /opt/kafka/
[root@localhost kafka]# ll
-rw-r--r--. 1 root root 57054917 11月 20 22:09 kafka_2.12-2.2.1.tgz
[root@localhost kafka]# tar -zvxf kafka_2.12-2.2.1.tgz
[root@localhost kafka]# ll
drwxr-xr-x. 6 root root 89 5月 14 2019 kafka_2.12-2.2.1
-rw-r--r--. 1 root root 57054917 11月 20 22:09 kafka_2.12-2.2.1.tgz
③ 修改broker的配置文件 server.properties,主要关注以下几个配置参数即可:
broker.id=0
listeners=PLAINTEXT://:9092
log.dirs=/opt/kafka/kafka_2.12-2.2.1/logs
zookeeper.connect=localhost:2181
# 允许外部端口连接
listeners=PLAINTEXT://192.168.38.22:9092
# 外部代理地址
advertised.listeners=PLAINTEXT://192.168.38.22:9092
[root@localhost kafka]# cd kafka_2.12-2.2.1
[root@localhost kafka_2.12-2.2.1]# ll
drwxr-xr-x. 3 root root 4096 5月 14 2019 bin
drwxr-xr-x. 2 root root 4096 5月 14 2019 config
drwxr-xr-x. 2 root root 4096 11月 20 22:10 libs
-rw-rw-r--. 1 root root 32216 5月 14 2019 LICENSE
-rw-rw-r--. 1 root root 336 5月 14 2019 NOTICE
drwxr-xr-x. 2 root root 44 5月 14 2019 site-docs
[root@localhost kafka_2.12-2.2.1]# cd config/
[root@localhost config]# ll
-rw-rw-r--. 1 root root 906 5月 14 2019 connect-console-sink.properties
-rw-rw-r--. 1 root root 909 5月 14 2019 connect-console-source.properties
-rw-rw-r--. 1 root root 5321 5月 14 2019 connect-distributed.properties
-rw-rw-r--. 1 root root 883 5月 14 2019 connect-file-sink.properties
-rw-rw-r--. 1 root root 881 5月 14 2019 connect-file-source.properties
-rw-rw-r--. 1 root root 1111 5月 14 2019 connect-log4j.properties
-rw-rw-r--. 1 root root 2262 5月 14 2019 connect-standalone.properties
-rw-rw-r--. 1 root root 1221 5月 14 2019 consumer.properties
-rw-rw-r--. 1 root root 4727 5月 14 2019 log4j.properties
-rw-rw-r--. 1 root root 1925 5月 14 2019 producer.properties
-rw-rw-r--. 1 root root 6851 5月 14 2019 server.properties
-rw-rw-r--. 1 root root 1032 5月 14 2019 tools-log4j.properties
-rw-rw-r--. 1 root root 1169 5月 14 2019 trogdor.conf
-rw-rw-r--. 1 root root 1023 5月 14 2019 zookeeper.properties
[root@localhost config]# vi server.properties
# 创建/opt/kafka/kafka_2.12-2.2.1/logs目录
[root@localhost config]# cd ..
[root@localhost kafka_2.12-2.2.1]# mkdir logs
broker.id=0 表示broker的编号,如果集群中有多个broker,则每个broker的编号需要设置的不同
listeners=PLAINTEXT://:9092 brokder对外提供的服务入口地址
log.dirs=/opt/kafka/kafka_2.12-2.2.1/logs 设置存放消息日志文件的地址
zookeeper.connect=localhost:2181 Kafka所需Zookeeper集群地址
④ kafka的启动:
# 启动kafka服务端
[root@192 config]# cd ..
[root@192 kafka_2.12-2.2.1]# bin/kafka-server-start.sh config/server.properties
⑤ 重新开启一个终端,验证kafka有没有启动:
[root@localhost ~]# ps -ef|grep kafka
真实的集群是需要部署在不同的服务器上的,但是在我们测试时同时启动十几个虚拟机内存会吃不消,所以这里我们搭建伪集群,也就是把所有的服务都搭建在一台虚拟机上,用端口进行区分。
搭建一个三个节点的Zookeeper集群(伪集群)。
① 将解压后的zookeeper-3.4.14目录复制到以下三个目录:zookeeper-01、zookeeper-02、zookeeper-03
[root@localhost zookeeper]# cp -rf zookeeper-3.4.14 zookeeper-01
[root@localhost zookeeper]# cp -rf zookeeper-3.4.14 zookeeper-02
[root@localhost zookeeper]# cp -rf zookeeper-3.4.14 zookeeper-03
[root@localhost zookeeper]# ll
drwxr-xr-x. 15 root root 4096 11月 21 06:37 zookeeper-01
drwxr-xr-x. 15 root root 4096 11月 21 06:38 zookeeper-02
drwxr-xr-x. 15 root root 4096 11月 21 06:38 zookeeper-03
drwxr-xr-x. 15 2002 2002 4096 11月 20 21:59 zookeeper-3.4.14
-rw-r--r--. 1 root root 37676320 11月 20 21:50 zookeeper-3.4.14.tar.gz
② 配置每一个Zookeeper 的dataDir(zoo.cfg) clientPort 分别为2181 2182 2183:
[root@localhost zookeeper]# cd zookeeper-01
[root@localhost zookeeper-01]# cd conf/
[root@localhost conf]# vi zoo.cfg
dataDir=/opt/zookeeper/zookeeper-01/data
clientPort=2181
[root@localhost zookeeper]# cd zookeeper-02
[root@localhost zookeeper-02]# cd conf/
[root@localhost conf]# vi zoo.cfg
dataDir=/opt/zookeeper/zookeeper-02/data
clientPort=2182
[root@localhost zookeeper]# cd zookeeper-03
[root@localhost zookeeper-03]# cd conf/
[root@localhost conf]# vi zoo.cfg
dataDir=/opt/zookeeper/zookeeper-03/data
clientPort=2183
③ 在每个zookeeper的 data 目录下创建一个 myid 文件,内容分别是0、1、2 。这个文件就是记录每个服务器的ID
[root@localhost zookeeper]# cd zookeeper-01
[root@localhost zookeeper-01]# cd data/
[root@localhost data]# vi myid
0
root@localhost zookeeper]# cd zookeeper-02
[root@localhost zookeeper-02]# cd data/
[root@localhost data]# vi myid
1
[root@localhost zookeeper-03]# cd data/
[root@localhost data]# ll
drwxr-xr-x. 2 root root 32 11月 21 06:38 version-2
-rw-r--r--. 1 root root 5 11月 21 06:38 zookeeper_server.pid
[root@localhost data]# vi myid
2
④ 在每一个zookeeper 的 zoo.cfg配置集群服务器IP列表:
server.服务器ID=服务器IP地址:服务器之间通信端口:服务器之间投票选举端口
后面的两个端口在三个节点中只要不同即可,服务器ID就是myid文件中的值。
[root@localhost zookeeper-03]# cd conf/
-rw-r--r--. 1 root root 535 11月 21 06:38 configuration.xsl
-rw-r--r--. 1 root root 2161 11月 21 06:38 log4j.properties
-rw-r--r--. 1 root root 940 11月 21 06:45 zoo.cfg
[root@localhost conf]# vi zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/opt/zookeeper/zookeeper-03/data
clientPort=2183
# 添加集群服务器IP列表
server.0=127.0.0.1:2888:3888
server.1=127.0.0.1:2889:3889
server.2=127.0.0.1:2890:3890
[root@localhost zookeeper]# cd zookeeper-01
[root@localhost zookeeper-01]# cd conf/
[root@localhost conf]# vi zoo.cfg
# 添加集群服务器IP列表
server.0=127.0.0.1:2888:3888
server.1=127.0.0.1:2889:3889
server.2=127.0.0.1:2890:3890
[root@localhost zookeeper]# cd zookeeper-02
[root@localhost zookeeper-02]# cd conf/
[root@localhost conf]# vi zoo.cfg
# 添加集群服务器IP列表
server.0=127.0.0.1:2888:3888
server.1=127.0.0.1:2889:3889
server.2=127.0.0.1:2890:3890
⑤ 启动集群:启动集群就是分别启动每个实例,启动后我们查询一下每个实例的运行状态。
注意:必须三个服务同时启动,再查看状态,否则在查看状态时会报错 Error contacting service. It is probably not running.
# 启动服务zookeeper-01
[root@localhost zookeeper-01]# cd bin
[root@localhost bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/zookeeper-01/bin/../conf/zoo.cfg
Starting zookeeper ... already running as process 43813.
# 启动服务zookeeper-02
[root@localhost zookeeper-02]# cd bin
[root@localhost bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/zookeeper-02/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
# 启动服务zookeeper-03
[root@localhost zookeeper-03]# cd bin
[root@localhost bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/zookeeper-03/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
查看集群节点状态:
# follower节点
[root@localhost zookeeper]# cd zookeeper-01/bin
[root@localhost bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/zookeeper-01/bin/../conf/zoo.cfg
Mode: follower
# leader节点
[root@localhost bin]# cd ../..
[root@localhost zookeeper]# cd zookeeper-02/bin
[root@localhost bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/zookeeper-02/bin/../conf/zoo.cfg
Mode: leader
# follower节点
[root@localhost bin]# cd ../..
[root@localhost zookeeper]# cd zookeeper-03/bin
[root@localhost bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/zookeeper-03/bin/../conf/zoo.cfg
Mode: follower
① 将解压后的kafka_2.12-2.2.1目录复制到以下三个目录:kafka-01、kafka-02、kafka-03
[root@localhost bin]# cd /opt/kafka/
[root@localhost kafka]# ll
drwxr-xr-x. 7 root root 101 11月 20 22:16 kafka_2.12-2.2.1
-rw-r--r--. 1 root root 57054917 11月 20 22:09 kafka_2.12-2.2.1.tgz
[root@localhost kafka]# cp -rf kafka_2.12-2.2.1 kafka-01
[root@localhost kafka]# cp -rf kafka_2.12-2.2.1 kafka-02
[root@localhost kafka]# cp -rf kafka_2.12-2.2.1 kafka-03
[root@localhost kafka]# ll
drwxr-xr-x. 7 root root 101 11月 21 07:35 kafka-01
drwxr-xr-x. 7 root root 101 11月 21 07:35 kafka-02
drwxr-xr-x. 7 root root 101 11月 21 07:35 kafka-03
drwxr-xr-x. 7 root root 101 11月 20 22:16 kafka_2.12-2.2.1
-rw-r--r--. 1 root root 57054917 11月 20 22:09 kafka_2.12-2.2.1.tgz
② 修改每个节点的 server.properties 配置文件:
broker.id=0
# 允许外部端口连接
listeners=PLAINTEXT://192.168.38.22:9092
# 外部代理地址
advertised.listeners=PLAINTEXT://192.168.38.22:9092
host.name=localhost
port=9092
log.dirs=/opt/kafka/kafka-01/logs
zookeeper.connect=localhost:2181,localhost:2182,localhost:2183
broker.id=1
# 允许外部端口连接
listeners=PLAINTEXT://192.168.38.22:9093
# 外部代理地址
advertised.listeners=PLAINTEXT://192.168.38.22:9093
host.name=localhost
port=9093
log.dirs=/opt/kafka/kafka-02/logs
zookeeper.connect=localhost:2181,localhost:2182,localhost:2183
broker.id=2
# 允许外部端口连接
listeners=PLAINTEXT://192.168.38.22:9094
# 外部代理地址
advertised.listeners=PLAINTEXT://192.168.38.22:9094
host.name=localhost
port=9094
log.dirs=/opt/kafka/kafka-03/logs
zookeeper.connect=localhost:2181,localhost:2182,localhost:2183
③ 打开3个终端控制台启动每个kafka实例:
bin/kafka-server-start.sh config/server.properties
[root@localhost kafka-01]# bin/kafka-topics.sh --zookeeper localhost:2181 --create --topic test --partitions 3 --replication-factor 3
Created topic test.
[root@localhost kafka-01]# bin/kafka-topics.sh --zookeeper localhost:2181 --describe --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
[root@localhost kafka-01]#
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。