Zookeeper (一)集群简单搭建

  • 使用zookeeper-3.4.5版本为例,进行集群搭建的配置说明。

将conf目录下的zoo-sample.cfg重命名成zoo.cfg, 进行如下修改配置(集群包含zk01, zk02, zk03三个节点)

[parallels@zk01 conf]$ less zoo.cfg
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=/home/parallels/app/zookeeper-3.4.5/data
# the port at which the clients will connect
clientPort=2181
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.1=zk01:2888:3888
server.2=zk02:2888:3888
server.3=zk03:2888:3888

然后根据配置在zookeeper-3.4.5目录下创建目录data,创建名为myid的文件,内容为配置文件中锁配置的server.id:

[parallels@zk01 data]$ less myid
1

其他两个节点zk02和zk03也如此配置。
进入每个节点的bin目录,依次启动zookeeper节点:

[parallels@zk01 bin]$ ./zkServer.sh start
JMX enabled by default
Using config: /home/parallels/app/zookeeper-3.4.5/bin/../conf/zoo.cfg

当节点未全部启动成功时,查看节点状态:

[parallels@zk01 bin]$ ./zkServer.sh status
JMX enabled by default
Using config: /home/parallels/app/zookeeper-3.4.5/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.

全部节点启动完成后,查看节点状态:

[parallels@zk01 bin]$ ./zkServer.sh status
JMX enabled by default
Using config: /home/parallels/app/zookeeper-3.4.5/bin/../conf/zoo.cfg
Mode: follower
[parallels@zk02 bin]$ ./zkServer.sh status
JMX enabled by default
Using config: /home/parallels/app/zookeeper-3.4.5/bin/../conf/zoo.cfg
Mode: leader
[parallels@zk03 bin]$ ./zkServer.sh status
JMX enabled by default
Using config: /home/parallels/app/zookeeper-3.4.5/bin/../conf/zoo.cfg
Mode: follower
  • 连接集群,发送数据

zookeeper管理客户所存放的数据采用的是类似于文件树的结构,每个节点叫做一个node。

[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] create zkData 1000
Command failed: java.lang.IllegalArgumentException: Path must start with / character
[zk: localhost:2181(CONNECTED) 2] create /zkData 1000
Created /zkData
[zk: localhost:2181(CONNECTED) 3] ls /               
[zkData, zookeeper]
[zk: localhost:2181(CONNECTED) 4] get /zkData
1000
cZxid = 0x100000004
ctime = Sun Sep 30 09:43:11 CST 2018
mZxid = 0x100000004
mtime = Sun Sep 30 09:43:11 CST 2018
pZxid = 0x100000004
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0

此时登录另外两台集群节点查看数据,能看到数据已经同步:

[zk: localhost:2181(CONNECTED) 1] ls /
[zkData, zookeeper]
[zk: localhost:2181(CONNECTED) 2] get /zkData
1005
cZxid = 0x100000004
ctime = Sun Sep 30 09:43:11 CST 2018
mZxid = 0x100000005
mtime = Sun Sep 30 09:45:26 CST 2018
pZxid = 0x100000004
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0

其他数据操作命令:

[zk: localhost:2181(CONNECTED) 6] set /zkData 1005
cZxid = 0x100000004
ctime = Sun Sep 30 09:43:11 CST 2018
mZxid = 0x100000005
mtime = Sun Sep 30 09:45:26 CST 2018
pZxid = 0x100000004
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
[zk: localhost:2181(CONNECTED) 7] get /zkData     
1005
cZxid = 0x100000004
ctime = Sun Sep 30 09:43:11 CST 2018
mZxid = 0x100000005
mtime = Sun Sep 30 09:45:26 CST 2018
pZxid = 0x100000004
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
[zk: localhost:2181(CONNECTED) 8]

当我们在某一台节点对数据进行了更新,数据会实时同步到另外的集群节点中。

  • 集群可靠性测试

当leader节点退出:

[parallels@zk02 bin]$ ./zkServer.sh status
JMX enabled by default
Using config: /home/parallels/app/zookeeper-3.4.5/bin/../conf/zoo.cfg
Mode: leader
[parallels@zk02 bin]$ jps
5176 Jps
31172 QuorumPeerMain
[parallels@zk02 bin]$ kill -9 31172

我们可以查看到其他节点被选举成为leader:

[parallels@zk03 bin]$ ./zkServer.sh status
JMX enabled by default
Using config: /home/parallels/app/zookeeper-3.4.5/bin/../conf/zoo.cfg
Mode: leader

但是如果集群中三个节点中的两个都退出(zk02, zk03),则集群将无法正常工作:

[parallels@zk01 bin]$ ./zkServer.sh status
JMX enabled by default
Using config: /home/parallels/app/zookeeper-3.4.5/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.

集群要想正常工作,存活节点的数量必须大于集群节点数量的一半。