ZooKeeper installation and configuration cluster

Introduction:

       ZooKeeper is a distributed, open source distributed application coordination service, an important component of Hadoop and Hbase. It is a software that provides consistent services for distributed applications. It provides high-availability, high-performance, and ordered access data storage in a distributed environment, enabling distributed applications to achieve efficient coordination. The functions provided include: configuration maintenance, domain name service, distributed synchronization, group service, etc.

      The goal of ZooKeeper is to encapsulate complex and error-prone key services, and provide users with simple and easy-to-use interfaces and systems with high performance and stable functions, so that application developers can focus on the business logic of the application without having to care about coordination tasks. details. ZooKeeper includes a simple set of primitives, providing Java and C interfaces. In the ZooKeeper code version, interfaces for distributed exclusive locks, elections, and queues are provided. The code is in $zookeeper_home\src\recipes. Among them, there are two versions of distributed locks and queues, Java and C, and only the Java version for elections.

The advantages of ZooKeeper include:

  1. High reliability: ZooKeeper is a highly reliable distributed coordination service that can ensure data consistency and integrity.
  2. Scalability: ZooKeeper supports dynamic expansion and reduction, and can adjust resource allocation according to demand.
  3. Ease of use: ZooKeeper provides a simple set of primitives, enabling developers to quickly implement distributed applications.
  4. Security: ZooKeeper uses SSL to encrypt communication to ensure the security of data transmission.

ZooKeeper is a powerful and flexible distributed coordination service, suitable for large-scale distributed system scenarios.

download path

Index of /apache/zookeeper/zookeeper-3.7.1

Note:  The version starting from 3.5.5 should be downloaded with bin at the end, don't ask why it will report an error

 Starting from 3.5.5, the package with the bin name is what we want to download and use directly, and there are compiled binary packages in it. The tar.gz package of the previous version is only a source package and cannot be used directly.

Configure environment variables

Check whether %JAVA_HOME%\bin is configured in the system variable path. If not, configure it

illustrate

Currently, it is a pseudo-cluster of windows , which is deployed on each machine in a normal production environment, not in windows but in containers or linux.

Why is the cluster not a single machine? Of course, it is to ensure the high availability of the system. let's start......... 

unzip directory

 Open zoo_sample.cfg in conf, save it as zoo.cfg after modification, the original (zoo_sample.cfg) file will not be executed.

 

Parameter parsing zoo.cfg file

1.tickTime: CS communication heartbeat time The time interval between Zookeeper servers or between the client and the server to maintain the heartbeat, that is, a heartbeat is sent every tickTime. tickTime is in milliseconds. tickTime=2000

2.initLimit: The maximum number of heartbeats (the number of tickTime) that can be tolerated during the initial connection between the follower server (F) and the leader server (L) in the LF initial communication time limit cluster. initLimit=5

3.syncLimit: The maximum number of heartbeats (the number of tickTime) that can be tolerated between the request and the response between the follower server and the leader server in the LF synchronous communication time limit cluster. syncLimit=2

4.dataDir: Data file directory Zookeeper saves the data directory. By default, Zookeeper also saves the log files for writing data in this directory. dataDir=/home/zookeeper/zookeeper/zkdata

5.clientPort: client connection port The client connects to the port of the Zookeeper server. Zookeeper will listen to this port and accept the client's access request. clientPort=2181

6. Server name and address: cluster information (server number, server address, LF communication port, election port) rules are as follows: cluster distribution mode, server.id (number) = ip: cluster interaction port: election leader port server.1
= localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389

Change setting

Note: The single slash "\" in the path below will be treated as an escape character, and the "\\" slash can be resolved. ( No problem for a single machine, if it is a cluster, an error will be reported )

#数据文件目录
dataDir=D:\work\zookeeperColony\apache-zookeeper-3.7.1-bin-server1\data
#日志文件目录
dataLogDir=D:\work\zookeeperColony\apache-zookeeper-3.7.1-bin-server1\logs

#伪集群分布模式,server.id(注意是数字)=ip:集群交互端口:选举leader端口
server.1=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389

 Create data folders in server1, server2, and server3 respectively, and create myid files in data (note: no suffix is ​​required), and the contents of myid files are 1, 2, and 3 (corresponding to each server.id).

Note: Check whether the file exists when the cluster starts, if not, an error will be reported!

 

 Start flashback 

If the startup crashes, you can compile zkServer.cmd under the bin and add pause to see the error more clearly

 start instance

After all three instances are configured, an error is reported when starting zkServer.cmd under the bin

 The single machine can start normally, but the cluster reports an error.

Reason: The cluster finds the myid file and belches... because "\" is treated as an escape character, so the path is wrong, and the myid file cannot be found, so the startup fails. After modifying the path and restarting, everything is normal (change to double slashes \\)

Modify path:  dataDir=D:\\work\\zookeeperColony\\apache-zookeeper-3.7.1-bin-server1\\data

                 dataLogDir=D:\\work\\zookeeperColony\\apache-zookeeper-3.7.1-bin-server1\\logs

Start server1 server2 server3 ( Note: An error message will always pop up when starting the first service, this is because the election strategy does not matter ) The leader can only be elected when more than half of the machines are started

 server1

server2

Note:  When I started the server3 service, because my port was occupied, I  changed server.3=localhost:2289:3389 in zoo.cfg to 2286:2286 in server1 server2 server3 respectively to make it normal.

server3

Use the cmd command line to check whether the normal command is started: jps

 

 Check through the console that it is not one LEADING but two FOLLOWING

server1 is FOLLOWING

 server2 is LEADING    

 server3 is FOLLOWING

 In short, machines with smaller ids in the cluster will vote for machines with larger ids than their own. If server2 Leader is now closed, xunh server3 will take over the position of Leader. You can try it yourself.

The script starts three at a time

It is too troublesome to start one by one, creating files zookeeperstart.bat内容如下 

 Zookeeper election strategy

  1. Leader Election (Leader Election): In the ZooKeeper cluster, there is a master node (Master), which is responsible for coordinating the operation of the entire cluster. The master node will elect a leader (Leader), as its agent, responsible for processing transactions and tasks in the cluster. When a new client connects to the ZooKeeper cluster, it tries to establish a connection with the master node. If the connection is successful, the client sends a request to become the new leader. If the master node has elected a leader, the client will wait until the leader is installed. If the leader has not yet taken office, the client will wait until the leader election is complete.
  2. Configuration Management Election: In a ZooKeeper cluster, there may be multiple clients connected to the cluster at the same time, and some configuration information needs to be shared. In order to ensure the consistency and reliability of these configuration information, the ZooKeeper cluster will adopt the configuration management election (Configuration Management Election) strategy. Under this strategy, when a client connects to the ZooKeeper cluster, it tries to establish a connection with the master node. If the connection is successful, the client sends a request to become the new leader. If the master node has elected a leader, the client will wait until the leader is installed. If the leader has not yet taken office, the client will wait until the leader election is complete.
  3. Majority Election: In a ZooKeeper cluster, if multiple clients send requests at the same time and want to become the new leader, they will compete according to certain rules. Specifically, these rules may include: timestamp, first-come-first-served (Round Robin), etc. Under this strategy, multiple clients are sorted according to their request timestamps, and the client with the earliest request becomes the new leader. If two or more clients request the same timestamp, ZooKeeper will adopt a "majority election" strategy to elect a leader.
  4. Throne Election: In a ZooKeeper cluster, only one client can become the new leader. This strategy is similar to the "leader election" strategy, but when a client becomes the leader, it can completely control the entire ZooKeeper cluster, including resource allocation, transaction processing, etc. This strategy is generally not recommended as it can lead to instability and unpredictability of the cluster.

Summary: ZooKeeper's election strategy depends on specific application scenarios and requirements. When choosing an election strategy, factors such as performance, reliability, and security need to be considered. Different strategies will have different impacts on the operation of the ZooKeeper cluster, and need to be selected according to the actual situation.

Zookeeper ZAB protocol

Zookeeper's ZAB protocol is a distributed consensus protocol used to ensure data consistency among multiple Zookeeper instances in a distributed environment. It mainly includes two functions of atomic broadcast and crash recovery.

Atomic broadcast:

The ZAB protocol uses an atomic broadcast mechanism to ensure data consistency among multiple Zookeeper instances in a distributed environment. Specifically, when a Zookeeper instance (master node) needs to broadcast data to other Zookeeper instances (slave nodes), the master node will atomically broadcast the data to all slave nodes. This way, all slave nodes are able to synchronize data, ensuring data consistency across the cluster.

Crash recovery:

When the master node crashes, the ZAB protocol will automatically trigger the crash recovery mechanism. In this case, the crashed primary node will replicate its data to the standby node and re-elect a new primary node. In this way, even if the master node crashes, the entire cluster can still maintain data consistency.

Summary: The ZAB protocol ensures data consistency and reliability in the Zookeeper cluster through atomic broadcast and crash recovery mechanisms. This makes Zookeeper ideal for high availability and reliability deployments in distributed environments.

Zookeeper data model

  •  ZooKeeper is a tree directory service. Its data model is very similar to the Unix file system directory tree and has a hierarchical structure.
  • Each node here is called: ZNode, and each node will save its own data and node information.
  • A node can have child nodes, and also allows a small amount (1MB) of data to be stored under the node

The data model of ZooKeeper mainly includes the following parts:

1. Name Space: ZooKeeper's data model is similar to a file system, using Name Space to represent different data objects. Each Name Space consists of a set of unique identifiers, called nodes (Node). A node is the basic unit in a ZooKeeper tree, and each node has a unique numeric identifier.

2. Data Store: ZooKeeper data is stored in nodes called Data Stores. Each data storage node is a file for storing data and metadata information.

A data storage node usually consists of the following parts:

a. Data: Data storage nodes store data objects, such as files and hash tables.

b. MetaData: The data storage node also contains metadata information, such as file permissions, owners, etc.

c. ACL: ACL (Access Control List) is a data structure used to define permissions between nodes. In ZooKeeper, ACLs are used to restrict different users' access to nodes.
3. Node: Each node in the ZooKeeper tree consists of a unique numeric identifier called Znode. Znode is the basic unit in the ZooKeeper tree, representing a data object.

Znode consists of the following parts:

a. Stat: Znode contains a metadata information, such as file size, modification time, etc.

b. Data: Znode stores actual data objects.

c. Children: Znode can contain child nodes for storing child data objects or other Znodes.
4. Zxid: Zxid is a globally unique identifier used to ensure the uniqueness of each event in the ZooKeeper tree. Zxid is used to generate sequence numbers for use during event notification and leader election.

Note: ZooKeeper's data model organizes data by using a tree structure, each node represents a data object, and nodes are related to each other through parent-child relationships. This structure guarantees the consistency and event-driven behavior of all data in the distributed system .

Four types of Zookeeper nodes 

  1. Temporary nodes: Temporary nodes are used to store session data and events. Their life cycle is bound to the client session. Once the session ends, these nodes will be automatically deleted.
  2. Persistent nodes: Persistent nodes are used to store long-term data, such as state information, configuration data, etc. They are persistent, and nodes remain in ZooKeeper even if client sessions are disconnected.
  3. Sequence Node: A sequence node is a group of nodes linked together according to a predetermined order. They are used to implement a distributed lock mechanism. When multiple clients request the same resource, only one client can obtain the lock and perform operations, while other clients need to wait.
  4. Temporary Sequence Nodes: Temporary Sequence Nodes are a special type of Sequence Nodes that are both temporal and sequential. Temporary sequence nodes are converted into permanent sequence nodes when certain conditions are met, during which time they can be shared by multiple clients.

Zookeeper node type

  1. Persistent nodes (Persistent): are the most common type of nodes in Zookeeper, they exist permanently on the server until deleted. These nodes are used to store data and meta-information such as membership, permissions, etc.
  2. Ephemeral: Ephemeral nodes usually live on the server for a short time, and they are used to create temporary data and relationships, such as subscription relationships or publish/subscribe relationships. Temporary nodes are quickly deleted after establishment to ensure the recoverability of the node state.
  3. Sequential nodes: Sequential nodes are created sequentially, and they are used to store ordered data in a distributed environment. Sequential nodes will be referenced and accessed in a predetermined order after they are created.
  4. Partition node (Partition): The partition node is used to divide the Zookeeper instance into multiple subsets, and each subset can be managed independently. Partitioning nodes can improve the scalability and fault tolerance of Zookeeper.
  5. Aggregating: The aggregation node is used to merge the data of multiple Zookeeper instances into a large aggregation instance. Aggregation nodes can reduce the use of network bandwidth and increase data transmission speed.
  6. Persistent Sequential Node (Persistent Sequential): A persistent sequential node is a node type between a persistent node and a sequential node. Once created, they retain data and metadata until deleted. These nodes are typically used to store ordered data, such as tasks or events.
  7. Container node (Container): A container node is a node dedicated to storing and managing shared resources. They can exist across multiple Zookeeper instances, improving resource shareability and management efficiency.
  8. Synchronous nodes (Synchronous): Synchronous nodes will remain in a synchronized state when they are created. They cannot receive new transactions, but can only receive crash retry transactions. Sync nodes are primarily used for transaction processing in test and development environments.
  9. Asynchronous nodes (Asynchronous): Asynchronous nodes will not maintain a synchronous state when they are created. They can receive new transactions and process them, but the commit and rollback of transactions are asynchronous. Asynchronous nodes are mainly used in scenarios with high real-time requirements, such as video streaming or real-time communication applications.

The above are some common node types of ZK, and different node types have different meanings and uses in ZK.

Zookeeper watch monitoring

A ZooKeeper watch is a mechanism for monitoring changes in a distributed system. ZooKeeper's watch monitoring mechanism can help applications achieve the following functions:

  1. Heartbeat detection: ZooKeeper monitors client connection status and performs heartbeat detection automatically. If a client disconnects, ZooKeeper will send a heartbeat packet to make sure the client is still running.
  2. Change detection: ZooKeeper supports multiple change detection methods, such as read, write, delete and other operations. When ZooKeeper detects a change in the distributed system, it notifies relevant clients.
  3. Auto-healing: ZooKeeper has an auto-healing mechanism that automatically fails over when a node in a distributed system fails. In this way, ZooKeeper can ensure high availability and reliability of distributed systems.

ZooKeeper's watch monitoring mechanism usually consists of the following components:

  1. Watcher: Watcher is the core component of ZooKeeper, which is used to monitor changes in distributed systems. It is responsible for monitoring the status, events and changes of nodes, and notifying relevant clients of this information.
  2. Event: Event is an event type in ZooKeeper that is used to describe changes in a distributed system. Events contain details about changes such as read, write, delete, etc.
  3. WatchManager: WatchManager is a component of ZooKeeper used to manage watch monitoring. It is responsible for creating, deleting, and restarting watchers, as well as coordinating communication between watchers.

ZooKeeper's watch monitoring mechanism can help applications implement functions such as distributed system monitoring, heartbeat detection, change detection, and automatic repair.

Roles in a Zookeeper cluster

  1. Leader: It is the core of Zookeeper cluster work and the only scheduler and processor of transactional requests (write operations). It ensures the order of cluster transaction processing and is responsible for voting initiation and resolution.
  2. Follower: It is another important role in the Zookeeper cluster. It is responsible for processing client non-transactional (read operation) requests, forwarding transaction requests to Leader, and participating in cluster Leader election voting.
  3. Observer: Non-transactional requests can be processed independently (read operations), and transactional requests will be forwarded to Leader for processing, but will not participate in any form of voting.
  4. Secret Manager: Responsible for joining, deleting, and exiting group members, and managing the consistency of shared data.
  5. Graceful exit: Responsible for an organized and orderly exit.

map

The above are the main roles in the Zookeeper cluster. They perform their respective duties and jointly ensure the normal operation of the Zookeeper cluster.

Guess you like

Origin blog.csdn.net/weixin_50002038/article/details/130472426