Zookeeper common commands

1, Start / connect / exit / close / status

1. Start Zookeeper SERVER background

  ./zkServer.sh start

2. Connect client

  ./zkCli.sh

3. Exit client

  quit

4. Close Zookeeper background service

  ./zkServer.sh stop

5. View the status of Zookeeper background service

  ./ zkServer.sh status (I don't have a cluster here, so the display mode is standalone. If it's a cluster, it's the leader)

 

2, Check whether the background starts normally

Mode 1: ps -ef | grep zookeeper | grep -v grep

Mode 2: zoomeeper command

echo ruok | nc Linux ip address zookeeper port number (the default port number is 2181. If no nc command is displayed, use yum install nc to install)

If it is shown below, it means that the zookeeper background is started successfully

 

3, Common commands for zookeeper client

Log in to the zookeeper client and use the help command to view the client commands of zookeepr

1. ls path [watch]: view the zookeeper node. By default, there is only one zookeeper node

2. create [-s] [-e] path data acl: create a node. Data represents data and acl represents permission control

- s: sequence node. The sequence is cumulative. It is provided by zookeeper

- e: temporary node. The node will disappear after the server is disconnected and reconnected

// View the child nodes under the root node
[zk: localhost:2181(CONNECTED) 20] ls /
[zookeeper]
// Create a temporary node under znode01
[zk: localhost:2181(CONNECTED) 21] create -e /znode01 001
Created /znode01
// Create a temporary order node znode02 (the order is cumulative) under the root node
[zk: localhost:2181(CONNECTED) 22] create -e -s /znode02 002
// Create a permanent node znode03 under the root node
Created /znode020000000005
[zk: localhost:2181(CONNECTED) 23] create /znode03 003
Created /znode03
// Create a permanent sequential node under the root node
[zk: localhost:2181(CONNECTED) 24] create -s /znode04 004
Created /znode040000000007

After disconnecting the zookeeper background service and connecting again, you can see that the temporary nodes znode01 and znode02 under the root node have disappeared, but znode03 and znode04 continue to exist because they are persistent nodes

3. Get: path [watch]: get the value of the node

// Get all child nodes under the root node (/)
[zk: localhost:2181(CONNECTED) 3] ls /    
// Under the root node, there are three child nodes, zookeeper (the default built-in node of zookeeper, znode03 and znode040000000007)   
[znode040000000007, zookeeper, znode03]
// Get the value of node / znode03
[zk: localhost:2181(CONNECTED) 4] get /znode03
// /Value of znode03 node
003
//========================These are the state bodies (STATs)========================
// Transaction id of the creation node
cZxid = 0x16
// When the node was created
ctime = Wed Sep 30 19:05:02 CST 2020
// Modify node id
mZxid = 0x16
// Time of modification
mtime = Wed Sep 30 19:05:02 CST 2020
// id of parent node
pZxid = 0x16
// Version created
cversion = 0
// Version of data
dataVersion = 0
// Version of permission
aclVersion = 0
// Is it a temporary node
ephemeralOwner = 0x0
// Length of data
dataLength = 3
// Number of child nodes
numChildren = 0

4. Get the state of the node body [path stat]:

5. ls2 path [watch]:ls2 is equivalent to LS + stat. LS only displays the child nodes under the node and does not display the state body of the node. However, ls2 not only displays the child nodes of the node, but also displays the state body information of the node

6. set path data [version]: modify the value of the node

[zk: localhost:2181(CONNECTED) 17] get /znode03
// The initial value is 003
003
cZxid = 0x16
ctime = Wed Sep 30 19:05:02 CST 2020
mZxid = 0x16
mtime = Wed Sep 30 19:05:02 CST 2020
pZxid = 0x16
cversion = 0
// Data version 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
// Modify / znode03 node data
[zk: localhost:2181(CONNECTED) 18] set /znode03 10086
cZxid = 0x16
ctime = Wed Sep 30 19:05:02 CST 2020
mZxid = 0x1c
mtime = Wed Sep 30 22:06:02 CST 2020
pZxid = 0x16
cversion = 0
// The data version number becomes 1
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0
// Get the current version of / znode03 node data
[zk: localhost:2181(CONNECTED) 19] get /znode03
// The discovery data has been modified to 10086
10086
cZxid = 0x16
ctime = Wed Sep 30 19:05:02 CST 2020
mZxid = 0x1c
mtime = Wed Sep 30 22:06:02 CST 2020
pZxid = 0x16
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0
// Modify the value according to the version number
[zk: localhost:2181(CONNECTED) 20] set /znode03 10001 1
cZxid = 0x16
ctime = Wed Sep 30 19:05:02 CST 2020
mZxid = 0x1d
mtime = Wed Sep 30 22:08:11 CST 2020
pZxid = 0x16
cversion = 0
// After the modification, the data version becomes 2
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0
// Get the data of the current version / znode03 node
[zk: localhost:2181(CONNECTED) 21] get /znode03
// The data becomes 10001
10001
cZxid = 0x16
ctime = Wed Sep 30 19:05:02 CST 2020
mZxid = 0x1d
mtime = Wed Sep 30 22:08:11 CST 2020
pZxid = 0x16
cversion = 0
// At this time, the data version has become 2
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0
// Continue to modify the value according to the data version
[zk: localhost:2181(CONNECTED) 22] set /znode03 13579 1
// Similar to optimistic lock, because the current version is already 2. If you continue to modify the value according to version 1, you will be prompted that the version of the node is invalid
version No is not valid : /znode03

7. Delete nodes. There are two ways to delete nodes in zookeeper

delete path [version]: similar to deleting a file in Linux

rmr path: similar to recursively deleting folders

// Displays all child nodes under the root node
[zk: localhost:2181(CONNECTED) 24] ls /
[znode040000000007, zookeeper, znode03]
// Delete / znode03 node
[zk: localhost:2181(CONNECTED) 25] delete /znode03
// Delete / znode040000000007 node
[zk: localhost:2181(CONNECTED) 26] delete /znode040000000007
// It can be seen that nodes znode03 and znode040000000007 have been deleted
[zk: localhost:2181(CONNECTED) 27] ls /
[zookeeper]
// Create / testNode node
[zk: localhost:2181(CONNECTED) 31] create /testNode testNode
Created /testNode
[zk: localhost:2181(CONNECTED) 32] ls /
[zookeeper, testNode]
// Create / testNode/node1, / testNode/node2, / testNode/node3 nodes
[zk: localhost:2181(CONNECTED) 33] create /testNode/node1 001
Created /testNode/node1
[zk: localhost:2181(CONNECTED) 34] create /testNode/node2 002
Created /testNode/node2
[zk: localhost:2181(CONNECTED) 35] create /testNode/node3 003
Created /testNode/node3
[zk: localhost:2181(CONNECTED) 36] ls /testNode
[node2, node3, node1]
// Delete parent node / testNode
[zk: localhost:2181(CONNECTED) 37] rmr /testNode
// The parent node / testNode has been deleted
[zk: localhost:2181(CONNECTED) 38] ls /

 

4, ACL permission control

ZK nodes have five operation permissions: CREATE, DELETE, READ, WRITE, and ADMIN, which correspond to add, DELETE, query, modify, and manage; The abbreviation of these five permissions is cdrwa (the acronym of each word)
Note: of the five permissions, DELETE refers to the permission to DELETE child nodes, and the other four refer to the operation permission to their own nodes

There are four ways of identity authentication:
world: the default mode, which means that anyone can access it
auth: represents the authenticated user (in cli, you can add the authorized user in the current context through addauth digest user:pwd)
digest: user name: password authentication, which is also the most commonly used method in business systems
IP: use IP address authentication

Use [scheme:id:permissions] to represent acl permissions

1. getAcl path: get the Acl permission information of a node

// Displays all child nodes under the root path	
[zk: localhost:2181(CONNECTED) 10] ls /
[zookeeper, testNode]
// Get acl permission of node / testNode
[zk: localhost:2181(CONNECTED) 11] getAcl /testNode
// The authentication type is world, which means that anyone can access it
'world,'anyone
// The permissions include CREATE, DELETE, READ, WRITE and ADMIN
: cdrwa

2. setAcl path: set the Acl permission of a node

// View child nodes under the root node
[zk: localhost:2181(CONNECTED) 10] ls /
[zookeeper, testNode]
// Get acl permission information of node / testNode
[zk: localhost:2181(CONNECTED) 11] getAcl /testNode
// Default permission: anyone can perform cdrwa operations
'world,'anyone
: cdrwa
[zk: localhost:2181(CONNECTED) 12] ls /
[zookeeper, testNode]
[zk: localhost:2181(CONNECTED) 13] ls /testNode
[]
// Create two child nodes node1 and node2 under node / testNode
[zk: localhost:2181(CONNECTED) 14] create /testNode/node1 001
Created /testNode/node1
[zk: localhost:2181(CONNECTED) 15] create /testNode/node2 002
Created /testNode/node2
[zk: localhost:2181(CONNECTED) 16] ls /
[zookeeper, testNode]
[zk: localhost:2181(CONNECTED) 17] ls /testNode
[node2, node1]
// Set the acl permission of node / testNode to crwa without DELETE, that is, the child node cannot be deleted
[zk: localhost:2181(CONNECTED) 18] setAcl /testNode world:anyone:crwa
cZxid = 0x2b
ctime = Thu Oct 01 00:23:31 CST 2020
mZxid = 0x2b
mtime = Thu Oct 01 00:23:31 CST 2020
pZxid = 0x2e
cversion = 2
dataVersion = 0
aclVersion = 1
ephemeralOwner = 0x0
dataLength = 7
numChildren = 2
// Get the acl permission information of node / testNode. It can be seen that it is crwa without DELETE
[zk: localhost:2181(CONNECTED) 19] getAcl /testNode
'world,'anyone
: crwa
// The node1 prompt for deleting the child node of / testNode is invalid (because the permission of / testNode is crwa and there is no DELETE, that is, the child node cannot be deleted
[zk: localhost:2181(CONNECTED) 20] delete /testNode/node1
Authentication is not valid : /testNode/node1
// Change the permission of / testNode node to cdrw
[zk: localhost:2181(CONNECTED) 21] setAcl /testNode world:anyone:cdrw
cZxid = 0x2b
ctime = Thu Oct 01 00:23:31 CST 2020
mZxid = 0x2b
mtime = Thu Oct 01 00:23:31 CST 2020
pZxid = 0x2e
cversion = 2
dataVersion = 0
aclVersion = 2
ephemeralOwner = 0x0
dataLength = 7
numChildren = 2
[zk: localhost:2181(CONNECTED) 22] getAcl /testNode
'world,'anyone
: cdrw
// Delete the child node node1 of / testNode
[zk: localhost:2181(CONNECTED) 23] delete /testNode/node1
// Check all the child nodes under the / testNode node. You can see that the child node node1 has been deleted
[zk: localhost:2181(CONNECTED) 24] ls /testNode
[node2]

  

5, Zookeeper quadword command

After starting the Zookeeper background service, you can view the relevant information of Zookeeper through the four word command of Zookeeper

1. Check the status information with stat
2. ruok check whether zookeeper is started
3. dump lists unprocessed nodes and temporary nodes
4. conf view server configuration
5. cons displays the information connected to the server
6. envi displays environment variable information
7. mntr check zk's health information
8. wchs displays the information of watch
9. wchc and wchp display the watch information of session and the watch information of path

 

 

Reprinted from: https://blog.csdn.net/dandandeshangni/article/details/80558383

 

Tags: Zookeeper

Posted by lbraine on Fri, 13 May 2022 17:03:21 +0300