Zookeeper的shell操作
本章节将分享一些zookeeper客服端的一些命令,实验操作有助于理解zookeeper的数据结构。
Zookeeper命令工具
在前一章的基础上,在启动Zookeeper服务之后,输入以下命令,连接到Zookeeper服务。连接成功之后,系统会输出Zookeeper的相关环境及配置信息,并在屏幕输出“welcome to Zookeeper!”等信息,倒数几行的位置,如下:
[root@k8s-m1 bin]# zkCli.sh -server k8s-m1:2181
Connecting to k8s-m1:2181
2023-08-31 16:36:48,125 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.12-e5259e437540f349646870ea94dc2658c4e44b3b, built on 03/27/2018 03:55 GMT
2023-08-31 16:36:48,129 [myid:] - INFO [main:Environment@100] - Client environment:host.name=k8s-m1
2023-08-31 16:36:48,130 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_65
2023-08-31 16:36:48,132 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2023-08-31 16:36:48,133 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/opt/jdk1.8.0_65/jre
2023-08-31 16:36:48,133 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/opt/zookeeper-3.4.12/bin/../build/classes:/opt/zookeeper-3.4.12/bin/../build/lib/*.jar:/opt/zookeeper-3.4.12/bin/../lib/slf4j-log4j12-1.7.25.jar:/opt/zookeeper-3.4.12/bin/../lib/slf4j-api-1.7.25.jar:/opt/zookeeper-3.4.12/bin/../lib/netty-3.10.6.Final.jar:/opt/zookeeper-3.4.12/bin/../lib/log4j-1.2.17.jar:/opt/zookeeper-3.4.12/bin/../lib/jline-0.9.94.jar:/opt/zookeeper-3.4.12/bin/../lib/audience-annotations-0.5.0.jar:/opt/zookeeper-3.4.12/bin/../zookeeper-3.4.12.jar:/opt/zookeeper-3.4.12/bin/../src/java/lib/*.jar:/opt/zookeeper-3.4.12/bin/../conf:.:/opt/jdk1.8.0_65/lib/dt.jar:/opt/jdk1.8.0_65/lib/tools.jar
2023-08-31 16:36:48,133 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2023-08-31 16:36:48,133 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2023-08-31 16:36:48,133 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2023-08-31 16:36:48,133 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2023-08-31 16:36:48,133 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2023-08-31 16:36:48,133 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-957.el7.x86_64
2023-08-31 16:36:48,133 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2023-08-31 16:36:48,133 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2023-08-31 16:36:48,134 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/opt/apache-zookeeper-3.7.1-bin/bin
2023-08-31 16:36:48,135 [myid:] - INFO [main:ZooKeeper@441] - Initiating client connection, connectString=k8s-m1:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@5c29bfd
Welcome to ZooKeeper!
2023-08-31 16:36:48,166 [myid:] - INFO [main-SendThread(k8s-m1:2181):ClientCnxn$SendThread@1028] - Opening socket connection to server k8s-m1/192.168.2.140:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2023-08-31 16:36:48,241 [myid:] - INFO [main-SendThread(k8s-m1:2181):ClientCnxn$SendThread@878] - Socket connection established to k8s-m1/192.168.2.140:2181, initiating session
[zk: k8s-m1:2181(CONNECTING) 0] 2023-08-31 16:36:48,314 [myid:] - INFO [main-SendThread(k8s-m1:2181):ClientCnxn$SendThread@1302] - Session establishment complete on server k8s-m1/192.168.2.140:2181, sessionid = 0x1002552fac20000, negotiated timeout = 30000WATCHER::WatchedEvent state:SyncConnected type:None path:null[zk: k8s-m1:2181(CONNECTED) 0]
输入help之后,屏幕会输出可用的Zookeeper命令,如下所示
[zk: k8s-m1:2181(CONNECTED) 0]
[zk: k8s-m1:2181(CONNECTED) 0] help
ZooKeeper -server host:port cmd argsstat path [watch]set path data [version]ls path [watch]delquota [-n|-b] pathls2 path [watch]setAcl path aclsetquota -n|-b val pathhistory redo cmdnoprintwatches on|offdelete path [version]sync pathlistquota pathrmr pathget path [watch]create [-s] [-e] path data acladdauth scheme authquit getAcl pathclose connect host:port
使用Zookeeper命令的简单操作
(1)使用ls命令查看当前Zookeeper中所包含的内容:ls /
[zk: k8s-m1:2181(CONNECTED) 1] ls /
[zookeeper]
(2)创建一个新的Znode节点"aa",以及和它相关字符,执行命令:create /aa “first zookeeper”,默认是不带编号的
[zk: k8s-m1:2181(CONNECTED) 2] create /aa "first zookeeper"
Created /aa
创建带编号的持久性节点"bb",
[zk: k8s-m1:2181(CONNECTED) 3] create -s /bb "bb"
Created /bb0000000006
创建不带编号的临时节点"cc"
[zk: k8s-m1:2181(CONNECTED) 4] create -e /cc "cc"
Created /cc
创建带编号的临时节点"dd"
[zk: k8s-m1:2181(CONNECTED) 5] create -s -e /dd "dd"
Created /dd0000000008
(3) 再次使用ls命令来查看现在Zookeeper的中所包含的内容:ls /
[zk: k8s-m1:2181(CONNECTED) 6] ls /
[aa, bb0000000006, cc, dd0000000008, zookeeper]
此时看到,上面的四个节点aa/bb/cc/dd都已经被创建。
关闭本次连接回话session,并直接使用connect重新打开一个连接。
[zk: k8s-m1:2181(CONNECTED) 7] close
2023-08-31 20:17:54,078 [myid:] - INFO [main:ZooKeeper@687] - Session: 0x1002552fac20002 closed
[zk: k8s-m1:2181(CLOSED) 8] 2023-08-31 20:17:54,084 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@521] - EventThread shut down for session: 0x1002552fac20002[zk: k8s-m1:2181(CLOSED) 8] ls /
Not connected
[zk: k8s-m1:2181(CLOSED) 9] connect k8s-m1:2181
2023-08-31 20:18:22,837 [myid:] - INFO [main:ZooKeeper@441] - Initiating client connection, connectString=k8s-m1:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@5680a178
[zk: k8s-m1:2181(CONNECTING) 10] 2023-08-31 20:18:22,841 [myid:] - INFO [main-SendThread(k8s-m1:2181):ClientCnxn$SendThread@1028] - Opening socket connection to server k8s-m1/192.168.2.140:2181. Will not attempt to authenticate using SASL (unknown error)
2023-08-31 20:18:22,842 [myid:] - INFO [main-SendThread(k8s-m1:2181):ClientCnxn$SendThread@878] - Socket connection established to k8s-m1/192.168.2.140:2181, initiating session
2023-08-31 20:18:22,863 [myid:] - INFO [main-SendThread(k8s-m1:2181):ClientCnxn$SendThread@1302] - Session establishment complete on server k8s-m1/192.168.2.140:2181, sessionid = 0x1002552fac20003, negotiated timeout = 30000WATCHER::WatchedEvent state:SyncConnected type:None path:null
重新查看,临时节点已经随着上一次的会话关闭自动删除了
[zk: k8s-m1:2181(CONNECTED) 10] ls /
[aa, bb0000000006, zookeeper]
[zk: k8s-m1:2181(CONNECTED) 11]
(4)使用get命令来确认第二步中所创建的Znode是否包含我们创建的字符串,执行命令:get /aa
[zk: k8s-m1:2181(CONNECTED) 11] get /aa
first zookeeper
cZxid = 0x20000000e
ctime = Thu Aug 31 19:53:59 CST 2023
mZxid = 0x20000000e
mtime = Thu Aug 31 19:53:59 CST 2023
pZxid = 0x20000000e
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 15
numChildren = 0
[zk: k8s-m1:2181(CONNECTED) 12]
(5)接下来通过set命令来对zk所关联的字符串进行设置,执行命令:set /aa haha123
[zk: k8s-m1:2181(CONNECTED) 12] set /aa hello
cZxid = 0x20000000e
ctime = Thu Aug 31 19:53:59 CST 2023
mZxid = 0x200000014
mtime = Thu Aug 31 20:20:39 CST 2023
pZxid = 0x20000000e
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0
[zk: k8s-m1:2181(CONNECTED) 13]
(6)再次使用get命令来查看,上次修改的内容,执行命令:get /aa
[zk: k8s-m1:2181(CONNECTED) 13] get /aa
hello
cZxid = 0x20000000e
ctime = Thu Aug 31 19:53:59 CST 2023
mZxid = 0x200000014
mtime = Thu Aug 31 20:20:39 CST 2023
pZxid = 0x20000000e
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0
(7)下面我们将刚才创建的Znode删除,执行命令:delete /aa
[zk: k8s-m1:2181(CONNECTED) 14] delete /aa
(8) 最后再次使用ls命令查看Zookeeper中的内容,执行命令:ls /
[zk: k8s-m1:2181(CONNECTED) 15] ls /
[bb0000000006, zookeeper]
经过验证,zk节点aa已经删除。
(9) 退出,执行命令:quit
[zk: k8s-m1:2181(CONNECTED) 17] quit
Quitting...
2023-08-31 20:22:07,820 [myid:] - INFO [main:ZooKeeper@687] - Session: 0x1002552fac20003 closed
2023-08-31 20:22:07,820 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@521] - EventThread shut down for session: 0x1002552fac20003
[root@k8s-m1 bin]#
查看状态信息
查看一个文件的状态信息
[zk: k8s-m1:2181(CONNECTED) 0] ls /
[bb0000000006, zookeeper]
[zk: k8s-m1:2181(CONNECTED) 1] stat /bb
Node does not exist: /bb
[zk: k8s-m1:2181(CONNECTED) 2] stat /bb0000000006
cZxid = 0x20000000f
ctime = Thu Aug 31 19:56:17 CST 2023
mZxid = 0x20000000f
mtime = Thu Aug 31 19:56:17 CST 2023
pZxid = 0x20000000f
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 0
详细解释:
zxid: 一个事务编号,zookeeper集群内部的所有事务,都有一个全局的唯一的顺序的编号。
它由两部分组成: 就是一个 64位的长整型 long
高32位: 用来标识leader关系是否改变,如 0x2
低32位: 用来做当前这个leader领导期间的全局的递增的事务编号,如上面的0000000f
状态属性 | 说明 |
---|---|
cZxid | 数据节点创建时的事务ID |
ctime | 数据节点创建时的时间 |
mZxid | 数据节点最后一次更新时的事务ID |
mtime | 数据节点最后一次更新时的时间 |
pZxid | 数据节点的子节点列表最后一次被修改(是子节点列表变更,而不是子节点内容变更)时的事务ID |
cversion | 子节点的版本号 |
dataVersion | 数据节点的版本号 |
aclVersion | 数据节点的ACL版本号 |
ephemeralOwner | 如果节点是临时节点,则表示创建该节点的会话的SessionID;如果节点是持久节点,则该属性值为0 |
dataLength | 数据内容的长度 |
numChildren | 数据节点当前的子节点个数 |
(1)修改节点a的数据,mZxid、dataVersion、dataLength 存储信息发生变化
[zk: k8s-m1:2181(CONNECTED) 10] ls /
[bb0000000006, zookeeper]
[zk: k8s-m1:2181(CONNECTED) 12] create /aa "first zookeeper"
Created /aa
[zk: k8s-m1:2181(CONNECTED) 13] ls /
[bb0000000006, aa, zookeeper]
[zk: k8s-m1:2181(CONNECTED) 14] set /aa 'aa'
cZxid = 0x200000019
ctime = Thu Aug 31 20:27:57 CST 2023
mZxid = 0x20000001a
mtime = Thu Aug 31 20:28:32 CST 2023
pZxid = 0x200000019
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 0
[zk: k8s-m1:2181(CONNECTED) 16]
(2)创建新的节点bb,状态信息都会发生变化,zxid的事物ID也会增加
[zk: k8s-m1:2181(CONNECTED) 18] create /bb "second zookeeper"
Created /bb
[zk: k8s-m1:2181(CONNECTED) 19] stat /bb
cZxid = 0x20000001b
ctime = Thu Aug 31 20:31:06 CST 2023
mZxid = 0x20000001b
mtime = Thu Aug 31 20:31:06 CST 2023
pZxid = 0x20000001b
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 16
numChildren = 0
(3)在aa节点下面新增节点bb,pZxid、cversion、numChildren 发生改变
[zk: k8s-m1:2181(CONNECTED) 21] create /aa/bb 'aabb'
Created /aa/bb
[zk: k8s-m1:2181(CONNECTED) 22] stat /aa
cZxid = 0x200000019
ctime = Thu Aug 31 20:27:57 CST 2023
mZxid = 0x20000001a
mtime = Thu Aug 31 20:28:32 CST 2023
pZxid = 0x20000001c
cversion = 1
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 1
(4)ephemeralOwner 持久性的节点信息是0x0临时的几点信息是本次会话的sessionid,如图
(5) 查看leader并将其干掉,此时将选择另一台机器成为leader,重新创建一个文件ff,此时发现czxid的前3位和之前发生变化,说明换了leader。其实前3位应该就是我们设置的serverid值。
#k8s-m2为leader
[root@k8s-m2 tmp]# /opt/apache-zookeeper-3.7.1-bin/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.7.1-bin/bin/../conf/zoo.cfg
Mode: leader
[root@k8s-m2 tmp]# /opt/apache-zookeeper-3.7.1-bin/bin/zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.7.1-bin/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED#在原来的界面继续操作
[zk: k8s-m1:2181(CONNECTED) 2] create /f 'ff'
Created /f
[zk: k8s-m1:2181(CONNECTED) 3] stat /f
cZxid = 0x300000001
ctime = Thu Aug 31 20:40:25 CST 2023
mZxid = 0x300000001
mtime = Thu Aug 31 20:40:25 CST 2023
pZxid = 0x300000001
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 0
[zk: k8s-m1:2181(CONNECTED) 4]
更多关于zookeeper的知识分享,请前往博客主页。编写过程中,能力有限难免出现差错,敬请指正