zookeeper工具书 - (zkCli常用命令 + 四字命令)

一、ZkCli常用命令

ZooKeeper-cli 官网介绍:https://zookeeper.apache.org/doc/current/zookeeperCLI.html

zkCli.sh

进入ZooKeeper-cli

# 不传参数默认连接本地的2181端口
# connect to the localhost with the default port:2181
bin/zkCli.sh

# 连接指定的ip下的指定端口,设置连接超时时间
# connect to the remote host with timeout:3s
bin/zkCli.sh -timeout 3000 -server remoteIP:2181

# 使用 -waitforconnection 选项连接到远程主机,等待连接成功后再执行命令    
# connect to the remote host with -waitforconnection option to wait for connection success before executing commands
bin/zkCli.sh -waitforconnection -timeout 3000 -server remoteIP:2181

# 通过指定配置文件启动zkCli    
# connect with a custom client configuration properties file
bin/zkCli.sh -client-configuration /path/to/client.properties

help

显示zk命令的帮助信息(Showing helps about ZooKeeper commands)

help
===>
ZooKeeper -server host:port cmd args
        addauth scheme auth
        close
        connect host:port
        create [-s] [-e] path [data] [acl]
        delete [-v version] path
        deleteall path
        delquota [-n|-b] path
        get [-s] [-w] path
        getAcl [-s] path
        history
        listquota path
        ls [-w] path
        ls2 path [watch]
        printwatches on|off
        quit
        redo cmdno
        removewatches path [-c|-d|-a] [-l]
        rmr path
        set [-s] [-v version] path data
        setAcl [-s] [-v version] path acl
        setquota -n|-b val path
        stat [-w] path
        sync path

addauth

为 ACL 添加授权用户(Add a authorized user for ACL)

#  addauth digest 用户名:明文密码
[zkshell: 12] addauth digest zookeeper:admin

close 关闭当前客户端/会话(Close this client/session)

[zkshell: 0] close
    2019-03-09 06:42:22,178 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@528] - EventThread shut down for session: 0x10007ab7c550006
    2019-03-09 06:42:22,179 [myid:] - INFO  [main:ZooKeeper@1346] - Session: 0x10007ab7c550006 closed

# 查看节点
ls /
===>
Not connected

connect host:port

连接一个zk服务端(Connect a ZooKeeper server)

# connect 默认连接127.0.0.1:2181及本地的zk server
[zkshell: 4] connect
    2019-03-09 06:43:33,179 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@986] - Socket connection established, initiating session, client: /127.0.0.1:35144, server: localhost/127.0.0.1:2181
    2019-03-09 06:43:33,189 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1421] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x10007ab7c550007, negotiated timeout = 30000
    connect "localhost:2181,localhost:2182,localhost:2183"

# 连接远程的zk server        
# connect a remote server
[zkshell: 5] connect remoteIP:2181

create [-s] [-e] path [data] [acl]

创建一个znode(Create a znode)

# 创建一个永久节点
# create a persistent_node
[zkshell: 7] create /persistent_node
    Created /persistent_node

# 创建一个临时节点
# create a ephemeral node
[zkshell: 8] create -e /ephemeral_node mydata
    Created /ephemeral_node

# 创建一个永久有序的节点
# create the persistent-sequential node
[zkshell: 9] create -s /persistent_sequential_node mydata
    Created /persistent_sequential_node0000000176

# 创建一个临时有序节点
# create the ephemeral-sequential_node
[zkshell: 10] create -s -e /ephemeral_sequential_node mydata
    Created /ephemeral_sequential_node0000000174

# 创建一个带有概要信息的节点(直译)
# create a node with the schema
# create 待创建的znode 设置节点的数据 digest:用户名:加密密码:权限    在创建znode的同时Acl权限
[zkshell: 11] create /zk-node-create-schema mydata digest:user1:+owfoSBn/am19roBPzR1/MfCblE=:crwad
    Created /zk-node-create-schema
[zkshell: 12] addauth digest user1:12345
[zkshell: 13] getAcl /zk-node-create-schema
    'digest,'user1:+owfoSBn/am19roBPzR1/MfCblE=
    : cdrwa
[zkshell: 13]  get /zk-node-create-schema
     mydata

# 创建一个容器节点,当该容器节点下的最后一个znode被删除后,该容器节点就会被删除
# 注意事项:在ZK 3.5版本后,新增了容器和TTL节点,分别是使用-c和-t创建。如果版本低于3.5的,是没有容器和TTL节点。
# create the container node.When the last child of a container is deleted,the container becomes to be deleted
[zkshell: 14] create -c /container_node mydata
    Created /container_node
[zkshell: 15] create -c /container_node/child_1 mydata
    Created /container_node/child_1
[zkshell: 16] create -c /container_node/child_2 mydata
    Created /container_node/child_2
[zkshell: 17] delete /container_node/child_1
[zkshell: 18] delete /container_node/child_2
[zkshell: 19] get /container_node
    org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /container_node

# 创建一个有存活时间的节点
# create the ttl node.
# set zookeeper.extendedTypesEnabled=true
# Otherwise:KeeperErrorCode = Unimplemented for /ttl_node
# 创建一个存活时间为3秒的znode
[zkshell: 20] create -t 3000 /ttl_node mydata
    Created /ttl_node
# after 3s later
# 3秒之后再次查看,发现节点不存在了
[zkshell: 21] get /ttl_node
    org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /ttl_node

delete [-v version] path

删除指定路径下的znode(Delete a node with a specific path)

[zkshell: 2] delete /config/topics/test
[zkshell: 3] ls /config/topics/test
    Node does not exist: /config/topics/test

# 需要注意的是不能删除还有子节点的父节点
[zkshell: 4] create /follower/test1
     Created /follower/test
[zkshell: 5] delete /follower
     Node not empty: /follower

deleteall path

删除指定路径下的所有znode(Delete all nodes under a specific path)

zkshell: 1] ls /config
    [changes, clients, topics]
[zkshell: 2] deleteall /config
[zkshell: 3] ls /config
    Node does not exist: /config

setquota -n|-b val path

为某一个znode设置子节点个数限制(Set the quota in one path)

# 实际上当创建的指定路径子节点个数(包括当前节点)超过设置的数目时,只是在日志中告警,对创建znode节点不受影响
# -n参数为限制子节点个数(包括本身的节点)
# -n to limit the number of child nodes(included itself)
[zkshell: 18] setquota -n 2 /quota_test
[zkshell: 19] create /quota_test/child_1
    Created /quota_test/child_1
[zkshell: 20] create /quota_test/child_2
    Created /quota_test/child_2
[zkshell: 21] create /quota_test/child_3
    Created /quota_test/child_3
# 注意:没有硬性约束,只记录警告信息
# Notice:don't have a hard constraint,just log the warning info
    2019-03-07 11:22:36,680 [myid:1] - WARN  [SyncThread:0:DataTree@374] - Quota exceeded: /quota_test count=3 limit=2
    2019-03-07 11:22:41,861 [myid:1] - WARN  [SyncThread:0:DataTree@374] - Quota exceeded: /quota_test count=4 limit=2

# -b 限制某个znode的节点数据的字节数大小        
# -b to limit the bytes(data length) of one path
[zkshell: 22] setquota -b 5 /brokers
[zkshell: 23] set /brokers "I_love_zookeeper"
# 注意:没有硬性约束,只记录警告信息        
# Notice:don't have a hard constraint,just log the warning info
    WARN  [CommitProcWorkThread-7:DataTree@379] - Quota exceeded: /brokers bytes=4206 limit=5

# -N 设置硬性配额限制 无法创建超过该数目的子节点    3.5之后才有的功能
# -N count Hard quota
[zkshell: 3] create /c1
Created /c1
[zkshell: 4] setquota -N 2 /c1
[zkshell: 5] listquota /c1
absolute path is /zookeeper/quota/c1/zookeeper_limits
Output quota for /c1 count=-1,bytes=-1=;byteHardLimit=-1;countHardLimit=2
Output stat for /c1 count=2,bytes=0
[zkshell: 6] create /c1/ch-3
Count Quota has exceeded : /c1/ch-3


# -B 硬性节点配额限制,节点数据的大小不能超过该限制 3.5之后才有的功能
# -B byte Hard quota
[zkshell: 3] create /c2
[zkshell: 4] setquota -B 4 /c2
[zkshell: 5] set /c2 "foo"
[zkshell: 6] set /c2 "foo-bar"
Bytes Quota has exceeded : /c2
[zkshell: 7] get /c2
foo

delquota [-n|-b] path

删除路径下的配额(Delete the quota under a path)

# 删除该节点的所有配额限制
[zkshell: 1] delquota /quota_test
# 查看发现该节点没有设置配额,说明已经成功解除限制
[zkshell: 2] listquota /quota_test
    absolute path is /zookeeper/quota/quota_test/zookeeper_limits
    quota for /quota_test does not exist.
[zkshell: 3] delquota -n /c1
[zkshell: 4] delquota -N /c2
[zkshell: 5] delquota -b /c3
[zkshell: 6] delquota -B /c4    

get [-s] [-w] path

获取指定路径的数据(Get the data of the specific path)

[zkshell: 10] get /latest_producer_id_block
    {"version":1,"broker":0,"block_start":"0","block_end":"999"}

# -s 显示统计数据
# -s to show the stat 
[zkshell: 11] get -s /latest_producer_id_block
    {"version":1,"broker":0,"block_start":"0","block_end":"999"}
    cZxid = 0x90000009a
    ctime = Sat Jul 28 08:14:09 UTC 2018
    mZxid = 0x9000000a2
    mtime = Sat Jul 28 08:14:12 UTC 2018
    pZxid = 0x90000009a
    cversion = 0
    dataVersion = 1
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 60
    numChildren = 0

# -w 对该节点的数据变化设置监听 ,注意:开启printwatches,一旦监听到该节点的数据发生改变,会打印一行数据改变的提示记录
# -w to set a watch on the data change, Notice: turn on the printwatches
[zkshell: 12] get -w /latest_producer_id_block
    {"version":1,"broker":0,"block_start":"0","block_end":"999"}
[zkshell: 13] set /latest_producer_id_block mydata
    WATCHER::
    WatchedEvent state:SyncConnected type:NodeDataChanged path:/latest_producer_id_block

getAcl [-s] path

获取指定路径的ACL权限(Get the ACL permission of one path)

# create 节点 节点数据 可以访问该znode的白名单
[zkshell: 4] create /acl_test mydata ip:127.0.0.1:crwda
    Created /acl_test
[zkshell: 5] getAcl /acl_test
    'ip,'127.0.0.1
    : cdrwa
    [zkshell: 6] getAcl /testwatch
    'world,'anyone
    : cdrwa

history

显示你最近执行的11个命令(Showing the history about the recent 11 commands that you have executed)

[zkshell: 7] history
    0 - close
    1 - close
    2 - ls /
    3 - ls /
    4 - connect
    5 - ls /
    6 - ll
    7 - history

listquota path

列出某个路径的配额信息(Listing the quota of one path)

# 当节点设置过配额时:
[zkshell: 1] listquota /c1
             absolute path is /zookeeper/quota/c1/zookeeper_limits
             Output quota for /c1 count=-1,bytes=-1=;byteHardLimit=-1;countHardLimit=2
             Output stat for /c1 count=4,bytes=0

# 当节点没有设置过配额时:                 
[zkshell: 2] listquota /test1
absolute path is /zookeeper/quota/test1/zookeeper_limits
quota for /test1 does not exist.

ls [-w] path

列出某个路径的子节点(Listing the child nodes of one path)

[zkshell: 36] ls /quota_test
    [child_1, child_2, child_3]

# -s 显示节点的统计信息
# -s to show the stat
[zkshell: 37] ls -s /quota_test
    [child_1, child_2, child_3]
    cZxid = 0x110000002d
    ctime = Thu Mar 07 11:19:07 UTC 2019
    mZxid = 0x110000002d
    mtime = Thu Mar 07 11:19:07 UTC 2019
    pZxid = 0x1100000033
    cversion = 3
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 0
    numChildren = 3

# -R 递归显示该节点的子节点  3.5之后才有的功能
# -R to show the child nodes recursely
[zkshell: 38] ls -R /quota_test
    /quota_test
    /quota_test/child_1
    /quota_test/child_2
    /quota_test/child_3

# -w 对子节点数目设置监听,当监听到子节点数据发生变化时,会打印一个子节点数发生变化的提示信息 注意:开启printwatches
# -w to set a watch on the child change,Notice: turn on the printwatches
[zkshell: 39] ls -w /brokers
    [ids, seqid, topics]
[zkshell: 40] delete /brokers/ids
    WATCHER::
    WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/brokers

ls2 path [watch]

查看节点数据并能看到更新次数等数据

[zkshell: 41] ls2 /test
     [test]
     cZxid = 0x50002814f
     ctime = Sun Aug 28 17:32:33 CST 2022
     mZxid = 0x50002814f
     mtime = Sun Aug 28 17:32:33 CST 2022
     pZxid = 0x500028150
     cversion = 1
     dataVersion = 0
     aclVersion = 0
     ephemeralOwner = 0x0
     dataLength = 0
     numChildren = 1

# 查看子节点信息的同时对子节点数变化进行监听
 [zkshell: 41] ls2 /test watch 
     [test]
     cZxid = 0x50002814f
     ctime = Sun Aug 28 17:32:33 CST 2022
     mZxid = 0x50002814f
     mtime = Sun Aug 28 17:32:33 CST 2022
     pZxid = 0x500028150
     cversion = 1
     dataVersion = 0
     aclVersion = 0
     ephemeralOwner = 0x0
     dataLength = 0
     numChildren = 1   
 [zkshell: 41] create /test/ceshi
     Created /test/ceshi
   WATCHER::
   WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/test  

printwatches on|off

开启/关闭是否打印监听的开关(A switch to turn on/off whether printing watches or not)

# printwatches查看该功能的状态:on|off
[zkshell: 0] printwatches
    printwatches is on

# 关闭printwatches 
[zkshell: 1] printwatches off
[zkshell: 2] printwatches
    printwatches is off

# 开启printwatches    
[zkshell: 3] printwatches on
[zkshell: 4] printwatches
    printwatches is on

quit

退出CLI窗口(Quit the CLI windows)

[zkshell: 1] quit
2022-08-28 17:37:37,694 [myid:] - INFO  [main:ZooKeeper@877] - Session: xxxxxxxxxxxxxxxx closed
2022-08-28 17:37:37,695 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@540] - EventThread shut down

redo cmdno

重新执行历史中的索引指向的命令 (Redo the cmd with the index from history)

# 查看历史最近使用的命令,每个命令都有一个索引
[zkshell: 4] history
    0 - ls /
    1 - get /consumers
    2 - get /hbase
    3 - ls  /hbase
    4 - history

# redo 3 执行历史记录中索引号为3的这条命令    ===> ls  /hbase 
[zkshell: 5] redo 3
    [backup-masters, draining, flush-table-proc, hbaseid, master-maintenance, meta-region-server, namespace, online-snapshot, replication, rs, running, splitWAL, switch, table, table-lock]

removewatches path [-c|-d|-a] [-l]

移除某个节点的监听(Remove the watches under a node)

# 对节点的数据变化进行监听
[zkshell: 1] get -w /brokers
    null
# 移除针对某个节点的数据变化的监听    
[zkshell: 2] removewatches /brokers
    WATCHER::
    WatchedEvent state:SyncConnected type:DataWatchRemoved path:/brokers

rmr path

递归删除当前节点及其所有的子节点

# 查看/follower的子节点信息
[zkshell: 3] ls /follower
     [test, wind]

# 递归删除/follower及其所有的子节点
[zkshell: 4] rmr /follower
     [test, wind]

[zkshell: 4] ls /follow
Node does not exist: /follow

set [-s] [-v version] path data

设置/更新节点的数据(Set/update the data on a path)

[zkshell: 50] set /brokers myNewData

# -s 显示该节点的统计信息
# -s to show the stat of this node.
[zkshell: 51] set -s /quota_test mydata_for_quota_test
    cZxid = 0x110000002d
    ctime = Thu Mar 07 11:19:07 UTC 2019
    mZxid = 0x1100000038
    mtime = Thu Mar 07 11:42:41 UTC 2019
    pZxid = 0x1100000033
    cversion = 3
    dataVersion = 2
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 21
    numChildren = 3

# -v 带版本号修改节点数据,但是-v后的数字需要和dataversion一致,否则会报错
# -v to set the data with CAS,the version can be found from dataVersion using stat.
[zkshell: 52] set -v 0 /brokers myNewData
[zkshell: 53] set -v 0 /brokers myNewData
    version No is not valid : /brokers

setAcl [-s] [-v version] path acl

设置节点的ACL权限(Set the Acl permission for one node)

# 创建用户
[zkshell: 28] addauth digest user1:12345
# 通过auth模式设置节点的ACL权限    setAcl 节点路径 授权模式:用户名:密码:权限
[zkshell: 30] setAcl /acl_auth_test auth:user1:12345:crwad
[zkshell: 31] getAcl /acl_auth_test
    'digest,'user1:+owfoSBn/am19roBPzR1/MfCblE=
    : cdrwa

# -R 递归设置ACL权限
# -R to set Acl recursely
[zkshell: 32] ls /acl_auth_test
    [child_1, child_2]
[zkshell: 33] getAcl /acl_auth_test/child_2
    'world,'anyone
    : cdrwa
[zkshell: 34] setAcl -R /acl_auth_test auth:user1:12345:crwad
[zkshell: 35] getAcl /acl_auth_test/child_2
    'digest,'user1:+owfoSBn/am19roBPzR1/MfCblE=
    : cdrwa

# -v 带ACL版本号修改节点的ACL权限,和上面同理,需要和aclVersion相同才能修改成功,或者干脆不带版本号修改
# -v set Acl with the acl version which can be found from the aclVersion using the stat
[zkshell: 36] stat /acl_auth_test
    cZxid = 0xf9fc0000001c
    ctime = Tue Mar 26 16:50:58 CST 2019
    mZxid = 0xf9fc0000001c
    mtime = Tue Mar 26 16:50:58 CST 2019
    pZxid = 0xf9fc0000001f
    cversion = 2
    dataVersion = 0
    aclVersion = 3
    ephemeralOwner = 0x0
    dataLength = 0
    numChildren = 2
[zkshell: 37] setAcl -v 3 /acl_auth_test auth:user1:12345:crwad

stat [-w] path

显示一个节点的统计信息/元数据信息(Showing the stat/metadata of one node)

[zkshell: 1] stat /hbase
    cZxid = 0x4000013d9
    ctime = Wed Jun 27 20:13:07 CST 2018
    mZxid = 0x4000013d9
    mtime = Wed Jun 27 20:13:07 CST 2018
    pZxid = 0x500000001
    cversion = 17
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 0
    numChildren = 15

# -w 对节点数据变化进行监听        
[zkshell: 2] stat -w /hbase
[zkshell: 3] set /hbase 123
        WATCHER::
        WatchedEvent state:SyncConnected type:NodeDataChanged path:/hbase

sync path

在leader和follower之间同步一个节点的数据(异步同步)(Sync the data of one node between leader and followers(Asynchronous sync)

[zkshell: 14] sync /
[zkshell: 15] Sync is OK

version

显示zk客户端/zkCli的版本号(Show the version of the ZooKeeper client/CLI)

[zkshell: 1] version
ZooKeeper CLI version: 3.6.0-SNAPSHOT-29f9b2c1c0e832081f94d59a6b88709c5f1bb3ca, built on 05/30/2019 09:26 GMT

二、四字命令 (The Four Letter Words)

**官网连接 ** https://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_adminserver

conf : New in 3.3.0: Print details about serving configuration.

cons : New in 3.3.0: List full connection/session details for all clients connected to this server. Includes information on numbers of packets received/sent, session id, operation latencies, last operation performed, etc...

crst : New in 3.3.0: Reset connection/session statistics for all connections.

dump : Lists the outstanding sessions and ephemeral nodes.

envi : Print details about serving environment

ruok : Tests if the server is running in a non-error state. When the whitelist enables ruok, the server will respond with imok if it is running, otherwise it will not respond at all. When ruok is disabled, the server responds with: "ruok is not executed because it is not in the whitelist." A response of "imok" does not necessarily indicate that the server has joined the quorum, just that the server process is active and bound to the specified client port. Use "stat" for details on state wrt quorum and client connection information.

srst : Reset server statistics.

srvr : New in 3.3.0: Lists full details for the server.

stat : Lists brief details for the server and connected clients.

wchs : New in 3.3.0: Lists brief information on watches for the server.

wchc : New in 3.3.0: Lists detailed information on watches for the server, by session. This outputs a list of sessions(connections) with associated watches (paths). Note, depending on the number of watches this operation may be expensive (ie impact server performance), use it carefully.

dirs : New in 3.5.1: Shows the total size of snapshot and log files in bytes

wchp : New in 3.3.0: Lists detailed information on watches for the server, by path. This outputs a list of paths (znodes) with associated sessions. Note, depending on the number of watches this operation may be expensive (ie impact server performance), use it carefully.

mntr : New in 3.4.0: Outputs a list of variables that could be used for monitoring the health of the cluster.

#  使用方式
echo 四字命令 | nc localhost 2181 
===>
zk_version 3.4.0 zk_avg_latency 0.7561 - be account to four decimal places zk_max_latency 0 zk_min_latency 0 zk_packets_received 70 zk_packets_sent 69 zk_outstanding_requests 0 zk_server_state leader zk_znode_count 4 zk_watch_count 0 zk_ephemerals_count 0 zk_approximate_data_size 27 zk_followers 4 - only exposed by the Leader zk_synced_followers 4 - only exposed by the Leader zk_pending_syncs 0 - only exposed by the Leader zk_open_file_descriptor_count 23 - only available on Unix platforms zk_max_file_descriptor_count 1024 - only available on Unix platforms
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值