Zookeeper (一)集羣簡單搭建

  • 使用zookeeper-3.4.5版本爲例,進行集羣搭建的配置說明。

將conf目錄下的zoo-sample.cfg重命名成zoo.cfg, 進行以下修改配置(集羣包含zk01, zk02, zk03三個節點)html

[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:java

[parallels@zk01 data]$ less myid
1

其餘兩個節點zk02和zk03也如此配置。
進入每一個節點的bin目錄,依次啓動zookeeper節點:node

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

當節點未所有啓動成功時,查看節點狀態:apache

[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.

所有節點啓動完成後,查看節點狀態:app

[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。less

[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

此時登陸另外兩臺集羣節點查看數據,能看到數據已經同步:ide

[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]

當咱們在某一臺節點對數據進行了更新,數據會實時同步到另外的集羣節點中。ui

  • 集羣可靠性測試

當leader節點退出:code

[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.

集羣要想正常工做,存活節點的數量必須大於集羣節點數量的一半。

相關文章
相關標籤/搜索