Zookeeper installation

How Zookeeper is built

There are three ways to install Zookeeper, stand-alone mode, cluster mode and pseudo-cluster mode.

1.单机模式:Zookeeper只运行在一台服务器上,适合测试环境;

2.伪集群模式:就是在一台物理机上运行多个Zookeeper 实例。

3.集群模式:Zookeeper运行于一个集群上,适合生产环境,这个计算机集群被称为一个“集合体”(ensemble)。

Zookeeper achieves high availability through replication, ensuring that service continues as long as more than half of the machines in the ensemble are available. Why does it have to be more than half? This is related to Zookeeper's replication strategy: zookeeper ensures that every modification to the znode tree is replicated to more than half of the machines in the ensemble.

1. Construction of Zookeeper's stand-alone mode

(1) Decompression: tar -zxvf zookeeper-3.4.5.tar.gz

(2) Configuration file: Modify zoo_sample.cfg to zoo.cfg in the conf directory.

(3) Configure environment variables: For the convenience of future operations, we need to configure the environment variables of Zookeeper. The method is as follows. Add the following content to the /etc/profile file:

export ZOOKEEPER_HOME=/usr/local/zk

export PATH=.ZOOKEEPER_HOME/bin:JAVA_HOME/bin: PATH

(4) Start the ZooKeeper Server: zkServer.sh start; close the ZooKeeper Server: zkServer.sh stop

Second, the construction of the pseudo-cluster mode of Zookeeper

Zookeeper can not only run Zookeeper in stand-alone mode on a single machine, but also simulate the operation of Zookeeper in cluster mode on a single machine, that is, run different nodes on the same machine.

(1) Decompression: tar -zxvf zookeeper-3.4.5.tar.gz -C zk

(2) Create a new one in the zk directory, data0, data1, data2, log0, log1, log2

(3) Enter the conf directory and create a new zoo1.cnf/zoo2.cnf/zoo3.cnf in turn, and the modifications are as follows:

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/opt/zk/data0(每个配置文件不同)
dataLogDir=/opt/zk/log0(每个配置文件不同)
clientPort=2181(每个配置文件不同)
server.0=10.20.151.34:2887:7770
server.1=10.20.151.34:2888:7771
server.2=10.20.151.34:2889:7772

parameter:

initLimit: This configuration item is used to configure Zookeeper to accept clients (the client mentioned here is not the client that the user connects to the Zookeeper server, but the Follower server connected to the Leader in the Zookeeper server cluster) How long can the initial connection be tolerated The number of heartbeat intervals. When the Zookeeper server has not received the return information from the client after more than 10 heartbeats (that is, tickTime), it indicates that the client connection failed. The total time length is 5*2000=10 seconds.

syncLimit: This configuration item identifies the length of the message, request and response time sent between LeaderFollowers. The longest time length cannot exceed the time length of how many tickTimes. The total time length is 2*2000=4 seconds.

server.A=B: C: D: where A is a number, indicating the number of the server; B is the ip address of the server; C indicates the port through which the server exchanges information with the leader server in the cluster; D It means that if the leader server in the cluster hangs up, a port is needed to re-elect and elect a new leader, and this port is the port used to communicate with each other during the election. If it is a pseudo-cluster configuration, since B is the same, the communication port numbers of different Zookeeper instances cannot be the same, so they must be assigned different port numbers.

Since it is built on a single machine, the clientPort of each server needs a different value, as follows: server.0-2181, server.1->2182, server.2->2183

(4) In the cluster mode, it is necessary to determine which server is by myid. There is a value dataDir in the zoo.cfg configured above, and a new file myid is created under the specified path, and only the corresponding file needs to be written in the file. A value, such as in server.0, the value should be 0

[root@mo ~]# echo "1" >> /opt/zk/data0/myid

[root@mo ~]# echo "2" >> /opt/zk/data1/myid

[root@mo ~]# echo "3" >> /opt/zk/data2/myid

(5) Start

Under the distributed cluster, we only have one machine, and we have to run three Zookeeper instances on time. At this point, if using the stand-alone mode the startup command will not work. At this point, as long as the following three commands are used to run the previously configured Zookeeper service. As follows:

zkServer.sh start zoo1.conf
zkServer.sh start zoo2.conf
zkServer.sh start zoo3.conf

After running the first command, there will be some error exceptions. The reason for the exception information is that each instance of the Zookeeper service has global configuration information, and they will perform leader election operations anytime and anywhere when they start. At this point, the first Zookeeper to start needs to communicate with the other two Zookeeper instances. However, the other two Zookeeper instances have not been started, so this strange message is generated.
We can just ignore it. After the Zookeeper instances of "No. 2" and "No. 3" in the figure are started, the corresponding abnormal information will naturally disappear. At this point, you can use the following three commands to query.

zkServer.sh status zoo1.conf
zkServer.sh status zoo2.conf
zkServer.sh status zoo3.conf

(6) Connect to the Zookeeper
cluster connection and connect to any server in the cluster.

[root@mo zookeeper-3.4.6]#./bin/zkCli.sh -server 192.168.200.128:2181

Precautions

Three servers are deployed on one machine. It should be noted that each configuration file we use simulates a machine when the cluster is in distributed mode, that is to say, a single machine and multiple Zookeeper instances are running on it. However, it must be ensured that the various port numbers of each configuration document cannot conflict, except that the clientPort is different, and the dataDir is also different. In addition, create a myid file in the directory corresponding to dataDir to specify the corresponding Zookeeper server instance.

(1) clientPort port: If multiple servers are deployed on one machine, each machine needs a different clientPort, such as server1 is 2181, server2 is 2182, server3 is 2183,

(2) dataDir and dataLogDir: dataDir and dataLogDir also need to be distinguished, and the data files and log files are stored separately, and the paths corresponding to these two variables of each server are different.

(3) server.X and myid: The number server.X corresponds to the number in data/myid. If 0, 1, and 2 are written in the myid files of the three servers, then the zoo.cfg in each server should be equipped with server.0, server.2, and server.3. Because on the same machine, the 2 ports connected behind, the 3 servers should not be the same, otherwise the ports will conflict.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325020430&siteId=291194637