linux install and use zookeeper
First, the installation conditions
Want to install zookeeper, you must first install the jdk in linux. See installation steps:
https://www.cnblogs.com/expiator/p/9987351.html
Second, download and unzip the archive zookeeper
1. First enter / usr / local / directory can also be other directories:
[root@localhost /]# cd /usr/local
2. zookeeper installation package can be downloaded from the official website.
You can also download later this address http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz
If the link does not work, you first open http://mirror.bit.edu.cn/apache/zookeeper , and then select the version.
In this directory zookeeper download the installation package:
[root@localhost local]# wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz
3. Extract:
[root@localhost local]# tar -zxvf zookeeper-3.4.13.tar.gz
Third, edit the configuration file
1. Go to the conf directory:
[root@localhost local]# cd zookeeper-3.4.13/conf
2. Copy this file to the zoo_sample.cfg zoo.cfg (must be the file name)
[root@localhost conf]# cp zoo_sample.cfg zoo.cfg
3. Go zoo.cfg file for editing
[root@localhost conf]# vim zoo.cfg
4. Press i to enter the edit mode, the following modifications:
dataDir=/tmp/zookeeper/data dataLogDir=/tmp/zookeeper/log
Note: If you want to configure a cluster, please add the server ip in clientPort below. Such as
server.1=192.168.180.132:2888:3888
server.2=192.168.180.133:2888:3888
server.3 = 192.168.180.134: 2888: 3888
If the computer memory is relatively small, zookeeper may be provided to pseudo cluster. That is, all servers using the same ip, but use a different port.
5. Create a directory in the tmp directory.
[root@localhost conf]# mkdir /tmp/zookeeper
[root@localhost conf]# mkdir /tmp/zookeeper/data
[root@localhost conf]# mkdir /tmp/zookeeper/log
6. If you are configuring a cluster, you also need to add myid configuration file in front of the path had dataDir
[root@localhost conf]# cd /tmp/zookeeper/data
[root@localhost data]# touch myid
[root@localhost data]# vim myid
Create a file in the data directory, a file named "myid", edit the "myid" file and enter the corresponding number on the corresponding IP of the machine.
As in 192.168.180.132, "myid" file content is 1. On 192.168.180.133, content is 2.
Fourth, configure the environment variables
1. After the above operations to finish up, we need to configure the environment variables, command configuration environment variable as follows:
[root@localhost zookeeper-3.4.13]# export ZOOKEEPER_INSTALL=/usr/local/zookeeper-3.4.13/ [root@localhost zookeeper-3.4.13]# export PATH=$PATH:$ZOOKEEPER_INSTALL/bin
Fifth, start zookeeper
1. Go to the bin directory, and start zookeep. If it is not executed in the bin directory, will complain when you start zookeeper: bash: ./zkServer.sh: No such file or directory
Note:. ./ZkServer.sh earlier start can not be ignored.
[root@localhost local]# cd /usr/local/zookeeper-3.4.13/bin [root@localhost bin]# ./zkServer.sh start
2. Start the successful results are as follows:
ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper-3.4.13/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
After 3.zookeeper the server side, start, also you need to start zookeeper clients:
[root@localhost bin]# ./zkCli.sh
If the connection of a plurality of different host node, with the following command:
./zkCli.sh -server 192.168.180.132:2888
Start successfully effect is as follows:
Connecting to localhost:2181
..........
..........
..........
Welcome to ZooKeeper! 2018-10-25 21:04:54,407 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1029] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error) JLine support is enabled 2018-10-25 21:04:54,471 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@879] - Socket connection established to localhost/0:0:0:0:0:0:0:1:2181, initiating session [zk: localhost:2181(CONNECTING) 0] 2018-10-25 21:04:54,501 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1303] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x10000712e6f0000, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:None path:null
4. View status:
[root@localhost bin]# ./zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper-3.4.13/bin/../conf/zoo.cfg Mode: standalone
I encountered a problem how to solve?
zookeeper's error log will be recorded in zookeeper.out.
Which is currently in the directory, after executing zkServer.sh start command, zookeeper.out which will be written in the directory.
vim zookeeper.out can view the error message. Then search solution.
Six, zookeeper use
After entering through ./zkCli.sh client, you can use commands to operate the zookeeper.
1. Create a node
Use the create command, you can create a zookeeper node.
create [-s] [-e] path data acl
Wherein a sequence node -s, -e temporary node represents. By default, creating an enduring node.
path is a path node, data is node data, acl is used for access control.
as follows:
Create called / zk-test node, the content is "123"
[zk: localhost:2181(CONNECTED) 0] create /zk-test 123
Created /zk-test
Creating / zk-test child nodes book, the content is "233"
[zk: localhost:2181(CONNECTED) 7] create /zk-test/book 233 Created /zk-test/book
2. Check node content
Use get command, you can get content and attribute information zookeeper specified node.
as follows:
[zk: localhost:2181(CONNECTED) 1] get /zk-test
123 cZxid = 0x3a ctime = Sun Nov 11 21:50:44 CST 2018 mZxid = 0x3a mtime = Sun Nov 11 21:50:44 CST 2018 pZxid = 0x3a cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 3 numChildren = 0
3. Review the child node
Use the ls command to see all the child nodes in the specified node
View the root directory of all child nodes:
[zk: localhost:2181(CONNECTED) 2] ls /
[zk-test, zookeeper]
Check the child nodes zk-test node:
[zk: localhost:2181(CONNECTED) 3] ls /zk-test
[book]
4. Update node content
Use the set command to update the contents of the node. The format is:
set path data
In which the data is to be updated new content.
[zk: localhost:2181(CONNECTED) 4] set /zk-test 456 cZxid = 0x3a ctime = Sun Nov 11 21:50:44 CST 2018 mZxid = 0x3b mtime = Sun Nov 11 22:05:20 CST 2018 pZxid = 0x3a cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 3 numChildren = 0
In the output information can be found, dataVersion value increased from 0 into a 1, because just updated version of the operating result in data node changes have taken place.
6. Delete Node
Use the delete command to delete a node, as follows:
[zk: localhost:2181(CONNECTED) 11] delete /zk-test
Node not empty: /zk-test
Can be found when there is a child node of the node, the node can not be deleted.
Removes the child node / zk-test / book, as follows:
[zk: localhost:2181(CONNECTED) 12] delete /zk-test/book WATCHER:: WatchedEvent state:SyncConnected type:NodeDeleted path:/zk-test/book
watcher monitors node in the zookeeper, when the child node changes will be notified. A prompt the child node / zk-test / book deleted successfully.
Continues to try to delete a node / zk-test,
[zk: localhost:2181(CONNECTED) 13] ls /zk-test [] [zk: localhost:2181(CONNECTED) 14] delete /zk-test [zk: localhost:2181(CONNECTED) 15] ls / []
successfully deleted.
References:
"Principles and Practice from Paxos to zookeeper distributed consensus"
First, the installation conditions
Want to install zookeeper, you must first install the jdk in linux. See installation steps:
https://www.cnblogs.com/expiator/p/9987351.html
Second, download and unzip the archive zookeeper
1. First enter / usr / local / directory can also be other directories:
[root@localhost /]# cd /usr/local
2. zookeeper installation package can be downloaded from the official website.
You can also download later this address http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz
If the link does not work, you first open http://mirror.bit.edu.cn/apache/zookeeper , and then select the version.
In this directory zookeeper download the installation package:
[root@localhost local]# wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz
3. Extract:
[root@localhost local]# tar -zxvf zookeeper-3.4.13.tar.gz
Third, edit the configuration file
1. Go to the conf directory:
[root@localhost local]# cd zookeeper-3.4.13/conf
2. Copy this file to the zoo_sample.cfg zoo.cfg (must be the file name)
[root@localhost conf]# cp zoo_sample.cfg zoo.cfg
3. Go zoo.cfg file for editing
[root@localhost conf]# vim zoo.cfg
4. Press i to enter the edit mode, the following modifications:
dataDir=/tmp/zookeeper/data dataLogDir=/tmp/zookeeper/log
Note: If you want to configure a cluster, please add the server ip in clientPort below. Such as
server.1=192.168.180.132:2888:3888
server.2=192.168.180.133:2888:3888
server.3 = 192.168.180.134: 2888: 3888
If the computer memory is relatively small, zookeeper may be provided to pseudo cluster. That is, all servers using the same ip, but use a different port.
5. Create a directory in the tmp directory.
[root@localhost conf]# mkdir /tmp/zookeeper
[root@localhost conf]# mkdir /tmp/zookeeper/data
[root@localhost conf]# mkdir /tmp/zookeeper/log
6. If you are configuring a cluster, you also need to add myid configuration file in front of the path had dataDir
[root@localhost conf]# cd /tmp/zookeeper/data
[root@localhost data]# touch myid
[root@localhost data]# vim myid
Create a file in the data directory, a file named "myid", edit the "myid" file and enter the corresponding number on the corresponding IP of the machine.
As in 192.168.180.132, "myid" file content is 1. On 192.168.180.133, content is 2.
Fourth, configure the environment variables
1. After the above operations to finish up, we need to configure the environment variables, command configuration environment variable as follows:
[root@localhost zookeeper-3.4.13]# export ZOOKEEPER_INSTALL=/usr/local/zookeeper-3.4.13/ [root@localhost zookeeper-3.4.13]# export PATH=$PATH:$ZOOKEEPER_INSTALL/bin
Fifth, start zookeeper
1. Go to the bin directory, and start zookeep. If it is not executed in the bin directory, will complain when you start zookeeper: bash: ./zkServer.sh: No such file or directory
Note:. ./ZkServer.sh earlier start can not be ignored.
[root@localhost local]# cd /usr/local/zookeeper-3.4.13/bin [root@localhost bin]# ./zkServer.sh start
2. Start the successful results are as follows:
ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper-3.4.13/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
After 3.zookeeper the server side, start, also you need to start zookeeper clients:
[root@localhost bin]# ./zkCli.sh
If the connection of a plurality of different host node, with the following command:
./zkCli.sh -server 192.168.180.132:2888
Start successfully effect is as follows:
Connecting to localhost:2181
..........
..........
..........
Welcome to ZooKeeper! 2018-10-25 21:04:54,407 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1029] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error) JLine support is enabled 2018-10-25 21:04:54,471 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@879] - Socket connection established to localhost/0:0:0:0:0:0:0:1:2181, initiating session [zk: localhost:2181(CONNECTING) 0] 2018-10-25 21:04:54,501 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1303] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x10000712e6f0000, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:None path:null
4. View status:
[root@localhost bin]# ./zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper-3.4.13/bin/../conf/zoo.cfg Mode: standalone
I encountered a problem how to solve?
zookeeper's error log will be recorded in zookeeper.out.
Which is currently in the directory, after executing zkServer.sh start command, zookeeper.out which will be written in the directory.
vim zookeeper.out can view the error message. Then search solution.
Six, zookeeper use
After entering through ./zkCli.sh client, you can use commands to operate the zookeeper.
1. Create a node
Use the create command, you can create a zookeeper node.
create [-s] [-e] path data acl
Wherein a sequence node -s, -e temporary node represents. By default, creating an enduring node.
path is a path node, data is node data, acl is used for access control.
as follows:
Create called / zk-test node, the content is "123"
[zk: localhost:2181(CONNECTED) 0] create /zk-test 123
Created /zk-test
Creating / zk-test child nodes book, the content is "233"
[zk: localhost:2181(CONNECTED) 7] create /zk-test/book 233 Created /zk-test/book
2. Check node content
Use get command, you can get content and attribute information zookeeper specified node.
as follows:
[zk: localhost:2181(CONNECTED) 1] get /zk-test
123 cZxid = 0x3a ctime = Sun Nov 11 21:50:44 CST 2018 mZxid = 0x3a mtime = Sun Nov 11 21:50:44 CST 2018 pZxid = 0x3a cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 3 numChildren = 0
3. Review the child node
Use the ls command to see all the child nodes in the specified node
View the root directory of all child nodes:
[zk: localhost:2181(CONNECTED) 2] ls /
[zk-test, zookeeper]
Check the child nodes zk-test node:
[zk: localhost:2181(CONNECTED) 3] ls /zk-test
[book]
4. Update node content
Use the set command to update the contents of the node. The format is:
set path data
In which the data is to be updated new content.
[zk: localhost:2181(CONNECTED) 4] set /zk-test 456 cZxid = 0x3a ctime = Sun Nov 11 21:50:44 CST 2018 mZxid = 0x3b mtime = Sun Nov 11 22:05:20 CST 2018 pZxid = 0x3a cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 3 numChildren = 0
In the output information can be found, dataVersion value increased from 0 into a 1, because just updated version of the operating result in data node changes have taken place.
6. Delete Node
Use the delete command to delete a node, as follows:
[zk: localhost:2181(CONNECTED) 11] delete /zk-test
Node not empty: /zk-test
Can be found when there is a child node of the node, the node can not be deleted.
Removes the child node / zk-test / book, as follows:
[zk: localhost:2181(CONNECTED) 12] delete /zk-test/book WATCHER:: WatchedEvent state:SyncConnected type:NodeDeleted path:/zk-test/book
watcher monitors node in the zookeeper, when the child node changes will be notified. A prompt the child node / zk-test / book deleted successfully.
Continues to try to delete a node / zk-test,
[zk: localhost:2181(CONNECTED) 13] ls /zk-test [] [zk: localhost:2181(CONNECTED) 14] delete /zk-test [zk: localhost:2181(CONNECTED) 15] ls / []
successfully deleted.
References:
"Principles and Practice from Paxos to zookeeper distributed consensus"