Zookeeper is an open-source project of Apache and is widely used in cluster management. Its cluster construction is also very simple. You only need simple configuration. Each node of the cluster will complete self-communication and automatically select the leader.
For more information about zookeeper and its principles, refer to this blog post: zookeeper principles and usage.
The single-Host Mode of zookeeper is simpler: In ubuntu, you only need to run sudo apt-Get install zookeeper to install zookeeper, and then find zkserver under the installation directory. sh script, run. /zkserver. sh start starts the standalone mode of zookeeper. The directory of zookeeper installed in this mode is usually/usr/share/zookeeper /. Run zkserver. Sh status to view the running status of zookeeper. The following figure shows the running status of zookeeper:
JMX enabled by defaultUsing config: /etc/zookeeper/conf/zoo.cfgMode: standalone
For other Linux systems (centos), run the following command:
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gztar zxf zookeeper-3.4.6.tar.gzcd zookeeper-3.4.6/conf/
This directory contains a zoo_sample.cfg file, which is an official example configuration. Run:
cp ./zoo_sample.cfg zoo.cfg
Copy an example configuration and change it to zoo. cfg to start zookeeper:
../bin/zkServer.sh start
Next we will try to build a zookeeper cluster. Due to limited machines, I will build a pseudo cluster on one machine to build a cluster. Zookeeper officially recommends that the number of nodes in the cluster be odd, so we are going to build a cluster with three nodes. First, create three folders to indicate three nodes:
mkdir server0mkdir server1mkdir server2
Decompress the downloaded zookeeper file to each directory:
tar xzvf zookeeper-3.4.6.tar.gz -C ./server0/tar xzvf zookeeper-3.4.6.tar.gz -C ./server1/tar xzvf zookeeper-3.4.6.tar.gz -C ./server2/
Change the directory name at the same time:
mv ./server0/zookeeper-3.4.6 ./server0/zookeepermv ./server1/zookeeper-3.4.6 ./server1/zookeepermv ./server2/zookeeper-3.4.6 ./server2/zookeeper
Then go to the zookeeper directory of each node and create the data directory and logs directory respectively:
mkdir datamkdir logs
Then, create the myid file in the data directory, whose content is the serial number of the current node, such as, 2:
vim ./data/myid
Enter the serial number and save it. Then enter the conf directory to create the zoo. cfg file:
vim ./zoo.cfg
Enter the content as follows: ticktime = 2000
Initlimit = 5
Synclimit = 2
Datadir =/opt/server0/zookeeper/Data
Datalogdir =/opt/server0/zookeeper/logs
Clientport = 2180
Server.0 = MAID: 8880: 7770
Server .1 = 127.0.0.1: 8881: 7771
Server.2 = 127.0.0.1: 8882: 7772
The marked red part should be modified according to the current node serial number. For example, if the current part is in the server0 directory, enter the part according to the above configuration and save it. After entering the configuration, you can start zookeeper:
./bin/zkServer.sh start
Then, repeat the above operations in the directory of the other two nodes to build the pseudo cluster. You can view the role of the current node in the cluster through./bin/zkserver. Sh status, as shown below:
JMX enabled by defaultUsing config: /opt/zookeeper/server0/zookeeper/bin/../conf/zoo.cfgMode: follower
You can specify any node to access the cluster through./bin/zkcli. Sh-server host: Port:
./bin/zkCli.sh -server 127.0.0.1:2181
Zookeeper cluster Construction