preface

Fellow fellow, long time no see. In the last chapter, I mainly talked about some basic knowledge points of Zookeeper. Data model + primitive set + Watches mechanism. This chapter mainly talks about the cluster construction related knowledge.

The content of this paper mainly includes the following points:

  • Zookeeper running mode
  • They are built

Zookeeper operating mode

Zookeeper runs in single point mode and cluster mode.

  • Standalone mode – Zookeeper runs on a single server and is often used during development and testing. This mode is relatively simple, but does not guarantee high availability and recovery of Zookeeper services.

  • This mode is called the “replicated mode”; In this mode, Zookeeper runs on a cluster and is suitable for production environments.

    A server node in the same cluster is called quorum, which translates to “the quorum of a formal meeting,” and if you look at the two modes of THE ZAB protocol described in the next chapter, you’ll find this metaphor actually quite graphic.

    NOTE: In clustered mode, a minimum of three server nodes are required. And it is officially recommended that you cluster with an odd number of server nodes. The reason for this is related to Zookeeper’s read/write policies and consistency protocols, which will be explained in a later chapter.

Ii. Construction of Zookeeper

Single point pattern

If you have used ZooKeeper, you should know that it is very easy to start a ZooKeeper Server. In single point mode, you only need to do the following steps:

  1. Go to the official website to download the corresponding source compression package, and then upload to the server

  2. Decompress the file tar -zxvf zookeeper-***.tar.gz

  3. CFG, and create a zoo. CFG configuration file according to zoo_sample. CFG. The zoo. CFG configuration file is started by default

#Interval for maintaining heartbeat communication between Zookeeper servers or clients and servers.
#That's one heartbeat per tickTime.
tickTime=2000 
#Zookeeper Directory for saving data
dataDir=/data/zk/data 
#Zookeeper Directory for saving log files
dataLogDir=/data/zk/log
#Port that the client connects to the Zookeeper server
#Zookeeper listens to this port and receives client access requests
clientPort=2181
Copy the code
  1. Go to the bin directory in the source directory and runzkServer.shScript file
#Start the
zkServer.sh start
#Shut down
zkServer.sh stop
Copy the code
  1. View the current ZooKeeper status
[root@localhost bin]# sh zkserver. sh status ZooKeeper JMX enabled by default Using config: /opt/zookeeper-3.4.8/bin/.. /conf/zoo.cfg Mode: standaloneCopy the code

Mode: Standalone Shows the current node startup type.

The cluster structures,

The configuration in cluster mode is not different from that in single point mode. If you follow the minimum requirements, perform the steps in single point mode on three servers. The configuration files of each server in the same cluster are similar.

The following descriptions refer to the follower and leader nodes, which belong to the node types in the cluster. The specific definitions will be detailed in the following chapters.

In addition to the above steps, we need to do the following steps

  1. The following table describes the parameters for adding clusters to the conf/zoo. CFG configuration file on each host
tickTime=2000
dataDir=/data/zk/data 
dataLogDir=/data/zk/log
clientPort=2181

#Cluster Configuration
#Initial connection time that allows followers to connect and synchronize to the leader
#For example, here is the time allowed for 5 ticks
initLimit=5
#Length of time for sending messages between the leader and followers, request and reply.
#If the follower cannot communicate with the leader within the set time, the follower is discarded.
#For example, here is the time allowed for 2 ticks
syncLimit=2
#Cluster information
server.1=zoo1:2888:3888
server.2=zoo2:2888:3888
server.3=zoo3:2888:3888

Copy the code

Server. id=host:port1:port2 Identifies information about different Zookeeper servers. Id is a number, indicating the server number. Host is the IP address of the server; As for port1 and port2, there are some articles on the Internet that confuse the meaning of port1 and port2.

Finally, note the two port numbers after each server name: ” 2888″ and “3888”. Peers use the former port to connect to other peers. Such a connection is necessary so that peers can communicate, for example, to agree upon the order of updates. More specifically, a ZooKeeper server uses this port to connect followers to the leader. When a new leader arises, a follower opens a TCP connection to the leader using this port. Because the default leader election also uses TCP, we currently require another port for leader election. This is the second port in the server entry.

Finally, let’s look at the two port numbers following the server name: “2888” and “3888”. The Zookeeper node uses the previous port (port1) to establish connections with other nodes. This connection must be established because the different nodes need to communicate with each other. For example, the follower node needs to “approve” the leader node’s update command. More specifically, this node is used for communication between the follower node and the leader node. When a leader node is present, the follower node establishes a TCP connection with the leader node on port1. Since the default leader election also uses TCP, we now need another port for the leader election, which is what port2 does.

In a nutshell,port1Communication port,port2Election port.

  1. You need to create a file named myID in the dataDir directory corresponding to each Zookeeper server. The file contains only one line of content and specifies its ID value, that is, the ID value in server.id=host:port1:port2. This ID value must be unique in the cluster.

  2. After confirming that zoo. CFG and myID files are created on each server, run the zkserver. sh start command to start the ZooKeeper Server.

  3. Run the zkserver. sh status command on the three nodes to check the status of the current node.

    • Node 1

    • Node 2

    • Node 3

    It can be seen that node 2 is the leader node, while node 1 and node 3 are follower nodes.

For details about cluster construction, see ZooKeeper Installation and Configuration

See the ZooKeeper Getting Started Guide for more information

conclusion

This chapter describes the two operating modes of Zookeeper and how to set up the two modes.

In the next chapter, we will introduce the advanced part of Zookeeper principle, including the read and write mechanism of Zookeeper cluster, knowledge analysis of ZAB protocol and other in-depth knowledge points.