ZooKeeper的伪分布式集群搭建以及真分布式集群搭建
zk集群的一些基本概念
zookeeper集群搭建:
- zk集群,主从节点,心跳机制(选举模式)
- 配置数据文件 myid 1/2/3 对应 server.1/2/3
- 通过 zkCli.sh -server [ip]:[port] 命令检测集群是否配置成功
文章图片
image.png 单机伪分布式搭建zookeeper集群
本节介绍单机伪分布式的zookeeper安装,官方下载地址如下:
https://archive.apache.org/dist/zookeeper/我这里使用的是3.4.11版本,所以找到相应的版本点击进去,复制到.tar.gz的下载链接到Linux上进行下载。命令如下:
[root@study-01 ~]# cd /usr/local/src/
[root@study-01 /usr/local/src]# wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11.tar.gz
下载完成之后将其解压到/usr/local/目录下:
[root@study-01 /usr/local/src]# tar -zxvf zookeeper-3.4.11.tar.gz -C /usr/local/
[root@study-01 /usr/local/src]# cd ../zookeeper-3.4.11/
[root@study-01 /usr/local/zookeeper-3.4.11]# ls
bindist-mavenlibREADME_packaging.txtzookeeper-3.4.11.jar.asc
build.xmldocsLICENSE.txtrecipeszookeeper-3.4.11.jar.md5
confivysettings.xmlNOTICE.txtsrczookeeper-3.4.11.jar.sha1
contribivy.xmlREADME.mdzookeeper-3.4.11.jar
[root@study-01 /usr/local/zookeeper-3.4.11]#
然后给目录重命名一下:
[root@study-01 ~]# cd /usr/local/
[root@study-01 /usr/local]# mv zookeeper-3.4.11/ zookeeper00
接着进行一系列的配置:
[root@study-01 /usr/local]# cd zookeeper00/
[root@study-01 /usr/local/zookeeper00]# cd conf/
[root@study-01 /usr/local/zookeeper00/conf]# cp zoo_sample.cfg zoo.cfg# 拷贝官方提供的模板配置文件
[root@study-01 /usr/local/zookeeper00/conf]# vim zoo.cfg# 增加或修改成如下内容
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper00/dataDir
dataLogDir=/usr/local/zookeeper00/dataLogDir
clientPort=2181
4lw.commands.whitelist=*
server.1=192.168.190.129:2888:3888# master节点,ip后面跟的是集群通信的端口
server.2=192.168.190.129:2889:3889
server.3=192.168.190.129:2890:3890
[root@study-01 /usr/local/zookeeper00/conf]# cd ../
[root@study-01 /usr/local/zookeeper00]# mkdir {dataDir,dataLogDir}
[root@study-01 /usr/local/zookeeper00]# cd dataDir/
[root@study-01 /usr/local/zookeeper00/dataDir]# vim myid# 配置该节点的id
1
[root@study-01 /usr/local/zookeeper00/dataDir]#
配置完之后,拷贝多个目录出来,因为是单机的伪分布式所以需要在一台机器上安装多个zookeeper:
[root@study-01 /usr/local]# cp zookeeper00 zookeeper01 -rf
[root@study-01 /usr/local]# cp zookeeper00 zookeeper02 -rf
配置 zookeeper01:
[root@study-01 /usr/local]# cd zookeeper01/conf/
[root@study-01 /usr/local/zookeeper01/conf]# vim zoo.cfg# 修改内容如下
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper01/dataDir
dataLogDir=/usr/local/zookeeper01/dataLogDir
clientPort=2182# 端口号必须要修改
4lw.commands.whitelist=*
server.1=192.168.190.129:2888:3888
server.2=192.168.190.129:2889:3889
server.3=192.168.190.129:2890:3890
[root@study-01 /usr/local/zookeeper01/conf]# cd ../dataDir/
[root@study-01 /usr/local/zookeeper01/dataDir]# vim myid
2
[root@study-01 /usr/local/zookeeper01/dataDir]#
配置 zookeeper02:
[root@study-01 /usr/local]# cd zookeeper02/conf/
[root@study-01 /usr/local/zookeeper02/conf]# vim zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper02/dataDir
dataLogDir=/usr/local/zookeeper02/dataLogDir
clientPort=2183# 端口号必须要修改
4lw.commands.whitelist=*
server.1=192.168.190.129:2888:3888
server.2=192.168.190.129:2889:3889
server.3=192.168.190.129:2890:3890
[root@study-01 /usr/local/zookeeper02/conf]# cd ../dataDir/
[root@study-01 /usr/local/zookeeper02/dataDir]# vim myid
3
[root@study-01 /usr/local/zookeeper02/dataDir]#
以上就在单机上配置好三个zookeeper集群节点了,现在我们来测试一下,这个伪分布式的zookeeper集群能否正常运作起来:
[root@study-01 ~]# cd /usr/local/zookeeper00/bin/
[root@study-01 /usr/local/zookeeper00/bin]# ./zkServer.sh start# 启动第一个节点
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper00/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@study-01 /usr/local/zookeeper00/bin]# netstat -lntp |grep java# 查看监听的端口
tcp600 192.168.190.129:3888:::*LISTEN3191/java# 集群通信的端口
tcp600 :::44793:::*LISTEN3191/java
tcp600 :::2181:::*LISTEN3191/java
[root@study-01 /usr/local/zookeeper00/bin]# cd ../../zookeeper01/bin/
[root@study-01 /usr/local/zookeeper01/bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper01/bin/../conf/zoo.cfg# 启动第二个节点
Starting zookeeper ... STARTED
[root@study-01 /usr/local/zookeeper01/bin]# cd ../../zookeeper02/bin/
[root@study-01 /usr/local/zookeeper02/bin]# ./zkServer.sh start# 启动第三个节点
[root@study-01 /usr/local/zookeeper02/bin]# netstat -lntp |grep java# 查看监听的端口
tcp600 192.168.190.129:2889:::*LISTEN3232/java
tcp600 :::48463:::*LISTEN3232/java
tcp600 192.168.190.129:3888:::*LISTEN3191/java
tcp600 192.168.190.129:3889:::*LISTEN3232/java
tcp600 192.168.190.129:3890:::*LISTEN3286/java
tcp600 :::44793:::*LISTEN3191/java
tcp600 :::60356:::*LISTEN3286/java
tcp600 :::2181:::*LISTEN3191/java
tcp600 :::2182:::*LISTEN3232/java
tcp600 :::2183:::*LISTEN3286/java
[root@study-01 /usr/local/zookeeper02/bin]# jps# 查看进程
3232 QuorumPeerMain
3286 QuorumPeerMain
3191 QuorumPeerMain
3497 Jps
[root@study-01 /usr/local/zookeeper02/bin]#
如上,可以看到,三个节点都正常启动成功了,接下来我们进入客户端,创建一些znode,看看是否会同步到集群中的其他节点上去:
[root@study-01 /usr/local/zookeeper02/bin]# ./zkCli.sh -server localhost:2181# 登录第一个节点的客户端
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] create /data test-data
Created /data
[zk: localhost:2181(CONNECTED) 2] ls /
[zookeeper, data]
[zk: localhost:2181(CONNECTED) 3] quit
[root@study-01 /usr/local/zookeeper02/bin]# ./zkCli.sh -server localhost:2182# 登录第二个节点的客户端
[zk: localhost:2182(CONNECTED) 0] ls /# 可以查看到我们在第一个节点上创建的znode,代表集群中的节点能够正常同步数据
[zookeeper, data]
[zk: localhost:2182(CONNECTED) 1] get /data# 数据也是一致的
test-data
cZxid = 0x100000002
ctime = Tue Apr 24 18:35:56 CST 2018
mZxid = 0x100000002
mtime = Tue Apr 24 18:35:56 CST 2018
pZxid = 0x100000002
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 0
[zk: localhost:2182(CONNECTED) 2] quit
[root@study-01 /usr/local/zookeeper02/bin]# ./zkCli.sh -server localhost:2183# 登录第三个节点的客户端
[zk: localhost:2183(CONNECTED) 0] ls /# 第三个节点也能查看到我们在第一个节点上创建的znode
[zookeeper, data]
[zk: localhost:2183(CONNECTED) 1] get /data# 数据也是一致的
test-data
cZxid = 0x100000002
ctime = Tue Apr 24 18:35:56 CST 2018
mZxid = 0x100000002
mtime = Tue Apr 24 18:35:56 CST 2018
pZxid = 0x100000002
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 0
[zk: localhost:2183(CONNECTED) 2] quit
[root@study-01 /usr/local/zookeeper02/bin]#
查看集群的状态、主从信息需要使用
./zkServer.sh status
命令,但是多个节点的话,逐个查看有些费劲,所以我们写一个简单的shell脚本来批量执行命令。如下:[root@study-01 ~]# vim checked.sh# 脚本内容如下
#!/bin/bash
/usr/local/zookeeper00/bin/zkServer.sh status
/usr/local/zookeeper01/bin/zkServer.sh status
/usr/local/zookeeper02/bin/zkServer.sh status
[root@study-01 ~]# sh ./checked.sh# 执行脚本
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper00/bin/../conf/zoo.cfg
Mode: follower# 从节点
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper01/bin/../conf/zoo.cfg
Mode: leader# 主节点
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper02/bin/../conf/zoo.cfg
Mode: follower
[root@study-01 ~]#
到此为止,我们就成功完成了单机zookeeper伪分布式集群的搭建,并且也测试成功了。
搭建zookeeper分布式集群
接下来,我们使用三台虚拟机来搭建zookeeper真实分布式集群,机器的ip地址如下:
- 192.168.190.128
- 192.168.190.129
- 192.168.190.130
首先配置一下系统的hosts文件:
[root@localhost ~]# vim /etc/hosts
192.168.190.128 zk000
192.168.190.129 zk001
192.168.190.130 zk002
把之前做伪分布式实验机器上的其他zookeeper目录删除掉,并把zookeeper目录使用rsync同步到其他机器上。如下:
[root@zk001 ~]# cd /usr/local/
[root@zk001 /usr/local]# rm -rf zookeeper01
[root@zk001 /usr/local]# rm -rf zookeeper02
[root@zk001 /usr/local]# mv zookeeper00/ zookeeper
[root@zk001 /usr/local]# rsync -av /usr/local/zookeeper/ 192.168.190.128:/usr/local/zookeeper/
[root@zk001 /usr/local]# rsync -av /usr/local/zookeeper/ 192.168.190.130:/usr/local/zookeeper/
然后逐个在三台机器上都配置一下环境变量,如下:
[root@zk001 ~]# vim .bash_profile# 增加如下内容
export ZOOKEEPER_HOME=/usr/local/zookeeper
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin:$ZOOKEEPER_HOME/bin
export PATH
[root@zk001 ~]# source .bash_profile
逐个修改配置文件,zk000:
[root@zk000 ~]# cd /usr/local/zookeeper/conf/
[root@zk000 /usr/local/zookeeper/conf]# vim zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper/dataDir
dataLogDir=/usr/local/zookeeper/dataLogDir
clientPort=2181
4lw.commands.whitelist=*
server.1=192.168.190.128:2888:3888# 默认server.1为master节点
server.2=192.168.190.129:2888:3888
server.3=192.168.190.130:2888:3888
[root@zk000 /usr/local/zookeeper/conf]# cd ../dataDir/
[root@zk000 /usr/local/zookeeper/dataDir]# vim myid
1
[root@zk000 /usr/local/zookeeper/dataDir]#
zk001:
[root@zk001 ~]# cd /usr/local/zookeeper/conf/
[root@zk001 /usr/local/zookeeper/conf]# vim zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper/dataDir
dataLogDir=/usr/local/zookeeper/dataLogDir
clientPort=2181
4lw.commands.whitelist=*
server.1=192.168.190.128:2888:3888# 默认server.1为master节点
server.2=192.168.190.129:2888:3888
server.3=192.168.190.130:2888:3888
[root@zk001 /usr/local/zookeeper/conf]# cd ../dataDir/
[root@zk001 /usr/local/zookeeper/dataDir]# vim myid
2
[root@zk001 /usr/local/zookeeper/dataDir]#
zk002:
[root@zk002 ~]# cd /usr/local/zookeeper/conf/
[root@zk002 /usr/local/zookeeper/conf]# vim zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper/dataDir
dataLogDir=/usr/local/zookeeper/dataLogDir
clientPort=2181
4lw.commands.whitelist=*
server.1=192.168.190.128:2888:3888# 默认server.1为master节点
server.2=192.168.190.129:2888:3888
server.3=192.168.190.130:2888:3888
[root@zk002 /usr/local/zookeeper/conf]# cd ../dataDir/
[root@zk002 /usr/local/zookeeper/dataDir]# vim myid
3
[root@zk002 /usr/local/zookeeper/dataDir]#
配置完成之后,启动三台机器的zookeeper服务:
[root@zk000 ~]# zkServer.sh start
[root@zk001 ~]# zkServer.sh start
[root@zk002 ~]# zkServer.sh start
启动成功后,查看三个机器的集群状态信息:
[root@zk000 ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: leader
[root@zk000 ~]#
[root@zk001 ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: follower
[root@zk001 ~]#
[root@zk002 ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: follower
[root@zk002 ~]#
然后我们来测试创建znode是否会同步,进入192.168.190.128机器的客户端:
[root@zk000 ~]# zkCli.sh -server 192.168.190.128:2181
[zk: 192.168.190.128:2181(CONNECTED) 0] ls /
[zookeeper, data]
[zk: 192.168.190.128:2181(CONNECTED) 1] create /real-culster real-data
Created /real-culster
[zk: 192.168.190.128:2181(CONNECTED) 2] ls /
[zookeeper, data, real-culster]
[zk: 192.168.190.128:2181(CONNECTED) 3] get /real-culster
real-data
cZxid = 0x300000002
ctime = Tue Apr 24 20:48:32 CST 2018
mZxid = 0x300000002
mtime = Tue Apr 24 20:48:32 CST 2018
pZxid = 0x300000002
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 0
[zk: 192.168.190.128:2181(CONNECTED) 4] quit
进入192.168.190.129机器的客户端
[root@zk000 ~]# zkCli.sh -server 192.168.190.129:2181
[zk: 192.168.190.129:2181(CONNECTED) 0] ls /
[zookeeper, data, real-culster]
[zk: 192.168.190.129:2181(CONNECTED) 1] get /real-culster
real-data
cZxid = 0x300000002
ctime = Tue Apr 24 20:48:32 CST 2018
mZxid = 0x300000002
mtime = Tue Apr 24 20:48:32 CST 2018
pZxid = 0x300000002
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 0
[zk: 192.168.190.129:2181(CONNECTED) 2] quit
进入192.168.190.130机器的客户端
[root@zk000 ~]# zkCli.sh -server 192.168.190.130:2181
[zk: 192.168.190.130:2181(CONNECTED) 0] ls /
[zookeeper, data, real-culster]
[zk: 192.168.190.130:2181(CONNECTED) 1] get /real-culster
real-data
cZxid = 0x300000002
ctime = Tue Apr 24 20:48:32 CST 2018
mZxid = 0x300000002
mtime = Tue Apr 24 20:48:32 CST 2018
pZxid = 0x300000002
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 0
[zk: 192.168.190.130:2181(CONNECTED) 2] quit
从以上的测试可以看到,在zookeeper分布式集群中,我们在任意一个节点创建的znode都会被同步的集群中的其他节点上,数据也会被一并同步。所以到此为止,我们的zookeeper分布式集群就搭建成功了。
测试集群角色以及选举
以上我们只是测试了znode的同步,还没有测试集群中的节点选举,所以本节就来测试一下,当master节点挂掉之后看看slave节点会不会通过选举坐上master的位置。首先我们来把master节点的zookeeper服务给停掉:
[root@zk001 ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: leader
[root@zk001 ~]# zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[root@zk001 ~]#
这时到其他两台机器上进行查看,可以看到有一台已经成为master节点了:
[root@zk002 ~]# zkServer.sh status# 可以看到zk002这个节点成为了master节点
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: leader
[root@zk002 ~]#
[root@zk000 ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: follower
[root@zk000 ~]#
然后再把停掉的节点启动起来,可以看到,该节点重新加入了集群,但是此时是以slave角色存在,而不会以master角色存在:
[root@zk001 ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@zk001 ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: follower
[root@zk001 ~]#
【ZooKeeper的伪分布式集群搭建以及真分布式集群搭建】可以看到,zk002这个节点依旧是master角色,不会被取代,所以只有在选举的时候集群中的节点才会切换角色:
[root@zk002 ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: leader
[root@zk002 ~]#
推荐阅读
- 热闹中的孤独
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 放屁有这三个特征的,请注意啦!这说明你的身体毒素太多
- 一个人的旅行,三亚
- 布丽吉特,人生绝对的赢家
- 慢慢的美丽
- 尽力
- 一个小故事,我的思考。
- 家乡的那条小河
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量