规划三个节点,分别为:bigdata01、bigdata02、bigdata03
tar -zxvf apache-zookeeper-3.7.0-bin.tar.gz
cd apache-zookeeper-3.7.0-bin/conf/
mv zoo_sample.cfg zoo.cfg
修改zoo.cfg中的dataDir参数的值,dataDir指向的目录存储的是zookeeper的核心数据,所以这个目录不能使用tmp目录,然后增加server.0、server.1、server.2这三行内容;
vi zoo.cfg
编辑之后的内容如下:
dataDir=/data/software/apache-zookeeper-3.7.0-bin/data
server.0=bigdata01:2888:3888
server.1=bigdata02:2888:3888
server.2=bigdata03:2888:3888
创建目录保存myid文件,并且向myid文件写入内容
myid中的值其实是zoo.cfg中server后面指定的编号一一对应的
编号0对应的是bigdata01这台机器,所以在这里指定0
在这里使用echo 和 重定向 实现数据写入
[root@BigData01 conf]# cd /data/software/apache-zookeeper-3.7.0-bin
[root@BigData01 apache-zookeeper-3.7.0-bin]# mkdir data
[root@BigData01 apache-zookeeper-3.7.0-bin]# cd data
[root@BigData01 data]# echo 0 > myid
[root@BigData01 software]# scp -rq apache-zookeeper-3.7.0-bin bigdata02:/data/software/
[root@BigData01 software]# scp -rq apache-zookeeper-3.7.0-bin bigdata03:/data/software/
首先修改bigdata02上的
[root@BigData02 ~]# cd /data/software/apache-zookeeper-3.7.0-bin/data/
You have new mail in /var/spool/mail/root
[root@BigData02 data]# ll
total 4
-rw-r--r--. 1 root root 2 Apr 8 22:49 myid
[root@BigData02 data]# echo 1 > myid
然后修改bigdata03上的
[root@BigData03 ~]# cd /data/software/apache-zookeeper-3.7.0-bin/data/
You have new mail in /var/spool/mail/root
[root@BigData03 data]# ll
total 4
-rw-r--r--. 1 root root 2 Apr 8 22:49 myid
[root@BigData03 data]# echo 2 > myid
分别在bigdata01、bigdata02和bigdata03上启动zookeeper进程
在bigdata01上启动
[root@BigData01 software]# cd /data/software/apache-zookeeper-3.7.0-bin
[root@BigData01 apache-zookeeper-3.7.0-bin]# bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /data/software/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
在bigdata02上启动
[root@BigData02 data]# cd /data/software/apache-zookeeper-3.7.0-bin/
[root@BigData02 apache-zookeeper-3.7.0-bin]# bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /data/software/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
在bigdata03上启动
[root@BigData03 data]# cd /data/software/apache-zookeeper-3.7.0-bin/
You have new mail in /var/spool/mail/root
[root@BigData03 apache-zookeeper-3.7.0-bin]# bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /data/software/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
分别在bigdata01、bigdata02和bigdata03上执行jps命令验证是否有QuorumPeerMain进程;
以bigdata01为例:
[root@BigData01 apache-zookeeper-3.7.0-bin]# jps
1840 QuorumPeerMain
1903 Jps
bigdata01:
[root@BigData01 apache-zookeeper-3.7.0-bin]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /data/software/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
bigdata02:
[root@BigData02 apache-zookeeper-3.7.0-bin]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /data/software/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader
bigdata03:
[root@BigData03 apache-zookeeper-3.7.0-bin]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /data/software/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
首先使用zookeeper的客户端工具连接到zookeeper里面,使用bin目录下面的zkCli.sh脚本,默认会连接本机的zookeeper服务:
[root@BigData01 apache-zookeeper-3.7.0-bin]# bin/zkCli.sh
进入命令行,随便输入任意字母,然后会显示zookeeper支持的所有命令:
[zk: localhost:2181(CONNECTED) 0]
[zk: localhost:2181(CONNECTED) 0] abc
ZooKeeper -server host:port -client-configuration properties-file cmd args
addWatch [-m mode] path # optional mode is one of [PERSISTENT, PERSISTENT_RECURSIVE] - default is PERSISTENT_RECURSIVE
addauth scheme auth
close
config [-c] [-w] [-s]
connect host:port
create [-s] [-e] [-c] [-t ttl] path [data] [acl]
delete [-v version] path
deleteall path [-b batch size]
delquota [-n|-b|-N|-B] path
get [-s] [-w] path
getAcl [-s] path
getAllChildrenNumber path
getEphemerals path
history
listquota path
ls [-s] [-w] [-R] path
printwatches on|off
quit
reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*]
redo cmdno
removewatches path [-c|-d|-a] [-l]
set [-s] [-v version] path data
setAcl [-s] [-v version] [-R] path acl
setquota -n|-b|-N|-B val path
stat [-w] path
sync path
version
whoami
Command not found: Command not found abc
[zk: localhost:2181(CONNECTED) 1]
这样就进入了zookeeper命令行
在这里可以操作zookeeper中的目录结构
zookeeper中的目录结构和Linux文件系统的目录结构类似
zookeeper里面的每一个目录,我们都称为节点(ZNode)
正常情况下,我们可以把Znode认为和文件系统中的目录类似,但是有一点需要注意:ZNode节点本身是可以存储数据的;
[zk: localhost:2181(CONNECTED) 1] ls /
[admin, brokers, cluster, config, consumers, controller, controller_epoch, feature, isr_change_notification, kafka-manager, latest_producer_id_block, log_dir_event_notification, zookeeper]
在跟节点下面创建一个test节点,在test节点上存储数据hello_zookeeper
[zk: localhost:2181(CONNECTED) 2] create /test hello_zookeeper
Created /test
[zk: localhost:2181(CONNECTED) 3] ls /
[admin, brokers, cluster, config, consumers, controller, controller_epoch, feature, isr_change_notification, kafka-manager, latest_producer_id_block, log_dir_event_notification, test, zookeeper]
获取test节点中的信息
[zk: localhost:2181(CONNECTED) 4] get /test
hello_zookeeper
这个命令可以递归删除,这里面还有一个delete命令,也可以删除节点,但是只能删除空节点,如果下面还有子节点,想一次性删除全部节点建议使用deleteall
[zk: localhost:2181(CONNECTED) 5] deleteall /test
[zk: localhost:2181(CONNECTED) 6] ls /
[admin, brokers, cluster, config, consumers, controller, controller_epoch, feature, isr_change_notification, kafka-manager, latest_producer_id_block, log_dir_event_notification, zookeeper]
kafka集群中的broker节点启动之后,会自动向zookeeper中注册,保存当前节点信息,可以通过get命令查看节点信息,这里面会显示对应的主机名和端口号等信息;
[zk: localhost:2181(CONNECTED) 0] ls /brokers
[ids, seqid, topics]
[zk: localhost:2181(CONNECTED) 1] ls /brokers/ids
[0, 2]
[zk: localhost:2181(CONNECTED) 2] get /brokers/ids/2
{"listener_security_protocol_map":{"PLAINTEXT":"PLAINTEXT"},"endpoints":["PLAINTEXT://BigData03:9092"],"jmx_port":-1,"features":{},"host":"BigData03","timestamp":"1618707961757","port":9092,"version":5}
在bigdata01、bigdata02和bigdata03上分别执行如下命令:
bin/zkServer.sh stop
注意:
(1)部署Kafka集群之前,需要确保zookeeper集群是启动状态;
(2)Kafka还需要依赖基础环境jdk,需要确保jdk已经安装到位;
规划三个节点,具体如下所示:
bigdata01
bigdata02
bigdata03
注意:Kafka集群,是没有主从之分,所有节点都是一样的;
解压安装包
[root@BigData01 ~]# cd /data/software/
[root@BigData01 software]# tar -zxvf kafka_2.12-2.7.0.tgz
修改配置文件
集群模式要修改 broker.id、log.dirs 以及 zookeeper.connect ;
[root@BigData01 software]# cd kafka_2.12-2.7.0/config/
[root@BigData01 config]# vi server.properties
增加如下内容:
broker.id=0
lod.dirs=/data/kafka-logs
zookeeper.connect=bigdata01:2181,bigdata02:2181,bigdata03:2181
[root@BigData01 config]# cd /data/software/
[root@BigData01 software]# scp -rq kafka_2.12-2.7.0 bigdata02:/data/software/
[root@BigData01 software]# scp -rq kafka_2.12-2.7.0 bigdata03:/data/software/
首先将bigdata02节点上的broker.id的值为1;
[root@BigData02 apache-zookeeper-3.7.0-bin]# cd /data/software/kafka_2.12-2.7.0/config/
[root@BigData02 config]# vi server.properties
具体如下所示:
broker.id=1
lod.dirs=/data/kafka-logs
zookeeper.connect=bigdata01:2181,bigdata02:2181,bigdata03:2181
然后修改bigdata03节点上的broker.id的值为2;
[root@BigData03 apache-zookeeper-3.7.0-bin]# cd /data/software/kafka_2.12-2.7.0/config/
[root@BigData03 config]# vi server.properties
具体修改如下所示:
broker.id=2
lod.dirs=/data/kafka-logs
zookeeper.connect=bigdata01:2181,bigdata02:2181,bigdata03:2181
分别在三个节点上启动Kafka进程
在bigdata01上启动
[root@BigData01 software]# cd /data/software/kafka_2.12-2.7.0
[root@BigData01 kafka_2.12-2.7.0]# bin/kafka-server-start.sh -daemon config/server.properties
在bigdata02上启动
[root@BigData02 config]# cd /data/software/kafka_2.12-2.7.0
[root@BigData02 kafka_2.12-2.7.0]# bin/kafka-server-start.sh -daemon config/server.properties
在bigdata03上启动
[root@BigData03 config]# cd /data/software/kafka_2.12-2.7.0
[root@BigData03 kafka_2.12-2.7.0]# bin/kafka-server-start.sh -daemon config/server.properties
分别在bigdata01、bigdata02和bigdata03节点上执行jps命令验证是否有Kafka进程,若有说明启动正常了; 以bigdata01为例:
[root@BigData01 kafka_2.12-2.7.0]# jps
1840 QuorumPeerMain
2845 Kafka
2941 Jps
在Github上搜索“CMAK”,CMAK是雅虎开源的一款Kafka集群管理工具。通过该工具,用户可以在Web界面操作Kafka集群,可以轻松检查集群状态(Topic、Consumer、Offset、Brokers、Replica、Partition)等。
由于CMAK-3.0.0.5.zip版本是在Java11上编译的,所以运行的时候也需要使用Java11这个版本,我们目前服务器上使用的是Java8这个版本;
我们可以通过额外安装一个JDK11的方式解决;
只需要解压即可,不需要配置环境变量,因为只有这个CMAK需要使用JDK11
上传JDK和CMAK至bigdata01的/data/software目录下,并解压
[root@BigData01 software]# tar -zxvf jdk-11.0.10_linux-x64_bin.tar.gz
解压CMAK-3.0.0.5.zip文件
[root@BigData01 software]# unzip cmak-3.0.0.5.zip
-bash: unzip: command not found
出现-bash: unzip: command not found问题,我们使用yum命令安装unzip工具
先清空一下yum缓存,否则使用yum可能无法安装unzip
[root@BigData01 software]# yum clean all
Loaded plugins: fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
Cleaning repos: base extras updates
Cleaning up list of fastest mirrors
You have new mail in /var/spool/mail/root
再安装unzip
[root@BigData01 software]# yum install -y unzip
Loaded plugins: fastestmirror
Determining fastest mirrors
....
Running transaction
Installing : unzip-6.0-21.el7.x86_64 1/1
Verifying : unzip-6.0-21.el7.x86_64 1/1
Installed:
unzip.x86_64 0:6.0-21.el7 Complete!
再重新解压CMAK-3.0.0.5.zip压缩包
[root@BigData01 software]# unzip cmak-3.0.0.5.zip
首先修改bin目录下的cmak脚本
在里面配置JAVA_HOME指向jdk11的安装目录,否则默认会使用jdk8;
[root@BigData01 software]# cd cmak-3.0.0.5/bin/
[root@BigData01 bin]# vi cmak
增加如下配置:
JAVA_HOME=/data/software/jdk-11.0.10
然后修改conf目录下的application.conf文件
只需要在里面增加一行cmak.zkhosts参数的配置即可,指定zookeeper的地址
注意:在这里指定zookeeper地址主要是为了让CMAK在里面保存数据,这个zookeeper地址不一定是Kafka集群中使用的那个zookeeper集群,随便指定哪个zookeeper集群都可以。
[root@BigData01 bin]# cd /data/software/cmak-3.0.0.5/conf/
[root@BigData01 conf]# vi application.conf
修改cmak.zkhosts配置,注意cmak.zkhosts已经有默认值,把默认值改为如下值:
cmak.zkhosts="bigdata01:2181,bigdata02:2181,bigdata03:2181"
想要在CMAK中查看Kafka的一些指标信息,在启动Kakfa的时候需要指定JXM_PORT
先停止三台机器上的Kafka集群
bigdata01:
[root@BigData01 conf]# cd /data/software/kafka_2.12-2.7.0
You have new mail in /var/spool/mail/root
[root@BigData01 kafka_2.12-2.7.0]# bin/kafka-server-stop.sh
bigdata02:
[root@BigData02 ~]# cd /data/software/kafka_2.12-2.7.0/
You have new mail in /var/spool/mail/root
[root@BigData02 kafka_2.12-2.7.0]# bin/kafka-server-stop.sh
bigdata03:
[root@BigData03 ~]# cd /data/software/kafka_2.12-2.7.0/
You have new mail in /var/spool/mail/root
[root@BigData03 kafka_2.12-2.7.0]# bin/kafka-server-stop.sh
重新启动Kafka集群,指定JXM_PORT;
bigdata01:
[root@BigData01 kafka_2.12-2.7.0]# JMX_PORT=9988 bin/kafka-server-start.sh -daemon config/server.properties
bigdata02:
[root@BigData02 kafka_2.12-2.7.0]# JMX_PORT=9988 bin/kafka-server-start.sh -daemon config/server.properties
bigdata03:
[root@BigData03 kafka_2.12-2.7.0]# JMX_PORT=9988 bin/kafka-server-start.sh -daemon config/server.properties
[root@BigData01 cmak-3.0.0.5]# bin/cmak -Dconfig.file=conf/application.conf -Dhttp.port=9001
如果想把CMAK放在后台执行的话需要添加上nohup和&
nohup bin/cmak -Dconfig.file=conf/application.conf -Dhttp.port=9001 &