System environment required by the server
1. This article is to build zookeeper cluster under Linux (CentOS6.5), if you want to know how to build zookeeper cluster under Windows, you can go to another piece of my article Zookeeper+kafka cluster
2.ZooKeeper is written in the Java language, so its operating environment requires the support of the Java environment, and each CentOS installation in this article is JDK1.8.
Zookeeper download
Zookeeper's official download address is: http://www.apache.org/dyn/closer.cgi/zookeeper/
This article is used under the CentOS6.5 zookeeper is 3.4.8 version, I uploaded to the csdn, welcome to download zookeeper-3.4.8.tar.gz.
Cluster and stand-alone
Zookeeper there are two modes of operation, one is cluster mode and the other is a stand-alone mode.
1. Cluster mode
My side uses the virtual machine with clone feature to prepare three centos6.5 Linux servers that can be networked with each other. are:
ip1:192.168.199.100
Ip2:192.168.199.101
ip3:192.168.199.102
The three servers are installed jdk1.8 environment, followed by the three servers on the installation of Zookeeper server, here only to demonstrate the installation operation in Ip1, the IP2 and IP3 installed on the same machine.
Create a new zookeeper directory under/usr/local, and unzip the zookeeper.3.4.8.tar.gz here, and modify the Zoo.cfg file:
[#root] Cd/usr/local
[#root] MkDir Zookeeper
[#root] Cd/root
[#root] CP Zookeeper3.4.5.tar.gz/usr/local/zookeeper
[#root] Cd/usr/local/zookeeper
[#root] Tar xzvf zookeeper3.4.5.tar.gz
[#root] RM zookeeper3.4.5.tar.gz
[#root] CD zookeeper3.4.5
[#root] CD conf
[#root] MV Zoo_sample.cfg zoo.cfg
For zoo.cfg file configuration, here is only one simple, the default Zoo.cfg file content is as follows:
/var contains data to be changed when the system is generally running. Usually the size of the directory in which the data resides is constantly changing or expanding.
/var/lib: The file to be changed when the system is in normal operation.
Make the following changes to the Zoo.cfg file:
In cluster mode, each machine in the cluster needs to perceive which machines the entire cluster is composed of, and in the configuration file, it can be configured in the following format, each of which represents a machine configuration:
Server.id=host:port:port
ID is called ServerID, used to identify the machine in the cluster through the serial number, on each zookeeper machine, you need to datadir the parameter specified in the directory to create a myID file, the file only one line of content, corresponding to each machine ServerID. For example, Server.1 's myID file content is "1".
Next, create a new myID file under the path specified by DataDir, and fill in the ID of the server:
[#root] Touch myID
[#root] VI myID
Because Server.1 corresponds to 192.168.199.100 this machine, so in 100 this machine's myID file fill 1 can
Follow the same steps to configure the Zoo.cfg and myID files for 101 and 102.
Start the cluster,
Proceed to the bin directory of each zookeeper machine in turn: (If you have configured the environment variable, you can use the zkserver.sh command directly, otherwise you will need to do it in the current directory)
./zkserver.sh Start
The results of the implementation are as follows:
After the server is started, you can use the Telnet command to test:
After installing the zookeeper cluster and want to enter the connection test, Telnet localhost 2181 found that the command is not telnet, so you need to install it yourself.
First, check if Telnet is installed on this machine
#rpm-qa | grep telnet
If nothing is displayed. Description you did not install Telnet
Second, start the installation
Yum Install xinetd
Yum Install Telnet
Yum Install Telnet-server
Third, after the installation of the Telnet service, the default is not to open the service, the following we need to modify the file to open the service.
Vim/etc/xinetd.d/telnet Modify Disable = yes to disable = no
Iv. need to activate XINETD service
# service xinetd restart or #/etc/rc.d/init.d/xinetd restart
I was in use for the first time there is a hint error, the second run will be fine.
Found this ZooKeeper instance was not currently serving requests connetction closed by Foregin host error, the reason for reporting this exception is that the cluster was not elected to leader, when This error message occurs when there is only one or less than half of the nodes in the cluster.
But why did not elect to leader it, the visual should be the port is not open, check the end is because the network is not through, then the firewall open port 2181,2888 and 38,883 ports.
Vi/etc/sysconfig/iptables
-A input-m state--state new-m tcp-p TCP--dport 2181-j ACCEPT
-A input-m state--state new-m tcp-p TCP--dport 2888-j ACCEPT
-A input-m state--state new-m tcp-p TCP--dport 3888-j ACCEPT
After saving, restart the Linux Firewall service with the following command:
/etc/init.d/iptables restart
or service iptables restart
First stop the zookeeper server first:
And then restart the boot, here is the point where there is a possibility to find the error, that is, the boot will be packet permssion denied error
This error is due to insufficient permissions to occur error, if the user doing this should have the right to start, then use CHOMD to add write permission to it, I am directly replaced by the root user to operate;
Su Root
To execute the start command:./zkserver. SH start
Note that the view to Mode property indicates follower, (cluster mode may also be leader,standalone)
2. Stand-alone
In the cluster mode above, a zookeeper cluster has been built.
In fact, the stand-alone mode is only a special cluster mode-only one machine cluster. After the two zookeeper machines, IP2 and IP3, are removed, only a cluster of ip1 zookeeper is left, which is called stand-alone mode.
When the stand-alone mode is started, the value of its Mode property is standalone.
3. Pseudo-Cluster mode
If there is a better machine on hand, then this time. If you are deploying as a stand-alone mode, the resources are obviously a bit wasteful, so you can use pseudo-clusters at this point.
Pseudo-cluster, is the cluster of all the machines are on a machine, but still with the characteristics of the cluster to provide services, such as the Zoo.cfg file can be set as follows:
server1.=192.168.199.100:2888:3888
server.2=192.168.199.100:2889:3889
server.3=192.168.199.100:2890:3890
corresponding to each Zookeeper program (three zookeeper on a machine)
This mode of cluster construction can refer to my other article Zookeeper+kafka cluster