【ZooKeeper】安装

Posted by 西维蜀黍 on 2020-07-25, Last Modified on 2021-11-06

Install on OS

Prerequisition

安装 ZooKeeper 之前需要先安装 JDK, 关于 JDK 的安装这里不再赘述。

Download

https://zookeeper.apache.org/releases.html

$ curl -O https://downloads.apache.org/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz
$ tar -zxf apache-zookeeper-3.7.0-bin.tar.gz
$ cd apache-zookeeper-3.7.0-bin
$ mkdir data

Install on Docker

$ docker run --name zookeeper --restart=unless-stopped -d zookeeper

This image includes EXPOSE 2181 2888 3888 8080 (the zookeeper client port, follower port, election port, AdminServer port respectively), so standard container linking will make it automatically available to the linked containers. Since the Zookeeper “fails fast” it’s better to always restart it.

Logs

This will write logs to /logs/zookeeper.log. Check ZooKeeper Logging for more details.

This image is configured with a volume at /logs for your convenience.

Configuring ZooKeeper

Create a configuration file named zoo.cfg at /opt/zookeeper/conf. You can create and open a file using nano or your favorite editor:

$ nano /home/parallels/apache-zookeeper-3.6.1-bin/conf/zoo.cfg

Add the following set of properties and values to that file:

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/parallels/apache-zookeeper-3.6.1-bin/data
clientPort=2181

A tickTime of 2000 milliseconds is the suggested interval between heartbeats. A shorter interval could lead to system overhead with limited benefits. The dataDir parameter points to the path defined by the symbolic link you created in the previous section. Conventionally, ZooKeeper uses port 2181 to listen for client connections. In most situations, 60 allowed client connections are plenty for development and testing.

Save the file and exit the editor.

You have configured ZooKeeper and are ready to start the server.

$ bin/zkServer.sh start
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /home/parallels/apache-zookeeper-3.6.1-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

如果出现:

$ bin/zkServer.sh start
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /home/parallels/apache-zookeeper-3.6.1/bin/../conf/zoo.cfg
Starting zookeeper ... FAILED TO START

则可以检查对应log:

$ cat logs/zookeeper-parallels-server-parallels-Parallels-Virtual-Platform.out
Error: Could not find or load main class org.apache.zookeeper.server.quorum.QuorumPeerMain
Caused by: java.lang.ClassNotFoundException: org.apache.zookeeper.server.quorum.QuorumPeerMain

这是因为你下载的不是binary,而是source code。我们要下载:

$ curl -O https://downloads.apache.org/zookeeper/zookeeper-3.6.1/apache-zookeeper-3.6.1-bin.tar.gz

而不是:

$ curl - O https://downloads.apache.org/zookeeper/zookeeper-3.6.1/apache-zookeeper-3.6.1.tar.gz

Testing the Standalone Installation

Connect to the local ZooKeeper server with the following command:

$ bin/zkCli.sh -server 127.0.0.1:2181
Connecting to 127.0.0.1:2181
...
...
[zk: 127.0.0.1:2181(CONNECTED) 0]

Reference