Categories
DevOps

How to execute ZooKeeper commands using command-line interface

Execute ZooKeeper commands using command-line interface.

By default zkCli.sh is very verbose.

$ sudo -u zookeeper /opt/zookeeper/zookeeper/bin/zkCli.sh                                                                                                       [16/1996]
/usr/bin/java                                                                                           
Connecting to localhost:2181                                                                            
2021-06-11 21:37:39,885 [myid:] - INFO  [main:Environment@98] - Client environment:zookeeper.version=3.7.0-e3704b390a6697bfdf4b0bef79e3da7a4f6bac4b, built on 2021-03-17 09:46 UTC
2021-06-11 21:37:39,887 [myid:] - INFO  [main:Environment@98] - Client environment:host.name=zookeeper3
2021-06-11 21:37:39,887 [myid:] - INFO  [main:Environment@98] - Client environment:java.version=11.0.11                                                                                                          
2021-06-11 21:37:39,889 [myid:] - INFO  [main:Environment@98] - Client environment:java.vendor=Debian                                                                                                            
2021-06-11 21:37:39,890 [myid:] - INFO  [main:Environment@98] - Client environment:java.home=/usr/lib/jvm/java-11-openjdk-amd64
2021-06-11 21:37:39,890 [myid:] - INFO  [main:Environment@98] - Client environment:java.class.path=/opt/zookeeper/zookeeper/bin/../zookeeper-server/target/classes:/opt/zookeeper/zookeeper/bin/../build/classes:
/opt/zookeeper/zookeeper/bin/../zookeeper-server/target/lib/*.jar:/opt/zookeeper/zookeeper/bin/../build/lib/*.jar:/opt/zookeeper/zookeeper/bin/../lib/zookeeper-prometheus-metrics-3.7.0.jar:/opt/zookeeper/zooke
eper/bin/../lib/zookeeper-jute-3.7.0.jar:/opt/zookeeper/zookeeper/bin/../lib/zookeeper-3.7.0.jar:/opt/zookeeper/zookeeper/bin/../lib/snappy-java-1.1.7.7.jar:/opt/zookeeper/zookeeper/bin/../lib/slf4j-log4j12-1.
7.30.jar:/opt/zookeeper/zookeeper/bin/../lib/slf4j-api-1.7.30.jar:/opt/zookeeper/zookeeper/bin/../lib/simpleclient_servlet-0.9.0.jar:/opt/zookeeper/zookeeper/bin/../lib/simpleclient_hotspot-0.9.0.jar:/opt/zook
eeper/zookeeper/bin/../lib/simpleclient_common-0.9.0.jar:/opt/zookeeper/zookeeper/bin/../lib/simpleclient-0.9.0.jar:/opt/zookeeper/zookeeper/bin/../lib/netty-transport-native-unix-common-4.1.59.Final.jar:/opt/
zookeeper/zookeeper/bin/../lib/netty-transport-native-epoll-4.1.59.Final.jar:/opt/zookeeper/zookeeper/bin/../lib/netty-transport-4.1.59.Final.jar:/opt/zookeeper/zookeeper/bin/../lib/netty-resolver-4.1.59.Final
.jar:/opt/zookeeper/zookeeper/bin/../lib/netty-handler-4.1.59.Final.jar:/opt/zookeeper/zookeeper/bin/../lib/netty-common-4.1.59.Final.jar:/opt/zookeeper/zookeeper/bin/../lib/netty-codec-4.1.59.Final.jar:/opt/z
ookeeper/zookeeper/bin/../lib/netty-buffer-4.1.59.Final.jar:/opt/zookeeper/zookeeper/bin/../lib/metrics-core-4.1.12.1.jar:/opt/zookeeper/zookeeper/bin/../lib/log4j-1.2.17.jar:/opt/zookeeper/zookeeper/bin/../li
b/jline-2.14.6.jar:/opt/zookeeper/zookeeper/bin/../lib/jetty-util-ajax-9.4.38.v20210224.jar:/opt/zookeeper/zookeeper/bin/../lib/jetty-util-9.4.38.v20210224.jar:/opt/zookeeper/zookeeper/bin/../lib/jetty-servlet
-9.4.38.v20210224.jar:/opt/zookeeper/zookeeper/bin/../lib/jetty-server-9.4.38.v20210224.jar:/opt/zookeeper/zookeeper/bin/../lib/jetty-security-9.4.38.v20210224.jar:/opt/zookeeper/zookeeper/bin/../lib/jetty-io-
9.4.38.v20210224.jar:/opt/zookeeper/zookeeper/bin/../lib/jetty-http-9.4.38.v20210224.jar:/opt/zookeeper/zookeeper/bin/../lib/javax.servlet-api-3.1.0.jar:/opt/zookeeper/zookeeper/bin/../lib/jackson-databind-2.1
0.5.1.jar:/opt/zookeeper/zookeeper/bin/../lib/jackson-core-2.10.5.jar:/opt/zookeeper/zookeeper/bin/../lib/jackson-annotations-2.10.5.jar:/opt/zookeeper/zookeeper/bin/../lib/commons-cli-1.4.jar:/opt/zookeeper/z
ookeeper/bin/../lib/audience-annotations-0.12.0.jar:/opt/zookeeper/zookeeper/bin/../zookeeper-*.jar:/opt/zookeeper/zookeeper/bin/../zookeeper-server/src/main/resources/lib/*.jar:/opt/zookeeper/zookeeper/bin/..
/conf:
2021-06-11 21:37:39,890 [myid:] - INFO  [main:Environment@98] - Client environment:java.library.path=/usr/java/packages/lib:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/li
b/jni:/lib:/usr/lib
2021-06-11 21:37:39,890 [myid:] - INFO  [main:Environment@98] - Client environment:java.io.tmpdir=/tmp
2021-06-11 21:37:39,890 [myid:] - INFO  [main:Environment@98] - Client environment:java.compiler=
2021-06-11 21:37:39,890 [myid:] - INFO  [main:Environment@98] - Client environment:os.name=Linux
2021-06-11 21:37:39,890 [myid:] - INFO  [main:Environment@98] - Client environment:os.arch=amd64
2021-06-11 21:37:39,890 [myid:] - INFO  [main:Environment@98] - Client environment:os.version=5.10.0-3-amd64
2021-06-11 21:37:39,890 [myid:] - INFO  [main:Environment@98] - Client environment:user.name=zookeeper
2021-06-11 21:37:39,890 [myid:] - INFO  [main:Environment@98] - Client environment:user.home=/opt/zookeeper
2021-06-11 21:37:39,891 [myid:] - INFO  [main:Environment@98] - Client environment:user.dir=/opt/zookeeper/zookeeper
2021-06-11 21:37:39,891 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.free=4MB
2021-06-11 21:37:39,892 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.max=247MB
2021-06-11 21:37:39,892 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.total=7MB
2021-06-11 21:37:39,896 [myid:] - INFO  [main:ZooKeeper@637] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@68c4039c
2021-06-11 21:37:39,899 [myid:] - INFO  [main:X509Util@77] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2021-06-11 21:37:39,905 [myid:] - INFO  [main:ClientCnxnSocket@239] - jute.maxbuffer value is 1048575 Bytes
2021-06-11 21:37:39,913 [myid:] - INFO  [main:ClientCnxn@1726] - zookeeper.request.timeout value is 0. feature enabled=false
Welcome to ZooKeeper!
JLine support is enabled
2021-06-11 21:37:39,965 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1171] - Opening socket connection to server localhost/127.0.0.1:2181.
2021-06-11 21:37:39,966 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1173] - SASL config status: Will not attempt to authenticate using SASL (unknown error)
2021-06-11 21:37:39,985 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1005] - Socket connection established, initiating session, client: /127.0.0.1:57456, server: localho
st/127.0.0.1:2181
2021-06-11 21:37:39,993 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1438] - Session establishment complete on server localhost/127.0.0.1:2181, session id = 0x3000000d28
90021, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null 
[zk: localhost:2181(CONNECTED) 0] get /zookeeper/config
server.1=zookeeper1.example.org:2888:3888:participant
server.2=zookeeper2.example.org:2888:3888:participant
server.3=zookeeper3.example.org:2888:3888:participant
version=0
[zk: localhost:2181(CONNECTED) 1] quit
2021-06-11 21:37:50,225 [myid:localhost:2181] - WARN  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1284] - An exception was thrown while closing send thread for session 0x3000000d2890021.
EndOfStreamException: Unable to read additional data from server sessionid 0x3000000d2890021, likely server has closed socket
        at org.apache.zookeeper.ClientCnxnSocketNIO.doIO(ClientCnxnSocketNIO.java:77)
        at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:350)
        at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1280)

WATCHER::

WatchedEvent state:Closed type:None path:null
2021-06-11 21:37:50,364 [myid:] - INFO  [main:ZooKeeper@1232] - Session: 0x3000000d2890021 closed
2021-06-11 21:37:50,366 [myid:] - ERROR [main:ServiceUtils@42] - Exiting JVM with code 0
2021-06-11 21:37:50,367 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@570] - EventThread shut down for session: 0x3000000d2890021

You can store these messages in a log file.

$ sudo -u zookeeper ZOO_LOG4J_PROP="INFO,ROLLINGFILE" /opt/zookeeper/zookeeper/bin/zkCli.sh
/usr/bin/java
Connecting to localhost:2181
Welcome to ZooKeeper!
JLine support is enabled

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] get /zookeeper/config 
server.1=zookeeper1.example.org:2888:3888:participant
server.2=zookeeper2.example.org:2888:3888:participant
server.3=zookeeper3.example.org:2888:3888:participant
version=0
[zk: localhost:2181(CONNECTED) 1] quit

WATCHER::

WatchedEvent state:Closed type:None path:null
cat /opt/zookeeper/zookeeper/logs/zookeeper-zookeeper-cli-zookeeper3.log 
2021-06-11 21:40:04,057 [myid:] - ERROR [main:ServiceUtils@42] - Exiting JVM with code 0

Log entries will be stored in a log file.

$ cat /opt/zookeeper/zookeeper/logs/zookeeper-zookeeper-cli-zookeeper3.log
2021-06-11 21:40:45,051 [myid:] - INFO  [main:Environment@98] - Client environment:zookeeper.version=3.7.0-e3704b390a6697bfdf4b0bef79e3da7a4f6bac4b, built on 2021-03-17 09:46 UTC
2021-06-11 21:40:45,054 [myid:] - INFO  [main:Environment@98] - Client environment:host.name=zookeeper3
2021-06-11 21:40:45,054 [myid:] - INFO  [main:Environment@98] - Client environment:java.version=11.0.11
2021-06-11 21:40:45,056 [myid:] - INFO  [main:Environment@98] - Client environment:java.vendor=Debian
2021-06-11 21:40:45,056 [myid:] - INFO  [main:Environment@98] - Client environment:java.home=/usr/lib/jvm/java-11-openjdk-amd64
2021-06-11 21:40:45,056 [myid:] - INFO  [main:Environment@98] - Client environment:java.class.path=/opt/zookeeper/zookeeper/bin/../zookeeper-server/target/classes:/opt/zookeeper/zookeeper/bin/../build/classes:/opt/zookeeper/zookeeper/bin/../zookeeper-server/target/lib/*.jar:/opt/zookeeper/zookeeper/bin/../build/lib/*.jar:/opt/zookeeper/zookeeper/bin/../lib/zookeeper-prometheus-metrics-3.7.0.jar:/opt/zookeeper/zookeeper/bin/../lib/zookeeper-jute-3.7.0.jar:/opt/zookeeper/zookeeper/bin/../lib/zookeeper-3.7.0.jar:/opt/zookeeper/zookeeper/bin/../lib/snappy-java-1.1.7.7.jar:/opt/zookeeper/zookeeper/bin/../lib/slf4j-log4j12-1.7.30.jar:/opt/zookeeper/zookeeper/bin/../lib/slf4j-api-1.7.30.jar:/opt/zookeeper/zookeeper/bin/../lib/simpleclient_servlet-0.9.0.jar:/opt/zookeeper/zookeeper/bin/../lib/simpleclient_hotspot-0.9.0.jar:/opt/zookeeper/zookeeper/bin/../lib/simpleclient_common-0.9.0.jar:/opt/zookeeper/zookeeper/bin/../lib/simpleclient-0.9.0.jar:/opt/zookeeper/zookeeper/bin/../lib/netty-transport-native-unix-common-4.1.59.Final.jar:/opt/zookeeper/zookeeper/bin/../lib/netty-transport-native-epoll-4.1.59.Final.jar:/opt/zookeeper/zookeeper/bin/../lib/netty-transport-4.1.59.Final.jar:/opt/zookeeper/zookeeper/bin/../lib/netty-resolver-4.1.59.Final.jar:/opt/zookeeper/zookeeper/bin/../lib/netty-handler-4.1.59.Final.jar:/opt/zookeeper/zookeeper/bin/../lib/netty-common-4.1.59.Final.jar:/opt/zookeeper/zookeeper/bin/../lib/netty-codec-4.1.59.Final.jar:/opt/zookeeper/zookeeper/bin/../lib/netty-buffer-4.1.59.Final.jar:/opt/zookeeper/zookeeper/bin/../lib/metrics-core-4.1.12.1.jar:/opt/zookeeper/zookeeper/bin/../lib/log4j-1.2.17.jar:/opt/zookeeper/zookeeper/bin/../lib/jline-2.14.6.jar:/opt/zookeeper/zookeeper/bin/../lib/jetty-util-ajax-9.4.38.v20210224.jar:/opt/zookeeper/zookeeper/bin/../lib/jetty-util-9.4.38.v20210224.jar:/opt/zookeeper/zookeeper/bin/../lib/jetty-servlet-9.4.38.v20210224.jar:/opt/zookeeper/zookeeper/bin/../lib/jetty-server-9.4.38.v20210224.jar:/opt/zookeeper/zookeeper/bin/../lib/jetty-security-9.4.38.v20210224.jar:/opt/zookeeper/zookeeper/bin/../lib/jetty-io-9.4.38.v20210224.jar:/opt/zookeeper/zookeeper/bin/../lib/jetty-http-9.4.38.v20210224.jar:/opt/zookeeper/zookeeper/bin/../lib/javax.servlet-api-3.1.0.jar:/opt/zookeeper/zookeeper/bin/../lib/jackson-databind-2.10.5.1.jar:/opt/zookeeper/zookeeper/bin/../lib/jackson-core-2.10.5.jar:/opt/zookeeper/zookeeper/bin/../lib/jackson-annotations-2.10.5.jar:/opt/zookeeper/zookeeper/bin/../lib/commons-cli-1.4.jar:/opt/zookeeper/zookeeper/bin/../lib/audience-annotations-0.12.0.jar:/opt/zookeeper/zookeeper/bin/../zookeeper-*.jar:/opt/zookeeper/zookeeper/bin/../zookeeper-server/src/main/resources/lib/*.jar:/opt/zookeeper/zookeeper/bin/../conf:
2021-06-11 21:40:45,056 [myid:] - INFO  [main:Environment@98] - Client environment:java.library.path=/usr/java/packages/lib:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib
2021-06-11 21:40:45,057 [myid:] - INFO  [main:Environment@98] - Client environment:java.io.tmpdir=/tmp
2021-06-11 21:40:45,057 [myid:] - INFO  [main:Environment@98] - Client environment:java.compiler=
2021-06-11 21:40:45,057 [myid:] - INFO  [main:Environment@98] - Client environment:os.name=Linux
2021-06-11 21:40:45,057 [myid:] - INFO  [main:Environment@98] - Client environment:os.arch=amd64
2021-06-11 21:40:45,057 [myid:] - INFO  [main:Environment@98] - Client environment:os.version=5.10.0-3-amd64
2021-06-11 21:40:45,057 [myid:] - INFO  [main:Environment@98] - Client environment:user.name=zookeeper
2021-06-11 21:40:45,057 [myid:] - INFO  [main:Environment@98] - Client environment:user.home=/opt/zookeeper
2021-06-11 21:40:45,057 [myid:] - INFO  [main:Environment@98] - Client environment:user.dir=/opt/zookeeper/zookeeper
2021-06-11 21:40:45,057 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.free=4MB
2021-06-11 21:40:45,058 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.max=247MB
2021-06-11 21:40:45,058 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.total=7MB
2021-06-11 21:40:45,062 [myid:] - INFO  [main:ZooKeeper@637] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@59fa1d9b
2021-06-11 21:40:45,066 [myid:] - INFO  [main:X509Util@77] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2021-06-11 21:40:45,072 [myid:] - INFO  [main:ClientCnxnSocket@239] - jute.maxbuffer value is 1048575 Bytes
2021-06-11 21:40:45,079 [myid:] - INFO  [main:ClientCnxn@1726] - zookeeper.request.timeout value is 0. feature enabled=false
2021-06-11 21:40:45,306 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1171] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181.
2021-06-11 21:40:45,308 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1173] - SASL config status: Will not attempt to authenticate using SASL (unknown error)
2021-06-11 21:40:45,324 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1005] - Socket connection established, initiating session, client: /0:0:0:0:0:0:0:1:47100, server: localhost/0:0:0:0:0:0:0:1:2181
2021-06-11 21:40:45,340 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1438] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, session id = 0x3000000d2890023, negotiated timeout = 30000
2021-06-11 21:40:54,347 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@570] - EventThread shut down for session: 0x3000000d2890023
2021-06-11 21:40:54,347 [myid:] - INFO  [main:ZooKeeper@1232] - Session: 0x3000000d2890023 closed
2021-06-11 21:40:54,349 [myid:] - ERROR [main:ServiceUtils@42] - Exiting JVM with code 0

You can also configure null appender.

$ sudo -u zookeeper cat /opt/zookeeper/zookeeper/conf/log4j.properties 
[...]

# Null Appender
log4j.appender.NullAppender=org.apache.log4j.varia.NullAppender

Just ignore these messages.

$ sudo -u zookeeper ZOO_LOG4J_PROP="INFO,NullAppender" /opt/zookeeper/zookeeper/bin/zkCli.sh
/usr/bin/java
Connecting to localhost:2181
Welcome to ZooKeeper!
JLine support is enabled

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] ls -R /
/
/zookeeper
/zookeeper/config
/zookeeper/quota
[zk: localhost:2181(CONNECTED) 1] quit

WATCHER::

WatchedEvent state:Closed type:None path:null

Send non-existing command to display help.

$ sudo -u zookeeper ZOO_LOG4J_PROP="INFO,NullAppender" /opt/zookeeper/zookeeper/bin/zkCli.sh help
/usr/bin/java
Connecting to localhost:2181
ZooKeeper -server host:port -client-configuration properties-file cmd args
        addWatch [-m mode] path # optional mode is one of [PERSISTENT, PERSISTENT_RECURSIVE] - default is PERSISTENT_RECURSIVE
        addauth scheme auth
        close 
        config [-c] [-w] [-s]
        connect host:port
        create [-s] [-e] [-c] [-t ttl] path [data] [acl]
        delete [-v version] path
        deleteall path [-b batch size]
        delquota [-n|-b|-N|-B] path
        get [-s] [-w] path
        getAcl [-s] path
        getAllChildrenNumber path
        getEphemerals path
        history 
        listquota path
        ls [-s] [-w] [-R] path
        printwatches on|off
        quit 
        reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*]
        redo cmdno
        removewatches path [-c|-d|-a] [-l]
        set [-s] [-v version] path data
        setAcl [-s] [-v version] [-R] path acl
        setquota -n|-b|-N|-B val path
        stat [-w] path
        sync path
        version 
        whoami 
Command not found: Command not found help

Send single command.

$ sudo -u zookeeper ZOO_LOG4J_PROP="INFO,NullAppender" /opt/zookeeper/zookeeper/bin/zkCli.sh version
/usr/bin/java
Connecting to localhost:2181
ZooKeeper CLI version: 3.7.0-e3704b390a6697bfdf4b0bef79e3da7a4f6bac4b, built on 2021-03-17 09:46 UTC

Send command to specific server.

$ sudo -u zookeeper ZOO_LOG4J_PROP="INFO,NullAppender" /opt/zookeeper/zookeeper/bin/zkCli.sh -server zookeeper1.example.org whoami
/usr/bin/java
Connecting to zookeeper1.example.org

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
Auth scheme: User
ip: 172.16.0.113

Send multiple commands.

$ sudo -u zookeeper ZOO_LOG4J_PROP="INFO,NullAppender" /opt/zookeeper/zookeeper/bin/zkCli.sh -server zookeeper1.example.org <<EOF
create /application
create /application/color "blue"
ls /application
get /application/color
delete /application/color
delete /application
quit
EOF
/usr/bin/java
Connecting to zookeeper1.example.org
Welcome to ZooKeeper!
JLine support is enabled

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: zookeeper1.example.org(CONNECTED) 0] create /application
Created /application
[zk: zookeeper1.example.org(CONNECTED) 1] create /application/color "blue"
Created /application/color
[zk: zookeeper1.example.org(CONNECTED) 2] ls /application
[color]
[zk: zookeeper1.example.org(CONNECTED) 3] get /application/color
blue
[zk: zookeeper1.example.org(CONNECTED) 4] delete /application/color
[zk: zookeeper1.example.org(CONNECTED) 5] delete /application
[zk: zookeeper1.example.org(CONNECTED) 6] quit

WATCHER::

WatchedEvent state:Closed type:None path:null