ZooKeeper-3.3.4 cluster installation configuration

Reprinted from http://shiyanjun.cn/archives/469.html
ZooKeeper is a distributed open source framework that provides basic services for coordinating distributed applications. It exposes a set of general services - Distributed Synchronization to external applications. ), Naming Service, Group Maintenance, etc., simplify the coordination and management of distributed applications, and provide high-performance distributed services. ZooKeeper itself can be installed and run in Standalone mode, but its advantage lies in ensuring the stability and availability of the ZooKeeper cluster based on a certain strategy through a distributed ZooKeeper cluster (one Leader, multiple Followers), so as to achieve the reliability of distributed applications .
There are many introductions to ZooKeeper on the Internet, and you can also refer to some related links that I have compiled at the back of the article.
The installation and configuration of ZooKeeper is relatively easy. Below, we briefly explain the configuration of ZooKeeper.

ZooKeeper Standalone Mode Download the ZooKeeper package

from the Apache website (zookeeper.apache.org), I chose version 3.3.4 (zookeeper-3.3.4.tar.gz), it is very easy to install on a Linux machine, just After decompression is required, the ZooKeeper server process can be started with a simple configuration.
Change the zoo_sample.cfg under the zookeeper-3.3.4/conf directory to zoo.cfg. The content of the configuration file is as follows:

tickTime=2000
dataDir=/home/hadoop/storage/zookeeper
clientPort=2181
initLimit = 5
syncLimit=2

The meaning of the above configuration parameters is also very simple, and the reference is as follows:

quote
tickTime —— the basic time unit in milliseconds used by ZooKeeper. It is used to do heartbeats and the minimum session timeout will be twice the tickTime.
dataDir —— the location to store the in-memory database snapshots and, unless specified otherwise, the transaction log of updates to the database.
clientPort —— the port to listen for client connections

Let's start the ZooKeeper server process:

cd zookeeper-3.3.4/
bin/zkServer.sh start

The ZooKeeper server process, named QuorumPeerMain, can be viewed through the jps command.
To connect to the ZooKeeper server on the client, execute the following command:


bin/zkCli.sh -server dynamic:2181

The above dynamic is my host name. If it is executed locally, just execute the following command:


bin/zkCli.sh

Client connection information is as follows:
shirdrn@master:~/installation/zookeeper-3.3.4$ bin/zkCli.sh -server dynamic:2181
Connecting to dynamic:2181
2013-10-28 21:30:06,178 - INFO  [main:Environment@97] - Client environment:zookeeper.version=3.3.3-1203054, built on 11/17/2011 05:47 GMT
2013-10-28 21:30:06,188 - INFO  [main:Environment@97] - Client environment:host.name=master
2013-10-28 21:30:06,191 - INFO  [main:Environment@97] - Client environment:java.version=1.6.0_30
2013-10-28 21:30:06,194 - INFO  [main:Environment@97] - Client environment:java.vendor=Sun Microsystems Inc.
2013-10-28 21:30:06,200 - INFO  [main:Environment@97] - Client environment:java.home=/home/hadoop/installation/jdk1.6.0_30/jre
2013-10-28 21:30:06,203 - INFO  [main:Environment@97] - Client environment:java.class.path=/home/hadoop/installation/zookeeper-3.3.4/bin/../build/classes:/home/hadoop/installation/zookeeper-3.3.4/bin/../build/lib/*.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../zookeeper-3.3.4.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/log4j-1.2.15.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/jline-0.9.94.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/commons-lang-2.4.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/commons-collections-3.2.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/commons-cli-1.1.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/apache-rat-tasks-0.6.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/apache-rat-core-0.6.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../src/java/lib/*.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../conf:/home/hadoop/installation/jdk1.6.0_30/lib/*.jar:/home/hadoop/installation/jdk1.6.0_30/jre/lib/*.jar
2013-10-28 21:30:06,206 - INFO  [main:Environment@97] - Client environment:java.library.path=/home/hadoop/installation/jdk1.6.0_30/jre/lib/i386/client:/home/hadoop/installation/jdk1.6.0_30/jre/lib/i386:/home/hadoop/installation/jdk1.6.0_30/jre/../lib/i386:/usr/java/packages/lib/i386:/lib:/usr/lib
2013-10-28 21:30:06,213 - INFO  [main:Environment@97] - Client environment:java.io.tmpdir=/tmp
2013-10-28 21:30:06,216 - INFO  [main:Environment@97] - Client environment:java.compiler=<NA>
2013-10-28 21:30:06,235 - INFO  [main:Environment@97] - Client environment:os.name=Linux
2013-10-28 21:30:06,244 - INFO  [main:Environment@97] - Client environment:os.arch=i386
2013-10-28 21:30:06,246 - INFO  [main:Environment@97] - Client environment:os.version=3.0.0-14-generic
2013-10-28 21:30:06,251 - INFO  [main:Environment@97] - Client environment:user.name=hadoop
2013-10-28 21:30:06,254 - INFO  [main:Environment@97] - Client environment:user.home=/home/hadoop
2013-10-28 21:30:06,255 - INFO  [main:Environment@97] - Client environment:user.dir=/home/hadoop/installation/zookeeper-3.3.4
2013-10-28 21:30:06,264 - INFO  [main:ZooKeeper@379] - Initiating client connection, connectString=dynamic:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@bf32c
2013-10-28 21:30:06,339 - INFO  [main-SendThread():ClientCnxn$SendThread@1061] - Opening socket connection to server dynamic/192.168.0.107:2181
Welcome to ZooKeeper!
2013-10-28 21:30:06,397 - INFO  [main-SendThread(dynamic:2181):ClientCnxn$SendThread@950] - Socket connection established to dynamic/192.168.0.107:2181, initiating session
JLine support is enabled
2013-10-28 21:30:06,492 - INFO  [main-SendThread(dynamic:2181):ClientCnxn$SendThread@739] - Session establishment complete on server dynamic/192.168.0.107:2181, sessionid = 0x134b9b714f9000c, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: dynamic:2181(CONNECTED) 0]

Next, you can use help to see the basic operation commands that the Zookeeper client can use.

ZooKeeper Distributed mode

ZooKeeper distributed mode installation (ZooKeeper cluster) is also relatively easy, here are the basic points.
The first thing to be clear is that the ZooKeeper cluster is an independent distributed coordination service cluster. The meaning of "independent" means that if you want to use ZooKeeper to realize the coordination and management of distributed applications and simplify coordination and management, any distributed application can Use, thanks to Zookeeper's data model (Data Model) and hierarchical namespace (Hierarchical Namespace) structure, please refer to http://zookeeper.apache.org/doc/trunk/zookeeperOver.html for details. When designing your distributed application coordination service, the first thing to consider is how to organize the hierarchical namespace.
The following describes the installation and configuration of the distributed mode. The process is as follows:
Step 1: Host name to IP address mapping configuration
There are two key roles in the ZooKeeper cluster: Leader and Follower. All nodes in the cluster provide services for distributed applications as a whole, and each node in the cluster is connected to each other. Therefore, when configuring a ZooKeeper cluster, the mapping from host to IP address of each node is To configure the mapping information of other nodes in the cluster.
For example, the configuration of each node in my ZooKeeper cluster, taking zk-01 as an example, the content of /etc/hosts is as follows:

192.168.0.179 zk-01
192.168.0.178 zk-02
192.168.0.177 zk-03

ZooKeeper employs an election algorithm called Leader election. During the operation of the entire cluster, there is only one leader, and the others are followers. If there is a problem with the leader during the operation of the ZooKeeper cluster, the system will use this algorithm to re-select a leader. Therefore, the above mapping must be configured to ensure that each node is connected to each other.
When the ZooKeeper cluster is started, a leader will be elected first. During the leader election process, a node that satisfies the election calculation can become the leader. The architecture of the entire cluster can be found at http://zookeeper.apache.org/doc/trunk/zookeeperOver.html#sc_designGoals.
Step 2: Modify the ZooKeeper configuration file
On one of the machines (zk-01), unzip zookeeper-3.3.4.tar.gz, and modify the configuration file conf/zoo.cfg, as shown below:


tickTime=2000
dataDir=/home/hadoop/storage/zookeeper
clientPort=2181
initLimit = 5
syncLimit=2
server.1=zk-01:2888:3888
server.2=zk-02:2888:3888
server.3=zk-03:2888:3888

For the description of the above configuration content, you can refer to http://zookeeper.apache.org/doc/trunk/zookeeperStarted.html#sc_RunningReplicatedZooKeeper.
Step 3: Remotely copy and distribute installation
files ZooKeeper has been configured on a machine zk-01. Now you can remotely copy the configured installation files to the corresponding directory of each node in the cluster:

cd /home/hadoop/installation/
scp -r zookeeper-3.3.4/ shirdrn@zk-02:/home/hadoop/installation/
scp -r zookeeper-3.3.4/ shirdrn@zk-03:/home/hadoop/installation/

Step 4: Set myid
Under the directory specified by the dataDir we configured, create a myid file, which contains a number to identify the current host, what is the number of X in server.X configured in the conf/zoo.cfg file, Then enter this number in the myid file, for example:

shirdrn@zk-01:~/installation/zookeeper-3.3.4$ echo "1" > /home/hadoop/storage/zookeeper/myid
shirdrn@zk-02:~/installation/zookeeper-3.3.4$ echo "2" > /home/hadoop/storage/zookeeper/myid
shirdrn@zk-03:~/installation/zookeeper-3.3.4$ echo "3" > /home/hadoop/storage/zookeeper/myid

Configure as above.
Step 5: Start the ZooKeeper cluster
On each node of the ZooKeeper cluster, execute the script to start the ZooKeeper service, as follows:

shirdrn@zk-01:~/installation/zookeeper-3.3.4$ bin/zkServer.sh start
shirdrn@zk-02:~/installation/zookeeper-3.3.4$ bin/zkServer.sh start
shirdrn@zk-03:~/installation/zookeeper-3.3.4$ bin/zkServer.sh start

Taking the node zk-01 as an example, the log is as follows:


The order of my startup is zk-01>zk-02>zk-03, because when the ZooKeeper cluster starts, each node tries to connect to other nodes in the cluster Nodes that are started first will definitely not be connected to those that have not been started later, so the exceptions in the previous part of the above log can be ignored. As you can see from the latter part, the cluster is finally stable after a leader is selected.
Other nodes may have similar problems, which are normal.
Step 6: Installation verification
You can view the startup status through the ZooKeeper script, including the roles of each node in the cluster (or Leader, or Follower), as shown below, which is queried on each node in the ZooKeeper cluster the result of:
shirdrn@zk-01:~/installation/zookeeper-3.3.4$ bin/zkServer.sh status
JMX enabled by default
Using config: /home/hadoop/installation/zookeeper-3.3.4/bin/../conf/zoo.cfg
Mode: follower
 
shirdrn@zk-02:~/installation/zookeeper-3.3.4$  bin/zkServer.sh status
JMX enabled by default
Using config: /home/hadoop/installation/zookeeper-3.3.4/bin/../conf/zoo.cfg
Mode: leader
 
shirdrn@zk-03:~/installation/zookeeper-3.3.4$  bin/zkServer.sh status
JMX enabled by default
Using config: /home/hadoop/installation/zookeeper-3.3.4/bin/../conf/zoo.cfg
Mode: follower

It can be seen from the above status query results that zk-02 is the leader of the cluster, and the other two nodes are followers.
In addition, it is possible to connect to the ZooKeeper cluster through client scripts. For the client, ZooKeeper is an ensemble, and connecting to the ZooKeeper cluster actually feels like the service of the entire cluster, so you can establish a connection to the service cluster on any node, for example:
shirdrn@zk-03:~/installation/zookeeper-3.3.4$ bin/zkCli.sh -server zk-01:2181
Connecting to zk-01:2181
2013-10-28 07:14:21,068 - INFO  [main:Environment@97] - Client environment:zookeeper.version=3.3.3-1203054, built on 11/17/2011 05:47 GMT
2013-10-28 07:14:21,080 - INFO  [main:Environment@97] - Client environment:host.name=zk-03
2013-10-28 07:14:21,085 - INFO  [main:Environment@97] - Client environment:java.version=1.6.0_30
2013-10-28 07:14:21,089 - INFO  [main:Environment@97] - Client environment:java.vendor=Sun Microsystems Inc.
2013-10-28 07:14:21,095 - INFO  [main:Environment@97] - Client environment:java.home=/home/hadoop/installation/jdk1.6.0_30/jre
2013-10-28 07:14:21,104 - INFO  [main:Environment@97] - Client environment:java.class.path=/home/hadoop/installation/zookeeper-3.3.4/bin/../build/classes:/home/hadoop/installation/zookeeper-3.3.4/bin/../build/lib/*.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../zookeeper-3.3.4.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/log4j-1.2.15.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/jline-0.9.94.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/commons-lang-2.4.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/commons-collections-3.2.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/commons-cli-1.1.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/apache-rat-tasks-0.6.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/apache-rat-core-0.6.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../src/java/lib/*.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../conf:/home/hadoop/installation/jdk1.6.0_30/lib/*.jar:/home/hadoop/installation/jdk1.6.0_30/jre/lib/*.jar
2013-10-28 07:14:21,111 - INFO  [main:Environment@97] - Client environment:java.library.path=/home/hadoop/installation/jdk1.6.0_30/jre/lib/i386/client:/home/hadoop/installation/jdk1.6.0_30/jre/lib/i386:/home/hadoop/installation/jdk1.6.0_30/jre/../lib/i386:/usr/java/packages/lib/i386:/lib:/usr/lib
2013-10-28 07:14:21,116 - INFO  [main:Environment@97] - Client environment:java.io.tmpdir=/tmp
2013-10-28 07:14:21,124 - INFO  [main:Environment@97] - Client environment:java.compiler=<NA>
2013-10-28 07:14:21,169 - INFO  [main:Environment@97] - Client environment:os.name=Linux
2013-10-28 07:14:21,175 - INFO  [main:Environment@97] - Client environment:os.arch=i386
2013-10-28 07:14:21,177 - INFO  [main:Environment@97] - Client environment:os.version=3.0.0-14-generic
2013-10-28 07:14:21,185 - INFO  [main:Environment@97] - Client environment:user.name=hadoop
2013-10-28 07:14:21,188 - INFO  [main:Environment@97] - Client environment:user.home=/home/hadoop
2013-10-28 07:14:21,190 - INFO  [main:Environment@97] - Client environment:user.dir=/home/hadoop/installation/zookeeper-3.3.4
2013-10-28 07:14:21,197 - INFO  [main:ZooKeeper@379] - Initiating client connection, connectString=zk-01:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@bf32c
2013-10-28 07:14:21,305 - INFO  [main-SendThread():ClientCnxn$SendThread@1061] - Opening socket connection to server zk-01/192.168.0.179:2181
Welcome to ZooKeeper!
2013-10-28 07:14:21,376 - INFO  [main-SendThread(zk-01:2181):ClientCnxn$SendThread@950] - Socket connection established to zk-01/192.168.0.179:2181, initiating session
JLine support is enabled
[zk: zk-01:2181(CONNECTING) 0] 2013-10-28 07:14:21,872 - INFO  [main-SendThread(zk-01:2181):ClientCnxn$SendThread@739] - Session establishment complete on server zk-01/192.168.0.179:2181, sessionid = 0x134bdcd6b730000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null

[zk: zk-01:2181(CONNECTED) 0] ls /
[zookeeper]

The current root path is /zookeeper.

Summary description

Hostname and IP address mapping configuration problem
When starting the ZooKeeper cluster, if the following error occurs in the log of the zk-01 node in the ZooKeeper cluster:
java.net.SocketTimeoutException
        at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:109)
        at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:371)
        at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(QuorumCnxManager.java:404)
        at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader (FastLeaderElection.java:688)
        at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:622)
2013-10-28 06:37:46,026 - INFO  [QuorumPeer:/0:0:0:0:0:0:0:0:2181:FastLeaderElection@697] - Notification time out: 6400
2013-10-28 06:37:57,431 - WARN  [QuorumPeer:/0:0:0:0:0:0:0:0:2181:QuorumCnxManager@384] - Cannot open channel to 2 at election address zk-02/202.106.199.35:3888
java.net.SocketTimeoutException
        at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:109)
        at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:371)
        at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(QuorumCnxManager.java:404)
        at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader (FastLeaderElection.java:688)
        at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:622)
2013-10-28 06:38:02,442 - WARN  [QuorumPeer:/0:0:0:0:0:0:0:0:2181:QuorumCnxManager@384] - Cannot open channel to 3 at election address zk-03/202.106.199.35:3888

Obviously, when zk-01 connects to other nodes in the cluster (zk-02, zk-03) at startup, the IP mapped by the hostname is inconsistent with our actual configuration, so links cannot be established between nodes in the cluster , the entire ZooKeeper cluster fails to start.
The zk-02/202.106.199.35:3888 in the above error log should actually be zk-02/202.192.168.0.178:3888, but there is a problem with the mapping during domain name resolution. Modify the /etc of each node The /hosts file configures the mapping of hostnames to IP addresses for all nodes in the ZooKeeper cluster.

Reference link

The following is the URL of ZooKeeper-related content that I have compiled and collected, which can be learned and referenced.
Chinese link:

http://agapple.iteye.com/blog/1111377
http://agapple.iteye.com/blog/1112032
http://agapple.iteye.com/blog/1292129
http://agapple.iteye. com/blog/1184023
http://agapple.iteye.com/blog/1184040
http://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/
http://luzengyi.blog.163. com/blog/static/529188201064113744373/
http://luzengyi.blog.163.com/blog/static/529188201061155444869/
English link:

http://zookeeper.apache.org/doc/trunk/zookeeperStarted.html
http://zookeeper.apache.org/doc/r3.3.4/zookeeperOver.html
http://zookeeper.apache.org/doc /r3.3.4/recipes.html
http://zookeeper.apache.org/doc/trunk/
http://wiki.apache.org/hadoop/ZooKeeper/Tutorial
http://wiki.apache.org/hadoop/ZooKeeper /FAQ
http://wiki.apache.org/hadoop/ZooKeeper/Troubleshooting

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326963423&siteId=291194637