基本命令
create [-s] [-e] path data acl
$ create /baozi data-baozi
-e 创建临时节点
$ create -e /baozi/tmp data-tmp
Created /baozi/tmp
$ get /baozi/tmp
...
ephemeralOwner = 0x162ff8cabfb0002 //临时节点
...
-s 顺序节点
$ create -s /baozi/seq seq
Created /baozi/seq0000000001
$ create -s /baozi/seq seq
Created /baozi/seq0000000002
$ create -s /baozi/seq seq
Created /baozi/seq0000000003
$ create -s /baozi/seq seq
Created /baozi/seq0000000004
$ ls /baozi
[seq0000000003, seq0000000004, seq0000000001, seq0000000002]
stat/ls/ls2 path [watch]
ls2 = ls + stat
cZxid 创建事务id
ctime 创建时间
mZxid 修改事务id
mtime 修改时间
pZxid 子节点被修改的事务id
cversion 子节点更新次数
dataVersion 节点数据更新次数
aclVersion 节点acl的更新次数
ephemeralOwner 临时节点的话是session id
dataLength 节点数据的字节数
numChildren 子节点个数
$ ls /
[zookeeper]
$ ls2 /
[zookeeper]
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1
$ stat /
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1
get path [watch]
$ get /baozi
data-baozi
cZxid = 0x4
ctime = Thu Apr 26 09:42:32 CST 2018
mZxid = 0x4
mtime = Thu Apr 26 09:42:32 CST 2018
pZxid = 0x4
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0 //不是临时节点
dataLength = 10
numChildren = 0
set path data [version]
$ set /baozi data-newdata
cZxid = 0x4
ctime = Thu Apr 26 09:42:32 CST 2018
mZxid = 0x10
mtime = Thu Apr 26 10:08:46 CST 2018
pZxid = 0xf
cversion = 6
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 12
numChildren = 4
delete path [version]
//无视版本号直接删除
$ delete /baozi/seq0000000003
//根据版本号删除
$ delete /baozi/seq0000000004 1
rmr path
$ rmr /baozi
wacther
使用场景: 统一资源配置。(A服务器上的配置文件更新了,触发事件,去更新对应的客户端配置)
添加watcher事件: 通过stat/ls/ls2/get
NodeCreated
创建节点触发:NodeCreated
$ stat /baozi watch
$ create /baozi data-baozi
WATCHER::
WatchedEvent state:SyncConnected type:NodeCreated path:/baozi
Created /baozi
NodeDeleted
删除节点触发:NodeDeleted
$ ls /baozi watch
$ delete /baozi
WATCHER::
WatchedEvent state:SyncConnected type:NodeDeleted path:/baozi
NodeDataChanged
修改节点数据触发:NodeDataChanged
$ get /baozi watch
$ set /baozi data-new
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/baozi
NodeChildrenChanged
创建和删除子节点触发:NodeChildrenChanged,修改不触发事件。
$ ls /baozi watch
$ create /baozi/abc 88
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/baozi
Created /baozi/abc
$ ls /baozi watch
$ delete /baozi/abc
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/baozi
ACL
Zookeeper acl的构成: [scheme:id:permissions]
scheme:采用某种权限机制。
- world: world下只有一个id,即只有一个用户anyone。world:anyone:[permissions]
- auth: 需要认证登录。需要注册用户有权限就行。auth:user:pwd:[permissions]
- digest: 需要对密码加密。digest:user:BASE64(SHA1(pwd)):[permissions]
- ip: 指定可以访问的ip。ip:192.168.....:[permissions]
- super: 超级管理员。拥有所有权限
id:允许访问的用户、用户与密码、ip等。
permissions:crdwa,权限组合字符串。
- c create 创建子节点
- d delete 删除子节点
- r read 读取节点数据
- w write 设置节点数据
- a admin 设置权限
addauth scheme auth
明文登录(只有digest的scheme能登录,存密码时用的密文通过sha1与BASE64
$ addauth digest baozi:baozi
getAcl path
$ getAcl /baozi
setAcl path acl
$ setAcl /baozi world:anyone:crwa
super超级管理员
1.修改zkServer.sh添加管理员,在nohup一行,添加"-Dzookeeper.DigestAuthenticationProvider.superDigest=管理员:密码(sha1然后base64)"
2.重启zkServer.sh
四字命令
stat: 查看状态信息
$ echo stat | nc localhost 2181
Zookeeper version: 3.4.5-cdh5.7.0--1, built on 03/23/2016 18:31 GMT
Clients:
/0:0:0:0:0:0:0:1:45490[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 0/1/5
Received: 21
Sent: 20
Connections: 1
Outstanding: 0
Zxid: 0xd
Mode: standalone
Node count: 4
ruok: 查看zkServer是否启动(ruok :are you ok imok:i am ok)
$ echo ruok | nc localhost 2181
imok
dump: 列出未经处理的会话和临时节点
$ echo dump | nc localhost 2181
SessionTracker dump:
Session Sets (4):
0 expire at Fri Apr 27 10:59:44 CST 2018:
0 expire at Fri Apr 27 10:59:52 CST 2018:
0 expire at Fri Apr 27 11:00:02 CST 2018:
1 expire at Fri Apr 27 11:00:04 CST 2018:
0x16304ab3e430001
ephemeral nodes dump:
Sessions with Ephemerals (1):
0x16304ab3e430001:
/tmp
conf: 查看服务器配置
$ echo conf | nc localhost 2181
clientPort=2181
dataDir=/home/user000/tmps/zookeeper-tmp/version-2
dataLogDir=/home/user000/tmps/zookeeper-tmp/version-2
tickTime=2000
maxClientCnxns=60
minSessionTimeout=4000
maxSessionTimeout=40000
serverId=0
cons: 客户端连接信息
$ echo cons | nc localhost 2181
/0:0:0:0:0:0:0:1:45506[0](queued=0,recved=1,sent=0)
/127.0.0.1:34892[1](queued=0,recved=43,sent=43,sid=0x16304ab3e430001,lop=PING,est=1524797923862,to=30000,lcxid=0x2,lzxid=0xf,lresp=1524798333642,llat=1,minlat=0,avglat=0,maxlat=4)
envi: 环境变量
$ echo envi | nc localhost 2181
Environment:
zookeeper.version=3.4.5-cdh5.7.0--1, built on 03/23/2016 18:31 GMT
host.name=host000
java.version=1.8.0_162
java.vendor=Oracle Corporation
java.home=/home/user000/tools/jdk1.8.0_162/jre
java.class.path=/home/user000/tools/zookeeper/bin/../build/classes:/home/user000/tools/zookeeper/bin/../build/lib/*.jar:/home/user000/tools/zookeeper/bin/../share/zookeeper/zookeeper-3.4.5-cdh5.7.0.jar:/home/user000/tools/zookeeper/bin/../share/zookeeper/slf4j-log4j12-1.7.5.jar:/home/user000/tools/zookeeper/bin/../share/zookeeper/slf4j-api-1.7.5.jar:/home/user000/tools/zookeeper/bin/../share/zookeeper/netty-3.2.2.Final.jar:/home/user000/tools/zookeeper/bin/../share/zookeeper/log4j-1.2.16.jar:/home/user000/tools/zookeeper/bin/../share/zookeeper/jline-2.11.jar:/home/user000/tools/zookeeper/bin/../src/java/lib/*.jar:/home/user000/tools/zookeeper/bin/../conf:
java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
java.io.tmpdir=/tmp
java.compiler=<NA>
os.name=Linux
os.arch=amd64
os.version=3.10.0-693.el7.x86_64
user.name=user000
user.home=/home/user000
user.dir=/home/user000/tools/zookeeper-3.4.5-cdh5.7.0/bin
mntr: 监控zk健康信息
$ echo mntr | nc localhost 2181
zk_version 3.4.5-cdh5.7.0--1, built on 03/23/2016 18:31 GMT
zk_avg_latency 0
zk_max_latency 0
zk_min_latency 0
zk_packets_received 2
zk_packets_sent 1
zk_num_alive_connections 1
zk_outstanding_requests 0
zk_server_state standalone
zk_znode_count 5
zk_watch_count 0
zk_ephemerals_count 1
zk_approximate_data_size 39
zk_open_file_descriptor_count 28
zk_max_file_descriptor_count 4096
srvr: server的简要信息
$ echo srvr | nc localhost 2181
\Zookeeper version: 3.4.5-cdh5.7.0--1, built on 03/23/2016 18:31 GMT
Latency min/avg/max: 0/0/0
Received: 1
Sent: 0
Connections: 1
Outstanding: 0
Zxid: 0x71
Mode: standalone
Node count: 4
wchs: 展示watch的信息
$ echo wchs | nc localhost 2181
0 connections watching 0 paths
Total watches:0
wchc wchp: 显示session与path
------------------
3.4.10以后版本需要配置这两个命令到白名单
修改zoo.cfg,添加41w.commands.whitelist=wchc,wchp
重启zkServer
------------------
$ echo wchc | nc localhost 2181
0x163051797180001
/baozi
/baozi2
$ echo wchp | nc localhost 2181
/baozi
0x163051797180001
/baozi2
0x163051797180001