Running ZooKeeper in standalone mode is convenient for evaluation, some development, and testing. But in production, you should run ZooKeeper in replicated mode. 单机模式的ZooKeeper便于评估,开发和测试。但在生产中,我们应该以集群模式运行ZooKeeper。
一个 ZooKeeper 集群通常由一组机器组成,一般 3 台以上就可以组成一个可用的 ZooKeeper 集群了。每台集群的具有相同的Zookeeper配置文件。组成 ZooKeeper 集群的每台机器都会在内存中维护当前的服务器状态,并且每台机器之间都会互相保持通信。
重要的一点是,只要集群中存在超过一半的机器能够正常工作,那么整个集群就能够正常对外服务。
For replicated mode, a minimum of three servers are required, and it is strongly recommended that you have an odd number of servers. If you only have two servers, then you are in a situation where if one of them fails, there are not enough machines to form a majority quorum. Two servers is inherently less stable than a single server, because there are two single points of failure. 对于集群模式,至少需要三台服务器,强烈建议您拥有奇数个服务器。 如果您只有两台服务器,那么您处于这样的情况,如果其中一台服务器出现故障,则没有足够的机器来形成大多数法定人数。 两台服务器固然比单一服务器稳定性差,因为有两个单点故障。
集群模式的Zookeeper配置同样参考官方文档: http://zookeeper.apache.org/doc/r3.4.10/zookeeperStarted.html
集群模式所需的conf/zoo.cfg配置文件类似于单机模式下使用的配置文件,但有一些区别。
下面先在node1节点上进行配置:
[root@node1 zookeeper-3.4.10]# cd conf
[root@node1 conf]# vi zoo.cfg
[root@node1 conf]# cat zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/var/lib/zookeeper
dataLogDir=/var/log
clientPort=2181
server.1=node1:2888:3888
server.2=node2:2888:3888
server.3=node3:2888:3888
The entries of the form server.X list the servers that make up the ZooKeeper service. When the server starts up, it knows which server it is by looking for the file myid in the data directory. That file has the contains the server number, in ASCII. server.X的条目列出构成ZooKeeper服务的服务器。 当服务器启动时,它会通过查找数据目录中的文件myid来知道哪个服务器。 该文件包含服务器编号,以ASCII格式显示。
[root@node1 ~]# mkdir /var/lib/zookeeper
[root@node1 ~]# echo 1 > /var/lib/zookeeper/myid
[root@node1 ~]# cat /var/lib/zookeeper/myid
1
[root@node1 ~]#
然后,将配置好的Zookeeper分发到其他两个节点
[root@node1 ~]# scp -r /opt/zookeeper-3.4.10/ node2:/opt
[root@node1 ~]# scp -r /opt/zookeeper-3.4.10/ node3:/opt
在node2和node3节点上分别创建myid文件
[root@node2 ~]# mkdir /var/lib/zookeeper
[root@node2 ~]# echo 2 > /var/lib/zookeeper/myid
[root@node3 ~]# mkdir /var/lib/zookeeper
[root@node3 ~]# echo 3 > /var/lib/zookeeper/myid
在node2和node3节点上配置环境变量
[root@node2 ~]# vi /etc/profile.d/custom.sh
[root@node2 ~]# cat /etc/profile.d/custom.sh
#!/bin/bash
#Java path
export JAVA_HOME=/opt/jdk1.8.0_112
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib
#Hadoop path
export HADOOP_HOME=/opt/hadoop-2.7.3
export PATH=$PATH:$HADOOP_HOME/bin:$HADOOP_HOME/sbin
#zookeeper path
export ZOO_HOME=/opt/zookeeper-3.4.10
export PATH=$PATH:$ZOO_HOME/bin
[root@node2 ~]# source /etc/profile.d/custom.sh
vi /etc/profile.d/custom.sh cat /etc/profile.d/custom.sh
[root@node3 ~]# vi /etc/profile.d/custom.sh
[root@node3 ~]# cat /etc/profile.d/custom.sh
#!/bin/bash
#Java path
export JAVA_HOME=/opt/jdk1.8.0_112
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib
#Hadoop path
export HADOOP_HOME=/opt/hadoop-2.7.3
export PATH=$PATH:$HADOOP_HOME/bin:$HADOOP_HOME/sbin
#zookeeper path
export ZOO_HOME=/opt/zookeeper-3.4.10
export PATH=$PATH:$ZOO_HOME/bin
[root@node3 ~]# source /etc/profile.d/custom.sh
(1)启动
在每个节点执行命令zkServer.sh start
[root@node1 ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@node2 ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@node3 ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
(2)查看状态
在每个节点上执行命令zkServer.sh status
[root@node1 ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
[root@node2 ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: leader
[root@node3 ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
(3)通过jps命令查看进程
[root@node1 ~]# jps
2346 Jps
2255 QuorumPeerMain
[root@node2 ~]# jps
2216 QuorumPeerMain
2298 Jps
[root@node3 ~]# jps
2320 Jps
2265 QuorumPeerMain