Zookeeper05 zookeeper related commands

1,zkCli. How to use sh script

  • Official Manual: https://zookeeper.apache.org/doc/current/zookeeperCLI.html

1.1. Connect and exit ZooKeeper server

1. Connect to ZooKeeper server

  • The syntax of using ZooKeeper client (zkCli.sh) to connect to ZooKeeper server is:
zkCli.sh -timeout 5000 -r -server ip1:port1[,ip2:port2,ip3:port3]
    -timeout: Indicates that the client sends ZooKeeper The interval, in milliseconds, at which the server sends heartbeats. Because the connection status between the client and the server is maintained through heartbeat detection, if the client does not send heartbeat packets to the server within the specified time interval, the server will disconnect from the client. Parameter 5000 indicates that the interval between the client sending heartbeat to the server is 5 seconds.
    -r: Indicates that the client is connected in read-only mode
    -server: Specify the server to which the client will connect IP The default port of the server is 2181

Example:

]# zkCli.sh    #Equivalent to zkcli sh -server 127.0.0.1:2181

]# zkCli.sh -timeout 5000 -r -server 10.1.1.10:2181

--Will be randomly connected to the next three ZooKeeper One of the servers
]# zkCli.sh -server 10.1.1.10:2181,10.1.1.11:2181,10.1.1.12:2181

2. Close connection

  • The close command closes the current client connection
[zk: localhost:2181(CONNECTED) 0] close

WATCHER::

WatchedEvent state:Closed type:None path:null
2021-12-26 21:48:31,454 [myid:] - INFO  [main:ZooKeeper@1422] - Session: 0x100001117980000 closed
2021-12-26 21:48:31,459 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@524] - EventThread shut down for session: 0x100001117980000

3. Connect to ZooKeeper server

connect [host:port]
    host:port: ZooKeeper The server IP And ports

Example:

[zk: localhost:2181(CLOSED) 1] connect 127.0.0.1:2181

4. Exit zkcli SH terminal

[zk: 127.0.0.1:2181(CONNECTED) 2] quit

WATCHER::

WatchedEvent state:Closed type:None path:null
2021-12-26 21:49:06,266 [myid:] - INFO  [main:ZooKeeper@1422] - Session: 0x100001117980001 closed
2021-12-26 21:49:06,269 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@524] - EventThread shut down for session: 0x100001117980001

1.2. Query znode

1. Query sub znode list

ls [-s] [-w] [-R] path
    path: znode route
    -s: display path Details of status (metadata)
    -w: monitor path Son of znode List changes (if any) path skillful or capable znode The monitoring point will be triggered if the number of is changed)
    -R: Recursive displayer znode

--ls2 Deprecated
ls2 path [watch]

Example:

[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] ls -s /
[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

[zk: localhost:2181(CONNECTED) 2] ls -R /
/
/zookeeper
/zookeeper/config
/zookeeper/quota
[zk: localhost:2181(CONNECTED) 3] ls -w /
[zookeeper]

2. Get znode status

stat [-w] path
    path: znode route
    -w: monitor path znode Status (metadata) change of

Example:

[zk: localhost:2181(CONNECTED) 8] stat /test1
cZxid = 0x100000011                     --cZxid: establish znode Transaction id
ctime = Sun Dec 26 19:33:43 CST 2021    --ctime: establish znode Time
mZxid = 0x100000011                     --mZxid: modify znode Transaction id
mtime = Sun Dec 26 19:33:43 CST 2021    --mtime: modify znode Time
pZxid = 0x100000011                     --pZxid: son znode List last modified transactions id. Delete or add child znode,No modifiers included znode Data.
cversion = 0                            --cversion: son znode Version number of, delete or add child znode,The version number will increase automatically
dataVersion = 0                         --dataVersion: znode Data version number, data writing operation, and the version number will be incremented
aclVersion = 0                          --aclVersion: znode ACL Permission version, permission write operation, and the version number will be incremented
ephemeralOwner = 0x0                    --ephemeralOwner: temporary znode Transaction at creation id,If it's permanent znode,Then its value is 0
dataLength = 0                          --dataLength: znode Data length (unit: byte),Chinese accounts for 3 byte
numChildren = 0                         --numChildren: son znode quantity

1.3. Create and delete znode

1. Create znode

create [-s] [-e] [-c] [-t ttl] path [data] [acl]
    path: znode route
    -s: Create order znode. (Automatically ordered znode (add number)
    -e: Create temporary znode,The default is persistent znode. (When a session is disconnected, the session's temporary znode (deleted)
    -c: Create container znode. When the last child of a container is deleted, the container is deleted
    -t: establish TTL Node. Need in zoo.cfg Medium setting zookeeper.extendedTypesEnabled=true
    data: store in znode Data in
    acl: Setting sub znode Access permission. By default, everyone can access the znode Read and write

Example:

--Create persistence znode
[zk: localhost:2181(CONNECTED) 0] create /test1
Created /test1
--Create temporary znode
[zk: localhost:2181(CONNECTED) 1] create -e /test2
Created /test2
--Create a persistent and orderly znode
[zk: localhost:2181(CONNECTED) 2] create -s /test3
Created /test30000000001
--Create temporary ordered znode
[zk: localhost:2181(CONNECTED) 3] create -e -s /test4
Created /test40000000001

2. Delete znode

--delete znode,Should znode You can't have children znode
delete [-v version] path
    path: znode route
    -v: znode Version number( znode state cversion Value of). If the version number is passed, it must be consistent with the version number on the server, otherwise an error will be reported: version No is not valid

--delete znode,Include its children znode(Deletes the child recursively znode,Delete again znode Itself)
deleteall path

--Deprecated
rmr path

Example:

[zk: localhost:2181(CONNECTED) 0] delete /test1
[zk: localhost:2181(CONNECTED) 1] delete /test2
Node not empty: /test2
[zk: localhost:2181(CONNECTED) 2] deleteall /test2

1.4. View and set znode data

1. Get znode data

get [-s] [-w] path
    path: znode route
    -s: display path Details of status (metadata)
    -w: monitor path znode Data changes in

2. Set znode data

set [-s] [-v version] path data
    path: znode route
    data: znode data
    -s: display path Details of status (metadata)
    -v: Data version number( znode state dataVersion Value of). If the version number is passed, it must be consistent with the version number on the server, otherwise an error will be reported.

1.5. znode quota

1. Query znode quota

listquota path 
    path: znode route

2. Set znode quota

  • Znode can store data or create child znodes. However, without control, the data of znode can be unlimited and the number of child znodes can be created. Therefore, in some scenarios, some restrictions need to be made on the data of znode and the number of child znodes. zk provides us with the setauota command to limit the number of child znodes. However, zk does not really restrict the znode physically, but if the znode limit is exceeded, a quota overrun warning message will be recorded in zk's log file.
setquota -n|-b val path 
    path: znode route
    -n: Restrictor znode Number of
    -b: limit znode Data length of
    val: If the parameter is-n,val Represents a limiter znode Number of. If the parameter is-b,val Representation limit znode Data length

3. Delete znode quota

delquota [-n|-b] path 
    path: znode route
    -n: Delete child znode Quantity quota limit
    -b: delete znode Data length quota limit

Example:

[zk: localhost:2181(CONNECTED) 0] create /test1
Created /test1
--see/test1 Quota of
[zk: localhost:2181(CONNECTED) 1] listquota /test1 
absolute path is /zookeeper/quota/test1/zookeeper_limits
quota for /test1 does not exist.
--set up/test1 Quota of
[zk: localhost:2181(CONNECTED) 2] setquota -n 2 /test1
[zk: localhost:2181(CONNECTED) 3] listquota /test1 
absolute path is /zookeeper/quota/test1/zookeeper_limits
Output quota for /test1 count=2,bytes=-1    --express znode Quota information, limit this znode There are two children at most znode,znode Data is-1,Indicates no restriction
Output stat for /test1 count=1,bytes=0      --Represents the current znode Status information of the znode There is one child znode,znode The data length is 0
--delete/test1 Quota of
[zk: localhost:2181(CONNECTED) 4] delquota /test1
[zk: localhost:2181(CONNECTED) 5] listquota /test1 
absolute path is /zookeeper/quota/test1/zookeeper_limits
quota for /test1 does not exist.

1.6 ACL of znode

1. Get znode ACL

getAcl [-s] path
    path: znode route
    -s: display path Details of status (metadata)

2. Set znode ACL

  • If acl permission is not set when creating a znode, all users can read and write the znode by default.
setAcl [-s] [-v version] [-R] path acl
    path: znode route
    acl: Setting sub znode Access permission. By default, everyone can access the znode Read and write
    -s: display path Details of status (metadata)
    -v: acl Version number( znode state aclVersion Value of). If the version number is passed, it must be consistent with the version number on the server, otherwise an error will be reported.
    -R: Recursive setting acl

Example:

[zk: localhost:2181(CONNECTED) 0] create /test1
Created /test1
[zk: localhost:2181(CONNECTED) 1] getAcl /test1 
'world,'anyone
: cdrwa
[zk: localhost:2181(CONNECTED) 2] setAcl /test1 ip:10.1.1.12:cdrwa
[zk: localhost:2181(CONNECTED) 3] getAcl /test1    --To operate this znode,Login required zkCli.sh -server 10.1.1.12:2181
Authentication is not valid : /test1

1.7 historical orders

1. View history commands

history

2. Execute history command

redo cmdno
    cmdno: Historical command number

Example:

[zk: localhost:2181(CONNECTED) 0] ls /
[test1, zookeeper]
[zk: localhost:2181(CONNECTED) 1] get /zookeeper 
ss
[zk: localhost:2181(CONNECTED) 2] history    --View history commands
0 - ls /
1 - get /zookeeper 
2 - history
[zk: localhost:2181(CONNECTED) 3] redo 0     --Execute historical commands
[test1, zookeeper]

1.8. Server configuration

1. Configuration information

  • Displays the configuration of the quorum member
config [-c] [-w] [-s]

2. Reconfiguration information

  • Change the members of the cluster at run time.
  • precondition:
    • In zoo Set reconfig enabled = true in CFG
    • Add superuser or skipAcl, otherwise you will get "insufficient permission". (e.g. addauth digest zookeeper:admin)
reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*]
    -s: 
    -v: If the version number is passed, it must be consistent with the version number on the server, otherwise an error will be reported.
    -file: Use the file for reconfiguration.
    -members: Full reconfiguration.
    -add...-remove: Incremental reconfiguration.

Example:

--Change server 2 to observer and change its port from 2182 to 12182; Add server 5 to the cluster and set it as an observer; Remove server 4 from the cluster.
reconfig --add 2=localhost:2781:2786:observer;12182 --add 5=localhost:2781:2786:observer;2185 -remove 4

--Full weight reconfiguration
reconfig -members server.1=localhost:2780:2785:participant;0.0.0.0:2181,server.2=localhost:2781:2786:observer;0.0.0.0:12182,server.3=localhost:2782:2787:participant;0.0.0.0:12183

--Change the current configuration to myNewConfig.txt Configuration in
reconfig -file /data/software/zookeeper/zookeeper-test/conf/myNewConfig.txt
    /data/software/zookeeper/zookeeper-test/conf/myNewConfig.txt
    server.1=localhost:2780:2785:participant;0.0.0.0:2181
    server.2=localhost:2781:2786:observer;0.0.0.0:12182
    server.3=localhost:2782:2787:participant;0.0.0.0:2183
    server.5=localhost:2784:2789:observer;0.0.0.0:2185

1.9 other orders

1. Add authorization information to the current client

addauth scheme auth
    scheme: Authorization method
    auth: jurisdiction

2. Synchronize data with leader

  • Synchronize the data of a node between leader and follower (asynchronous synchronization)
sync path
    path: znode route

3. Turn listening log on or off

  • When obtaining znode data, sub znode list and other operations, you can add the watch parameter to monitor the change of znode, so that you can receive a notification when znode data and sub znode list change and output it to the console. The default is on. You can set parameters to turn it off.
printwatches on|off
on: open
off: close

4. Delete listener

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

2. Four letter command

  • The four letter command provides a simple way to perform various checks on the system. Its main purpose is to provide a very simple protocol, which can be used with simple tools (such as telnet and nc) to check system health and diagnose problems.

1,ruok

  • Provides (limited) server status information. If the server is running, it will return the response information of "imok". In fact, the "OK" status is only a relative concept. For example, although the server is running and cannot communicate with other servers in the cluster, the status returned by the server is still "OK". For more detailed and reliable health checks, you need to use the stat command.

2,stat

  • Provides the status information of the server and the current active connection. The status information includes some basic statistics, whether the server is currently active (if it is a leader or follower), and the last known zxid information of the server. Some statistics are cumulative values, which can be reset using the srst command.

3,srvr

  • The information provided is the same as stat, but the connection information is ignored.

4,mntr

  • Provides more detailed statistics than the stat command. The output format of each line is: key < tab > value. (the leader will also list additional parameters for the leader only)

5,dump

  • Provides session information, which lists the currently active session information and the expiration time of these sessions. This command can only be run on the leader.

6,conf

  • Lists the basic configuration parameters used when the server starts.

7,envi

  • Lists various Java environment parameters.

8,wchs

  • Lists a short summary of the monitoring points tracked by the server.

9,wchc

  • Lists the details of the monitoring points tracked by the server, grouped by session.

10,wchp

  • Lists the details of the monitoring points tracked by the server, grouped according to the monitored znode path.

11,cons, crst

  • The cons command lists detailed statistics for each connection on the server. crst resets all connection counters to zero.

Example:

  • The server will refuse to execute the command until the four letter command is turned on.
]# echo conf | nc 127.0.0.1 2181
conf is not executed because it is not in the whitelist.
  • Open the permission to execute four letter commands in zoo Add configuration items to the cfg file:
#Start the four letter command to restart the service
4lw.commands.whitelist=*
  • Use in combination with nc command
]# echo ruok | nc 127.0.0.1 2181
imok
  • Use with telnet command
]# telnet 127.0.0.1 2181
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
ruok                     --Four letter command
imok                     --Output of command
Connection closed by foreign host.

Keywords: Zookeeper

Added by Johnm on Sun, 23 Jan 2022 05:32:28 +0200