In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the method of Zookeeper configuration and cluster management". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the configuration and cluster management method of Zookeeper".
4.1 profile
After ZooKeeper is installed, you can find a file called "zoo_sample.cfg" in the conf folder of the installation directory, which is the template for the ZooKeeper configuration file.
When ZooKeeper starts, it loads "conf/zoo.cfg" as the configuration file by default, so you need to make a copy of "zoo_sample.cfg", name it "zoo.cfg", and then set the configuration items as needed.
The configuration item is simple and is described as follows:
TickTime=2000
This time is used as the interval between ZooKeeper servers or between clients and servers to maintain a heartbeat, that is, a heartbeat is sent at each tickTime time. In milliseconds.
InitLimit=10
This configuration item is used to configure the maximum number of heartbeat intervals that Leader can tolerate when accepting Follower initialization connections. When the Leader has not received the return message from Follower after the length of 10 heartbeats (that is, tickTime), the Follower connection failed. The total length of time is 5 "2000" 10 seconds.
SyncLimit=5
This configuration item identifies the length of time for sending messages, requests and replies between Leader and Follower, which cannot exceed the maximum number of tickTime. The total length of time is 5, 000, 000, 10 seconds.
DataDir=/tmp/zookeeper
As the name implies, it is the directory where ZooKeeper stores the data, the folder where the in-memory database snapshots are stored, and the myid files used for the cluster are also stored in this folder. By default, ZooKeeper also saves log files for write data in this directory. Note: a configuration file can contain only one dataDir, even if it is commented out.
ClientPort=2181
This port is the port on which the client connects to the ZooKeeper server. ZooKeeper listens on this port and accepts requests for access from the client.
MaxClientCnxns=60
The maximum number of client connections, default is 60.
Autopurge.snapRetainCount=3
Autopurge.purgeInterval=1
The client will generate a lot of logs in the process of interacting with ZooKeeper, and ZooKeeper will save the data in memory as snapshot, which will not be deleted automatically, so there will be more and more data on disk. However, you can set these two parameters to let zookeeper delete the data automatically. Autopurge.purgeInterval is to set the number of hours to clean. Autopurge.snapRetainCount sets how many snapshot are retained, and the previous one is deleted.
4.2 Server command
The "zkServer.sh" script is used to perform operations such as starting, stopping and status checking of Zookeeper.
With the "zkServer.sh help" command, you can view supported parameters:
As you can see, the parameters that can be attached to "zkServer.sh" are:
(1) start: used to start the server
(2) stop: used to stop the server
(3) restart: used to restart the server
(4) status: used to view server status
And other parameters for foreground startup, update, and so on.
For example, start the ZooKeeper server with the command "zkServer.sh start", which can be followed by parameters to specify the path to the configuration file, such as "zkServer.sh start.. / conf/ZooKeeper.cfg", which means to use ZooKeeper.cfg as the configuration file. If you do not specify the path, the "conf/zoo.cfg" file is loaded by default:
Use "zkServer.sh stop" to stop the server:
4.3 client command
Use the command "zkCli.sh-server 127.0.0.1 IP 2181" to connect to a ZooKeeper server with a port of "127.0.0.1" and a port of "2181". If you connect port 2181 of this machine, the following parameters can be omitted. Such as:
At this point, enter "help" to view the command parameters:
4.3.1 View node list
As mentioned earlier, ZooKeeper maintains a tree data structure with a root node of "/".
"ls path" is used to view all direct child nodes under the path path:
It can be seen that when the system is initialized, a node named "zookeeper" is automatically created under the root node to store the management information of ZooKeeper. Therefore, users can no longer create child nodes with the same name under the root node.
4.3.2 create a new node
"create path data" is used to create a new node under the path path, carrying the data data.
For example, create a new node called "firstNode" under the root node, and the stored data is "HelloWorld":
. / zkClient.sh-server 127.0.01
Create / firstNode HelloWorld
4.3.3 View node data
"get path" is used to get the data under the path node, for example:
Get / firstNode
In addition to returning the data stored by the node, there is also a series of meta-information, such as "cZxid" and "ctime" (two representations) that represent the creation time of the node, and "mZxid" and "mtime" of the modification time of the node.
4.3.4 modify node data
"set path data" is used to change the data under the path node to data.
For example, change the data under "/ firstNode" to "WorldHello":
Set / firstNode WorldHello
4.3.5 Delete a node
"delete path" is used to delete the path node.
For example, delete the "/ firstNode" node:
Delete / firstNode
In addition, there are other commands for setting the node ACL, viewing the node status, and so on, and you can consult the relevant manual when needed.
4.4 ZooKeeper four-word command
ZooKeeper supports the interaction of certain four-character command letters with them. Most of them are query commands that are used to obtain the current status and related information of the ZooKeeper service. Users can submit corresponding commands to ZooKeeper through telnet or nc on the client side.
Such as:
ZooKeeper four-word command
Conf
Function description
Output details of the relevant service configuration
Cons
Lists the full connection / session details of all clients connected to the server. Including the number of packets received / sent, session id, operation delay, last operation execution, and so on.
Dump
List unhandled sessions and temporary nodes
Envi
Output details about the service environment (different from the conf command)
Reqs
List unprocessed requests
Ruok
Test whether the service is in the correct state. If so, the service returns "imok", otherwise no response is made.
Stat
Output a list of clients about performance and connectivity
Wchs
List the details of server watch
Wchc
List the details of the server watch through session, and its output is a list of watch-related sessions
Wchp
Lists the details of the server watch by path. It outputs a path related to session
For example, view configuration information:
"echo conf | nc 127.0.0.1 2181":
The command provided by nc for the "NetCat" tool, which usually comes with NetCat in Linux distributions. Known as "Swiss knife" among network tools, NetCat is designed as a simple and reliable network tool that can transmit read and write data through TCP or UDP protocols.
This command means that the "conf" command is passed to port 2181 of 127.0.0.1 (that is, the native ZooKeeper service port) and the response is printed out:
Run an instance of ZooKeeper on a machine, called Standalone mode. There is a fatal flaw in stand-alone mode. Once the only instance is down, all applications that rely on ZooKeeper will be wiped out.
In practical applications, the cluster mode is generally used to deploy ZooKeeper, and the Server in the cluster is odd (2N+1). As long as most of the Server in the cluster is alive, the cluster can provide services to the outside world.
One ZooKeeper instance is deployed on each machine, and multiple machines form a cluster, which is called a fully distributed cluster. In addition, multiple ZooKeeper instances can be deployed on a single machine to run in pseudo-cluster mode.
5.1 Cluster configuration
Let's build a pseudo-distributed cluster of zookeeper with 3 instances.
First, you need to create different profiles for the three instances:
The configuration items of zk1.cfg are as follows: the configuration items of tickTime=2000 initLimit=10 syncLimit=5 dataDir=/zk1/dataDir clientPort=2181 server.1=127.0.0.1:2888:3888 server.2=127.0.0.1:2889:3889 server.3=127.0.0.1:2890:3890zk2.cfg are as follows: tickTime=2000 initLimit=10 syncLimit=5 dataDir=/zk2/dataDir clientPort=2182 server.1=127.0.0.1:2888:3888 server.2=127.0.0.1:2889:3889 server.3=127.0.0.1:2890:3890zk3.cfg configuration The items are as follows: tickTime=2000initLimit=10syncLimit=5dataDir=/zk3/dataDirclientPort=2183server.1=127.0.0.1:2888:3888server.2=127.0.0.1:2889:3889server.3=127.0.0.1:2890:3890
Because it is deployed on the same machine, the dataDir and clientPort of each instance should be distinguished, and the rest of the configuration should remain the same.
It should be noted that all the instances in the cluster provide services as a whole, and each instance in the cluster is connected to each other, so the mapping of all instances should be listed in each configuration file.
At the end of each configuration file, there are several lines of "server.A=B:C:D" configuration, where An is a number indicating which server this is, B is the ip address of the server, and C represents the port on which the server exchanges information with the Leader servers in the cluster. D means that in case the Leader server in the cluster dies, a port is needed for re-election to select a new Leader, and this port is the port through which the servers communicate with each other during the election. If it is a pseudo-cluster configuration, because B is all the same, the communication port numbers of different Zookeeper instances cannot be the same, so they should be assigned different port numbers.
In addition to modifying the zoo.cfg configuration file, a myid file should be configured in the cluster mode. There is only one data in the file under the dataDir directory, which is the value of A (server number). When Zookeeper starts, it will read this file and compare the data with the configuration information to determine which Server it is.
In the above example, you need to create a new myid file under each instance's own dataDir directory, and enter "1", "2" and "3" respectively.
5.2 Cluster startup
Start three instances in turn:
View Server status:
It can be seen that in today's cluster, zk2 plays the role of Leader, while zk1 and zk3 play the role of Follower.
Use three clients to connect to three Server. Under the client of zk1, add a "/ newNode" node to store data "zk1":
On the client side of zk2 and view the node:
On the client side of zk3 and view the node:
It can be seen that the Server in the cluster keeps the data synchronized.
5.3 Cluster disaster recovery
What happens if we shut down zk2, which is a Leader?
As you can see, the cluster automatically completes the switch, and the zk3 becomes Leader. In practical application, if the Leader in the cluster goes down, or if the Leader loses contact with more than half of the Follower, it will trigger the election process of the ZooKeeper and continue to serve the external service after electing a new Leader.
What will happen if we shut down zk3 again?
It can be seen that after shutting down zk3, since there is only one available Server in the cluster (less than half of the total number of clusters), the cluster will be unavailable.
Thank you for reading, the above is the content of "what is the configuration and cluster management method of Zookeeper". After the study of this article, I believe you have a deeper understanding of what the configuration and cluster management method of Zookeeper is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.