Implementation of zookeeper pseudo clustering

1. Install the stand-alone Zookeeper

1. You need to install jdk first

2. Install zookeeper, download address https://archive.apache.org/dist/zookeeper/ , select the corresponding version, I choose version 3.4.9 here to install, copy the link address of the version you want to download, and use the wget command to download to the virtual machine.

wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz

3. Extract the zookeeper compressed file to / usr/local/apps /. The app directory needs to be created by yourself.

tar -zxvf ~/zookeeper-3.4.9.tar.gz -C /usr/local/apps/

4. Rename the zookeeper installation directory. This step is not necessary. It is just for convenience

mv zookeeper-3.4.9 zookeeper

5. Modify environment variables. This step is to execute some commands of zookeeper (such as start command) without entering bin directory to execute in relative path or absolute path. It is configured that environment variables can execute commands under bin directory of zookeeper in any directory.

Edit the / etc/profile file

vi /etc/profile

Add the following content: the configuration of zookeeper home is the installation location of zookeeper, $zookeeper home refers to / usr/local/apps/zookeeper, and the configuration of PATH is to append the variable / usr/local/apps/zookeeper/bin to the PATH.

export ZOOKEEPER_HOME=/usr/local/apps/zookeeper
export PATH=$PATH:$ZOOKEEPER_HOME/bin

6. Recompile the file to make the modified file effective

source /etc/profile

2. Build zookeeper pseudo cluster environment

1. The implementation of zookeeper pseudo cluster is to create three zookeeper instances on a virtual machine. Since the startup of zookeeper depends on the configuration file of zoo_sample.cfg, I have created three additional configuration files to simulate three zookeeper instances

Create zookeeper instance 1, enter zookeeper/conf directory and copy a configuration file named zk1.cfg

cd zookeeper/conf
cp zoo_sample.cfg zk1.cfg

2. Modify the zk1.cfg configuration file. Some attributes have default values that need to be commented out to avoid conflicts

dataDir=/usr/local/apps/zookeeper/data/zk1
dataLogDir=/usr/local/apps/zookeeper/log/zk1

clientPort=2181

#server. S / N = (host name, heartbeat port, data port)
server.1=192.168.2.10:1888:2888	
server.2=192.168.2.10:3888:4888
server.3=192.168.2.10:5888:6888

3. Create zookeeper instance 2

cp zk1.cfg zk2.cfg
dataDir=/usr/local/apps/zookeeper/data/zk2
dataLogDir=/usr/local/apps/zookeeper/log/zk2

clientPort=2182

#server. S / N = (host name, heartbeat port, data port)
server.1=192.168.2.10:1888:2888	
server.2=192.168.2.10:3888:4888
server.3=192.168.2.10:5888:6888

4. Create zookeeper instance 3

cp zk1.cfg zk3.cfg
dataDir=/usr/local/apps/zookeeper/data/zk3
dataLogDir=/usr/local/apps/zookeeper/log/zk3

clientPort=2183

#server. S / N = (host name, heartbeat port, data port)
server.1=192.168.2.10:1888:2888	
server.2=192.168.2.10:3888:4888
server.3=192.168.2.10:5888:6888

5. The folder required to create the directory configured in the previous step

mkdir -p /usr/local/apps/zookeeper/data/zk1
mkdir -p /usr/local/apps/zookeeper/log/zk1

mkdir -p /usr/local/apps/zookeeper/data/zk2
mkdir -p /usr/local/apps/zookeeper/log/zk2

mkdir -p /usr/local/apps/zookeeper/data/zk3
mkdir -p /usr/local/apps/zookeeper/log/zk3

6. Create the myid file in the corresponding data folder and add the corresponding content

echo "1" > /usr/local/apps/zookeeper/data/zk1/myid
echo "2" > /usr/local/apps/zookeeper/data/zk2/myid
echo "3" > /usr/local/apps/zookeeper/data/zk3/myid

7. Start the zookeeper cluster

zkServer.sh start zk1.cfg
zkServer.sh start zk2.cfg
zkServer.sh start zk3.cfg

8. View cluster status

zkServer.sh status zk1.cfg
zkServer.sh status zk2.cfg
zkServer.sh status zk3.cfg

9. The test results are as follows

3.ZooKeeper command line operation

1. Access the client

zkCli.sh -server 192.168.2.10:2182

The successful login is as follows

2. Use help to view operation commands

3. Create a node. Create a servers node in the root directory. The content is ss1

create /servers ss1

4. View nodes

ls /

5. View node content

get /servers

6. Modify node content

set /servers ss2

7. To delete a node, the delete command can only delete leaf nodes (nodes without child nodes), and the rmr command can delete non leaf nodes

delete /servers

8. There are two types of nodes

(1) Temporary (disconnect and delete by yourself)

(2) persistent (do not delete when disconnected)

Create a temporary node, log in to the client again after exiting the client to view, and the node has been deleted.

create -e /servers2 es1

To create a persistent node, if - e is not added, it is the created persistent node. The client exits and logs in the node again.

No more nodes of any type can be created under temporary nodes

9. For node content monitoring, get path watch is used to monitor the node. When the node content changes, the node change information will be received

Client 2183 listens

get /servers watch

Client 2181 changes the content of the / servers node to ss3

set /servers ss3

Client 2183 will display the following information

10. Directory change monitoring

Client 2183 listens

ls /servers watch

Add a / servers/server1 node to client 2181

create /servers/server1 cs1

Client 2183 displays node change information

Tags: Programming Zookeeper Apache JDK

Posted on Tue, 05 May 2020 23:05:02 -0700 by 3dron