preface
Record the process of setting up a Zookeeper cluster.
What is Zookeeper?
ZooKeeper is a distributed, open source distributed application coordination service. It is an open source implementation of Google’s Chubby and an important component of Hadoop and Hbase. It provides consistency services for distributed applications, including configuration and maintenance, domain name service, distributed synchronization, and group service.
Key points: Distributed application coordination service, in distributed system, usually need a general service registration and discovery center, so Zookeeper is acting as such a role, domestic very famous distributed framework Dubbo also suggested using Zookeeper as the service registration and discovery center.
Environment to prepare
Prepare three Linux VMS and download the ZooKeeper compressed package from the official website. If you do not know how to download the zooKeeper compressed package from the official website, you can reply to the Java tool from the official account “Zhang Shaolin” to obtain the download link of the web disk
OS | The host name | ip | Zookeeper/port |
---|---|---|---|
centos7 | LIHETEST6 | 192.168.2.216 | Zookeeper – 3.4.11. Tar. Gz / 2181 |
centos7 | LIHETEST7 | 192.168.2.217 | Zookeeper – 3.4.11. Tar. Gz / 2181 |
centos7 | LIHETEST8 | 192.168.2.218 | Zookeeper – 3.4.11. Tar. Gz / 2181 |
Configure the hosts mapping for the server
Perform the following operations on the three servers. LIHETEST6 is used as an example:
- To view the host name:
hostname
vim /etc/hostname
Delete the original content and add the host name:LIHETEST6
- Set hostname and IP mapping:
vim /etc/hosts
Add content at the end of the file in the format of IP address host name (separated by Spaces), save the configuration and exit, for example:192.168.2.216 LIHETEST6
- Restart the network service:
systemctl restart network
Single deployment
Upload the Zookeeper package to the server and decompress it
// Upload the compressed package
scp zookeeper-3.4.11.tar.gz root@192.1682.219: / home/tools// Log in to the server directory and decompress
tar -zxvf zookeeper-3.4.11.tar.gz
// Go to the zooKeeper home directory
cd zookeeper-3.411.Copy the code
Unzip it and see what it has:
We just need to focus on these two directories:
- Bin directory: script execution directory in which to place some
Zookeeper
Startup scripts, etc. - Conf directory: configuration file directory
Run the /bin/zkserver. sh start command to start Zookeeper. The following information is displayed:
Hint could not be found.. /bin/ zkserver. sh status /bin/ zkserver. sh status
Zookeeper failed to start because the configuration file could not be found. CFG,./bin/ zkserver. sh start uses this configuration file by default when the command is started, but this file is not available in the./conf directory during initialization. Run cp./conf/zoo_sample. CFG./conf/zoo. CFG to copy the./conf/zoo. CFG file and run the following command to start Zookeeper:
Zookeeper has been started in single-machine mode.
Cat zookeeper.out:
Zookeeper, binding on port 2181, check the process whether there is: netstat LNTP | grep, 2181, the results are as follows:
tcp6 0 0: : :2181 :::* LISTEN 27201/java
Copy the code
Configuration file Parsing
- TickTime: indicates the number of milliseconds for the client and server to maintain the heartbeat
- InitLimit: indicates the number of heartbeats initially tolerated
- SyncLimit: Waits for the maximum number of tolerated heartbeats
- DataDir: indicates the directory where snapshots are stored. The default directory is a temporary directory. You are advised to customize the directory
- ClientPort: The port exposed to clients
Single-node pseudo-cluster deployment
Sometimes we will build a pseudo cluster on the local machine for the project code test, so we only need to start three Zookeeper with different configurations, as long as the port is different, it can be regarded as different programs.
CFG, zoo2. CFG, and zoo3. CFG configuration files are added in the./conf directory. The dataDir directories are different in turn, and the clientPort values are 2181,2182,2183. Run the./bin/ zkserver. sh start conf/zoo3. CFG command to start three Zookeeper configuration files in sequence. Run netstat -lntp to check port monitoring:
You can see that three ZooKeepers are started that listen on different ports.
Multi-node cluster deployment
In a real production environment, we usually build a multi-machine cluster for the obvious purpose of improving fault tolerance.
The Zookeeper service is installed on each of the three machines, which form a small cluster.
Modify the./conf/zoo. CFG configuration file on each machine as follows:
initLimit=10 // The number of heartbeats initially tolerated
syncLimit=5 // Wait for the maximum number of heartbeats to be tolerated
dataDir=/home/wwwroot/easywits/tools/zookeeper-3.4.11/data // Data store directory
dataLogDir=/home/wwwroot/easywits/tools/zookeeper-3.4.11/logs // Data log file directory
clientPort=2181 // ZooKeeper listening port
server.1=192.1682.216:2888:3888 // Host IP address/heartbeat connection port between services/data port
server.2=192.1682.217:2888:3888
server.3=192.1682.218:2888:3888
Copy the code
Respectively in each server data directory/home/below/easywits/tools/zookeeper – 3.4.11 / data in new called myid text files, content is 0, in turn, this is the only logo per zookeeper service in the cluster, It cannot be repeated. Take the first machine as an example:
echo "0" > /home/wwwroot/easywits/tools/zookeeper-3.4.11/data/myid
Copy the code
Run the./bin/ zkserver. sh start command to start Zookeeper
Run the./bin/ zkserver. sh status command to check the Zookeeper status of each server. The following information is displayed:
Using config: /home/wwwroot/easywits/tools/zookeeper-3.4.11/bin/.. /conf/zoo.cfg Mode: follower Using config: /home/wwwroot/easywits/tools/zookeeper-3.4.11/bin/.. /conf/zoo.cfg Mode: leader Using config: /home/wwwroot/easywits/tools/zookeeper-3.4.11/bin/.. /conf/zoo.cfg Mode: followerCopy the code
The startup mode can be seen. The first and the third are followers, while the second is leader
Note: In a Zookeeper cluster, the number of machines can be an odd number. In a three-machine cluster, only one of the machines can be suspended. If the active node fails, the remaining two services will re-elect one as the active node.
conclusion
Zookeeper
Startup failure can be in./zookeeper.out
Abnormal file viewing logs- For every machine
myid
The content is the unique identifier of the service and cannot be repeated - Make sure that every machine must be able to
ping
Please turn off the firewall if necessary - On ali cloud server, please use the Intranet
ip
configure - Ensure that the port is not occupied
Zookeeper
The number of cluster machines can be an odd number. For details, seewww.cnblogs.com/LeeScofiled…
Ps: As for the tools needed, you can reply to the Java tool on the public account “Zhang Shaolin” to obtain the download link of the web disk
The last
Record a wave of Zookeeper construction process, the follow-up will be updated from time to time original articles, welcome to pay attention to the public account “Zhang Shaolin students”!