ZooKeeper Series (2): Install and set up ZooKeeper environment

ZooKeeper has three installation modes: standalone, pseudo-cluster, and cluster:

  • standalone mode: Install ZooKeeper on only one machine and start only one ZooKeeper instance.
  • Pseudo Cluster: Open multiple ZooKeeper instances on one machine to simulate a cluster.This is achieved by loading different profiles and assigning different data directories.Because it runs on the same machine, the machine goes down and the entire ZooKeeper hangs up.Basically, such a ZooKeeper cluster will not be laid out.
  • Cluster mode: Install ZooKeeper on at least three machines to form a cluster, ensuring the availability of ZooKeeper services as long as the number of failing nodes is within a certain range.

The real layout is basically cluster mode, which standalone occasionally uses to do some testing. It has a single point of failure, and pseudo-cluster mode has a single point of failure.

Cluster mode does not necessarily have at least three machines, but it is more dangerous to use two machines than one because a casual failure that does not meet the "most" requirements will block the entire ZooKeeper service.Two machines are twice as likely to fail as one.Therefore, it is also recommended that you deploy ZooKeeper with an odd number of machines.

ZooKeeper Download: https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/

I downloaded version 3.4.12.

1. Install jdk

ZooKeeper runs in a java environment, so jdk needs to be installed first and requires a version higher than 1.6.

jdk download: http://www.oracle.com/technetwork/java/javase/downloads/index.html

Take the rpm package as an example.

yum localinstall -y jdk-8u131-linux-x64.rpm

The installation path for the rpm package is/usr/java.

[root@s1 ~]# ls -l /usr/java/
total 4
lrwxrwxrwx 1 root root   16 Jun 26 22:53 default -> /usr/java/latest
drwxr-xr-x 9 root root 4096 Jun 26 22:53 jdk1.8.0_131
lrwxrwxrwx 1 root root   22 Jun 26 22:53 latest -> /usr/java/jdk1.8.0_131

With this soft link method, a new version of jdk will be installed in the future, simply change the link object of latest.

Then set the JAVA_HOME environment variable and export the PATH environment variable of the directory where the java program resides.

echo 'JAVA_HOME=/usr/java/latest' > /etc/profile.d/jdk.sh
echo 'PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile.d/jdk.sh
chmod +x /etc/profile.d/jdk.sh
source /etc/profile.d/jdk.sh

2.StandAlone mode

standalone mode is to install ZooKeeper on a single machine.

Unzip zookeeper-3.4.12.tar.gz first.

tar xf zookeeper-3.4.12.tar.gz

Move it under/usr/local/for easy administration.

mv zookeeper-3.4.12 /usr/local/zookeeper

Several scripts from Windows and Linux are available in ZooKeeper's bin directory:

[root@s2 zookeeper]# cd /usr/local/zookeeper
[root@s2 zookeeper]# ls bin
README.txt  zkCleanup.sh  zkCli.cmd  zkCli.sh  zkEnv.cmd  zkEnv.sh  zkServer.cmd  zkServer.sh
  • zkServer: Used to start and stop ZooKeeper and to view ZooKeeper status.

    zkServer.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}
  • zkEnv: Set environment variables when ZooKeeper starts and closes.This script is referenced in every other script.
  • zkCleanup: Clear the transaction log and snapshot of ZooKeeper.
  • zkCli: A command line client for ZooKeeper.

Setting ZooKeeper's environment variables is not required, it's just for the convenience of working with the scripts above.

echo 'ZOOKEEPER_HOME=/usr/local/zookeeper' >/etc/profile.d/zk.sh
echo 'PATH=$ZOOKEEPER_HOME/bin:$PATH' >> /etc/profile.d/zk.sh
chmod +x /etc/profile.d/zk.sh
source /etc/profile.d/zk.sh

Delete the script under windows:

rm -rf /usr/local/zookeeper/bin/{*.cmd,README.txt}

To start ZooKeeper, first provide a configuration file.The path to the default profile is the conf directory under $ZOKEEPER_HOME.In this directory, there are several files:

[root@s2 zookeeper]# ls conf
configuration.xsl  log4j.properties  zoo_sample.cfg
  • configuration.xsl: Ignore it.
  • log4f.properties: is ZooKeeper's log profile.
  • zoo_sample.cfg: A sample configuration file for ZooKeeper.

The default configuration file name for zkServer.sh is zoo.cfg.So create a zoo.cfg in the conf directory.Write several configuration items to keep ZooKeeper working properly:

tickTime=2000
dataDir=/usr/local/zookeeper/data1
clientPort=2181

Where:

  • tickTime: The unit of time used by many of the configuration items in ZooKeeper, such as heartbeat time, connection timeout, and so on, is configured here as 2,000 milliseconds, or 2 seconds.
  • dataDir: The data directory of the ZooKeeper instance.
  • clientPort: The port on which ZooKeeper provides services to the outside world.

Then start ZooKeeper with zkServer.sh.

[root@s2 zk]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

You can also manually specify a configuration file to read at startup:

[root@s2 zk]# zkServer.sh start /usr/local/zookeeper/conf/zoo.cfg

View the running status of ZooKeeper:

[root@s2 zk]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: standalone

You can see that the running mode is "standalone".

When ZooKeeper is started, it is ready to provide ZooKeeper services to the outside world.Here, use the command line client provided by ZooKeeper to connect to ZooKeeper for a simple test.

Connect ZooKeeper instance:

zkCli.sh -server localhost:2181

A large amount of information is output during the connection process.When the connection succeeds, it enters ZooKeeper's interactive mode:

WatchedEvent state:SyncConnected type:None path:null

[zk: localhost:2181(CONNECTED) 0]   # Enter the commands ZooKeeper allows here

For example, create a znode:

[zk: localhost:2181(CONNECTED) 0] create /zk_test mydata1
Created /zk_test
[zk: localhost:2181(CONNECTED) 1] ls /
[zookeeper, zk_test]

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

3. Configuring pseudo-cluster mode

The pseudo-cluster mode of ZooKeeper is to run multiple ZooKeeper instances on a single server to simulate a ZooKeeper cluster.In pseudo cluster mode, each instance has different configuration files, different data directories, and different ports.Generally speaking, a ZooKeeper cluster requires at least three server nodes.

Here, I configure three instances of ZooKeeper pseudoclusters with data directories of data1, data2, and data3 under $ZOOKEEPER_HOME.Since you will be writing myid files to these directories later, create these three directories first:

mkdir /usr/local/zookeeper/data{1,2,3}

Provide the configuration files for three instances starting with $ZOOKEEPER_HOME/conf/{zoo1.cfg,zoo2.cfg,zoo3.cfg}.

Below is the zoo1.cfg content, which is explained later.

tickTime=2000
dataDir=/usr/local/zookeeper/data1
clientPort=2181
initLimit=5
syncLimit=2
server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889

Below is the zoo2.cfg content, which is explained later.

tickTime=2000
dataDir=/usr/local/zookeeper/data2
clientPort=2182
initLimit=5
syncLimit=2
server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889

Below is the zoo3.cfg content, which is explained later.

tickTime=2000
dataDir=/usr/local/zookeeper/data3
clientPort=2183
initLimit=5
syncLimit=2
server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889

In the configuration item above:

  • initLimit: When non-leader nodes (i.e. follower s and observers) are started, data needs to be copied from the leader first to ensure that all ZooKeeper node data is synchronized.This option sets the timeout for non-leader nodes from start to finish synchronization, which is in tickTime, so the timeout above is 5*2=10 seconds.

    Generally speaking, ZooKeeper keeps coordinated data, but the amount of data is not large, so this parameter can be ignored most of the time. If the data to be synchronized is really large, consider increasing this timeout.

  • Maximum length of time for data delay between syncLimit:follower and leader.For example, if a node is slow to update and its data is far behind the leader, ZooKeeper will kick it out of the ZooKeeper cluster.ZooKeeper uses time to measure the latency of data between follower and leader. The value of this option depends on tickTime, for example, tickTime=2000, and syncLimit=2 to indicate that follower is four seconds behind leader.
  • server.X=[hostname]:port_A:port_B: This option specifies the server nodes in the ZooKeeper cluster.Where:
    • X: Integer.Is a simple identification of the server in ZooKeeper.This number needs to be consistent with the contents of the myid file under dataDir.When starting each instance in the ZooKeeper cluster, you need to read the myid file in the data directory and match the values in the file to the server.X in the configuration file to indicate which ZooKeeper server node it is.
    • hostname: The address of the ZooKeeper server node.
    • port_A: This is the first port for data synchronization and other communication between Follower and Leader.
    • port_B: This is the second port for voting communication during the Leader election.

Therefore, create corresponding myid files under dataDir for each instance.

echo 1 >/usr/local/zookeeper/data1/myid
echo 2 >/usr/local/zookeeper/data2/myid
echo 3 >/usr/local/zookeeper/data3/myid

Then start the three ZooKeeper instances.

zkServer.sh start /usr/local/zookeeper/conf/zoo1.cfg
zkServer.sh start /usr/local/zookeeper/conf/zoo2.cfg
zkServer.sh start /usr/local/zookeeper/conf/zoo3.cfg

View the current list of java processes:

[root@s1 zk]# jps -l
5473 org.apache.zookeeper.server.quorum.QuorumPeerMain
5395 org.apache.zookeeper.server.quorum.QuorumPeerMain
5427 org.apache.zookeeper.server.quorum.QuorumPeerMain
5524 sun.tools.jps.Jps

Look at the relationship between these three instances: zoo2.cfg was chosen leader, and the others were follower s.

[root@s1 zk]# zkServer.sh status /usr/local/zookeeper/conf/zoo3.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/conf/zoo3.cfg
Mode: follower

[root@s1 zk]# zkServer.sh status /usr/local/zookeeper/conf/zoo1.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/conf/zoo1.cfg
Mode: follower

[root@s1 zk]# zkServer.sh status /usr/local/zookeeper/conf/zoo2.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/conf/zoo2.cfg
Mode: leader

You can use the command-line client tools provided by ZooKeeper to test, write data to one instance, and other instances can receive it.

[root@s1 zk]# zkCli.sh -server localhost:2181 create /my_znode hello
......ellipsis.........
WatchedEvent state:SyncConnected type:None path:null
Created /my_znode


[root@s1 zk]# zkCli.sh -server localhost:2182 ls /
......ellipsis........
WatchedEvent state:SyncConnected type:None path:null
[my_znode, zookeeper]

4. Configure ZooKeeper Cluster

Take a three-node cluster as an example: 192.168.100.21, 192.168.100.22, 192.168.100.23.Because the previous configurations of single-machine ZooKeeper and pseudo-cluster ZooKeeper have explained the meaning of all the steps and configuration items, steps are directly given here.

Suppose you have JDK installed on all three nodes (this is the premise) and downloaded ZooKeeper.

Unzip zookeeper-3.4.12.tar.gz first.

# All three nodes execute
tar xf zookeeper-3.4.12.tar.gz
mv zookeeper-3.4.12 /usr/local/zookeeper

Adding ZooKeeper environment variables is not a required procedure, but is recommended.

# Execute on all 3 nodes
echo 'ZOOKEEPER_HOME=/usr/local/zookeeper' >/etc/profile.d/zk.sh
echo 'PATH=$ZOOKEEPER_HOME/bin:$PATH' >> /etc/profile.d/zk.sh
chmod +x /etc/profile.d/zk.sh
source /etc/profile.d/zk.sh

Provide a configuration file.The following are the configuration files on three nodes:

[root@s1 zk]# cat /usr/local/zookeeper/conf/zoo.cfg 
tickTime=2000
dataDir=/usr/local/zookeeper/data
clientPort=2181
initLimit=5
syncLimit=2
server.1=192.168.100.21:2888:3888
server.2=192.168.100.22:2888:3888
server.3=192.168.100.23:2888:3888

Create a data directory on all three nodes and write the myid file.

# Execute on all three nodes:
mkdir /usr/local/zookeeper/data

# Executed on 192.168.100.21
echo 1 >/usr/local/zookeeper/data/myid

# Executed on 192.168.100.22
echo 2 >/usr/local/zookeeper/data/myid

# Executed on 192.168.100.23
echo 3 >/usr/local/zookeeper/data/myid

Start these three ZooKeeper instances.

# All three nodes execute:
zkServer.sh start

Then verify that ZooKeeper is started correctly on all three nodes.

# Execute on Node 1:
[root@s1 zk]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: follower

# Execute on Node 2:
[root@s2 zk]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: follower

# Execute on Node 3:
[root@s3 zk]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: leader

If an error occurs with a server instance, close the ZooKeeper instance by zkServer.sh stop, and then check with the jps command to see if any ZooKeeper instances are still running and, if so, kill out.Then start ZooKeeper.Additionally, it is recommended that the dataDir/version-2 directory be deleted before starting ZooKeeper for the first time (if it already exists, it may have been left behind by previous instances for various reasons).

Finally, test whether a znode is created on one node and synchronized by other nodes.

# Create a znode on 192.168.100.21:
[root@s2 zk]# zkCli.sh -server 192.168.100.21:2181 create /test_znode "hello world"
Connecting to 192.168.100.21:2181
.........ellipsis..........
Created /test_znode

# Get this znode on 192.168.100.22
[root@s2 zk]# zkCli.sh -server 192.168.100.22:2181 get /test_znode
Connecting to 192.168.100.22:2181
.........ellipsis..........
hello world
cZxid = 0x100000002
ctime = Wed Jun 27 08:14:38 CST 2018
mZxid = 0x100000002
mtime = Wed Jun 27 08:14:38 CST 2018
pZxid = 0x100000002
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 11
numChildren = 0

# Get this znode on 192.168.100.23
[root@s2 zk]# zkCli.sh -server 192.168.100.23:2181 ls /
Connecting to 192.168.100.23:2181
.........ellipsis..........
[zookeeper, test_znode]

Keywords: Linux Zookeeper JDK Java Apache

Added by konqest on Mon, 20 May 2019 02:51:17 +0300