1. Preparation:
1) Linux environment is required, ubuntu system is recommended. If you are learning, use Oracle's virtual box to install the virtual machine, and you can install it by looking for the installation document online, and the production environment will generally be installed.
2) JDK: Zookeeper requires java operating environment. It is recommended that above 1.6, configure JAVA_HOME, CLASSPATH, and PATH variables.
Environment variable configuration, .profile in the home directory can be:
export JAVA_HOME="/usr/lib/jvm/jdk1.7.0_40" export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export ZOOKEEPER_HOME="/data/apache/zookeeper" export PATH="$PATH:$JAVA_HOME/bin:$ZOOKEEPER_HOME/bin"
3) Zookeeper installation package: http://zookeeper.apache.org/releases.html download the stable version
2. Various decompressions, not to mention, it is recommended to establish a soft connection zk for the decompressed directory (with version number) to facilitate subsequent configuration and upgrade.
I decompressed it and placed it under: /data/apache/zookeeper, and modified the file permissions to run user permissions:
sudo chown -R zqgame:zqgame zookeeper
zqgame: zqgame is the user who runs zookeeper and the user's group. View permissions:
zqgame@develop-test-web:/data/apache$ ll total 16 drwxr-xr-x 4 zqgame zqgame 4096 Jan 17 12:41 ./ drwxr-xr-x 11 root root 4096 Dec 13 14:49 ../ drwxr-xr-x 11 zqgame zqgame 4096 Nov 20 15:50 hadoop-2.2.0/ drwxr-xr-x 12 zqgame zqgame 4096 Jan 17 12:56 zookeeper/ zqgame@develop-test-web:/data/apache$
3. Port check, usually used ports 2181, 2888, 3888, check whether they are occupied, and change the port in the configuration below.
netstat -ano | grep 2181
4. Single machine multi-instance configuration and startup:
#Generate a configuration file, the configuration file name is arbitrary, here is zoo.cfg cp /data/apache/zookeeper/conf/zoo_sample.cfg /data/apache/zookeeper/conf/zoo.cfg vi /data/apache/zookeeper/conf/ zoo.cfg
Configuration file content:
tickTime=2000 initLimit=5 syncLimit=5 dataDir=/data/apache/zookeeper/data # The directory needs to be created manually to store zk data, mainly snapshot clientPort=2181 # dataLogDir transaction log storage directory, the best configuration, transaction log writing The input speed seriously affects the performance of zookeeper dataLogDir=/data/apache/zookeeper/datalog server.1=192.168.130.170:2889:3889 server.2=192.168.130.170:2890:3890 server.3=192.168.130.170:2891:3891
Copy the configuration files to generate three configuration files: zoo-slave1.cfg: (the other two zoo-slave2.cfg and zoo-slave3.cfg).
zoo-slave1.cfg needs to set directories for dataDir and dataLogDir, the changes are as follows:
dataDir=/data/apache/zookeeper/data/slave1 dataLogDir=/data/apache/zookeeper/datalog/slave1 clientPort=2182
zoo-slave2.cfg :
dataDir=/data/apache/zookeeper/data/slave2 dataLogDir=/data/apache/zookeeper/datalog/slave2 clientPort=2183
zoo-slave3.cfg :
dataDir=/data/apache/zookeeper/data/slave3 dataLogDir=/data/apache/zookeeper/datalog/slave3 clientPort=2184
Configure the same local IP and different port numbers as above, here are three examples
How to distinguish the first instance? There must be an id file whose name must be myid
echo "1" > /data/apache/zookeeper/data/slave1/myid echo "2" > /data/apache/zookeeper/data/slave1/myid echo "3" > /data/apache/zookeeper/data/slave1/myid
Start quickly under three windows:
bin/zkServer.sh start zoo-slave1.cfg bin/zkServer.sh start zoo-slave2.cfg bin/zkServer.sh start zoo-slave3.cfg
Check the leader selected by zookeeper, and specify the configuration files separately through the following script, you can check which instance is the leader:
bin/zkServer.sh status zoo-slave1.cfg
You can see the following output information:
zqgame@develop-test-web:/data/apache/zookeeper$ bin/zkServer.sh status zoo-slave2.cfg JMX enabled by default Using config: /data/apache/zookeeper/bin/../conf/zoo-slave2.cfg Mode: leader
5. Distributed deployment:
It is similar to a single-machine multi-instance, but instead of three directories, only one directory, one configuration file, and three different IPs are needed. Distributed on different IPs, they are still different myids. Suppose I have three independent machines, then zoo.cfg (only a default configuration file zoo.cfg is needed) configuration file is as follows:
tickTime=2000 initLimit=5 syncLimit=5 dataDir=/data/apache/zookeeper/data # The directory needs to be created manually to store zk data, mainly snapshot clientPort=2181 # dataLogDir transaction log storage directory, the best configuration, transaction log writing The input speed seriously affects the performance of zookeeper dataLogDir=/data/apache/zookeeper/datalog server.1=192.168.130.101:2888:3888 server.2=192.168.130.102:2888:3888 server.3=192.168.130.103:2888:3888
Then write different myid on the three machines:
#Execute the following echo "1" in 192.168.130.101 > /data/apache/zookeeper/data/myid #Execute the following echo "2" in 192.168.130.102 > /data/apache/zookeeper/data/myid #In 192.168. 130.103 Execute the following echo "3"> /data/apache/zookeeper/data/myid
In this way, zookeeper is quickly started on the three machines. At this time, there is no need to specify the configuration file. The default configuration file is zoo.cfg:
bin/zkServer.sh start
Check the status after startup:
bin/zkServer.sh status
Enter zookeeper's shell client:
bin/zkCli.sh #Enter the local zookeeper shell
Enter the shell client of the specified machine:
bin/zkCli.sh -server 192.168.130.101
If there is an error in viewing the startup status, wait a while and then check again, because it takes a while to select the leader after zookeeper starts.