[Detailed picture and text] Zookeeper cluster construction (CentOs6.3)

Introduction to Zookeeper:

Zookeeper is a distributed coordination service that provides coordination services for users' distributed applications.

A. zookeeper serves other distributed programs

B. Zookeeper itself is a distributed program (as long as more than half of the nodes survive, zk can serve normally)

C. The services provided by Zookeeper include: master-slave coordination, dynamic online and offline of server nodes, unified configuration management, distributed shared locks, unified name service...

D. Although it can provide various services, zookeeper actually only provides two functions at the bottom:

  • Manage (store, read) data submitted by user programs;
  • And provide data node monitoring services for user programs;

Zookeeper cluster mechanism

  • The role of the Zookeeper cluster: 1 leader and multiple followers

  • As long as more than half of the nodes in the cluster survive, the cluster can provide services. Therefore, it is suitable to choose an odd number of machines in the cluster to avoid the situation of the same number of votes in the leader election.

Zookeeper election mechanism

  1. Server 1 is started, and only one of its servers is started at this time, and there is no response to the report it sends, so its election status is always LOOKING
  2. Server 2 starts, it communicates with server 1, which was started at the beginning, and exchanges its election results with each other. Since neither has historical data, server 2 with a larger id value wins, but because it does not reach more than half of the servers. Agree to elect it (more than half of them are 3 in this example), so servers 1, 2 continue to remain in the LOOKING state.
  3. Server 3 is started. According to the previous theoretical analysis, server 3 becomes the leader of servers 1, 2, and 3. The difference from the above is that three servers elected it at this time, so it became the leader of this election.
  4. Server 4 is started. According to the previous analysis, in theory, server 4 should be the largest among servers 1, 2, 3, and 4. However, since more than half of the servers have elected server 3, it can only receive the life of the younger brother. .
  5. Server 5 starts, like 4, when the younger brother.

To sum up:

  • Start 3 machines in the cluster successively, then machine 2 is the leader
  • Start 7 machines in the cluster successively, then machine 4 is the leader

Zookeeper cluster construction

1. Preliminary preparation

  • 3 virtual machines (Linux-----CentOs6.3)

    mini1----192.168.25.13
    
    mini2----192.168.25.14
    
    mini3----192.168.25.15
  • JDKs are installed on each of the three virtual machines

2. Upload the Zookeeper installation package to mini1

3. Unzip the installation package, and rename the unzipped directory to zk and put it in the /usr/local/ directory

tar -zxvf zookeeper-3.4.6.tar.gz
mv zookeeper-3.4.6 /usr/local/zk

4. Enter the conf directory under the zk directory to view the relevant configuration files

cd /usr/local/zk/conf

5. Copy zoo_sample.cfg and rename it to zoo.cfg

cp zoo_sample.cfg zoo.cfg

6. Modify the configuration file zoo.cfg (basically keep the default, just modify the two configurations in the figure)

vim zoo.cfg

  • tickTime: The basic event unit. This time is used as the time interval for maintaining heartbeats between Zookeeper servers or between the client and the server. A heartbeat is sent every tickTime.

  • initLimit: This configuration indicates the initial connection time to allow followers to connect and synchronize to the Leader, in tickTime units. When the initial connection time exceeds this value, it means that the connection fails.

  • syncLimit: This configuration item indicates the length of the request and response time when sending messages between the leader and the follower. If the follower cannot communicate with the leader within the set time, the follower will be discarded.

  • dataDir: the location where the data is stored

  • dataLogdDir: The location where log information is stored

  • server.a=b:c:d

    a:集群中此台服务器的编号
    
    B:此台服务器的ip
    
    C:Zookeeper服务器之间的通信端口
    
    D:Leader选举的端口

7. Create the data directory in the zk directory

mkdir /usr/local/zk/data

8. Create a myid file in the data directory and write the number of this server (filled in the zoo.cfg configuration file)

echo 1 > myid

9. Configure environment variables

vim /etc/profile
export ZOOKEEPER_HOME=/usr/local/zk
export PATH=.:$HADOOP_HOME/bin:$ZOOKEEPER_HOME/bin:$JAVA_HOME/bin:$PATH

10. Send the zk directory to the mini2 and mini3 machines respectively

scp -r /usr/local/zk/ 192.168.25.14:/usr/local/
scp -r /usr/local/zk/ 192.168.25.15:/usr/local/

11. Send the environment variable files to the mini2 and mini3 machines respectively

scp /etc/profile 192.168.25.14:/etc/
scp /etc/profile 192.168.25.15:/etc/

12. Go to the /usr/local/ directory of the mini2 machine and you can see the zk directory just passed over

13. Go to the data directory under the zk directory of mini2 and change the content of the myid file to the number of the machine mini2 (do the same for mini3)

cd /usr/local/zk/data/
echo 2 > myid(mini3的编号为3)

14. Start Zookeeper on the three machines respectively and view the status (reload environment variables before starting)

source /etc/profile
zkServer.sh start
zkServer.sh status

Since the Zookeeper cluster is built, we can see that mini3 is the leader in the cluster, and the other two are followers. If your startup sequence is to start mini1, mini2, and mini3 in sequence, then mini2 will be the leader in the cluster.


more recommendations:


Author: py xiaojie

Blog address: http://www.cnblogs.com/52mm/

This article is welcome to reprint, but this statement must be retained without the author's consent, and a link to the original text should be given in an obvious position on the article page

Guess you like

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