`
zgax
  • 浏览: 20864 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

ZooKeeper-3.3.4集群安装配置

 
阅读更多
转载自http://shiyanjun.cn/archives/469.html
ZooKeeper是一个分布式开源框架,提供了协调分布式应用的基本服务,它向外部应用暴露一组通用服务——分布式同步(Distributed Synchronization)、命名服务(Naming Service)、集群维护(Group Maintenance)等,简化分布式应用协调及其管理的难度,提供高性能的分布式服务。ZooKeeper本身可以以Standalone模式安装运行,不过它的长处在于通过分布式ZooKeeper集群(一个Leader,多个Follower),基于一定的策略来保证ZooKeeper集群的稳定性和可用性,从而实现分布式应用的可靠性。
有关ZooKeeper的介绍,网上很多,也可以参考文章后面,我整理的一些相关链接。
ZooKeeper的安装配置还算比较容易的,下面,我们简单说明一下ZooKeeper的配置。

ZooKeeper Standalone模式

从Apache网站上(zookeeper.apache.org)下载ZooKeeper软件包,我选择了3.3.4版本的(zookeeper-3.3.4.tar.gz),在一台Linux机器上安装非常容易,只需要解压缩后,简单配置一下即可以启动ZooKeeper服务器进程。
将zookeeper-3.3.4/conf目录下面的 zoo_sample.cfg修改为zoo.cfg,配置文件内容如下所示:

tickTime=2000
dataDir=/home/hadoop/storage/zookeeper
clientPort=2181
initLimit=5
syncLimit=2

上面各个配置参数的含义也非常简单,引用如下所示:

引用
tickTime —— the basic time unit in milliseconds used by ZooKeeper. It is used to do heartbeats and the minimum session timeout will be twice the tickTime.
dataDir —— the location to store the in-memory database snapshots and, unless specified otherwise, the transaction log of updates to the database.
clientPort —— the port to listen for client connections

下面启动ZooKeeper服务器进程:

cd zookeeper-3.3.4/
bin/zkServer.sh start

通过jps命令可以查看ZooKeeper服务器进程,名称为QuorumPeerMain。
在客户端连接ZooKeeper服务器,执行如下命令:


bin/zkCli.sh -server dynamic:2181

上面dynamic是我的主机名,如果在本机执行,则执行如下命令即可:


bin/zkCli.sh

客户端连接信息如下所示:
shirdrn@master:~/installation/zookeeper-3.3.4$ bin/zkCli.sh -server dynamic:2181
Connecting to dynamic:2181
2013-10-28 21:30:06,178 - INFO  [main:Environment@97] - Client environment:zookeeper.version=3.3.3-1203054, built on 11/17/2011 05:47 GMT
2013-10-28 21:30:06,188 - INFO  [main:Environment@97] - Client environment:host.name=master
2013-10-28 21:30:06,191 - INFO  [main:Environment@97] - Client environment:java.version=1.6.0_30
2013-10-28 21:30:06,194 - INFO  [main:Environment@97] - Client environment:java.vendor=Sun Microsystems Inc.
2013-10-28 21:30:06,200 - INFO  [main:Environment@97] - Client environment:java.home=/home/hadoop/installation/jdk1.6.0_30/jre
2013-10-28 21:30:06,203 - INFO  [main:Environment@97] - Client environment:java.class.path=/home/hadoop/installation/zookeeper-3.3.4/bin/../build/classes:/home/hadoop/installation/zookeeper-3.3.4/bin/../build/lib/*.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../zookeeper-3.3.4.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/log4j-1.2.15.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/jline-0.9.94.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/commons-lang-2.4.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/commons-collections-3.2.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/commons-cli-1.1.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/apache-rat-tasks-0.6.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/apache-rat-core-0.6.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../src/java/lib/*.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../conf:/home/hadoop/installation/jdk1.6.0_30/lib/*.jar:/home/hadoop/installation/jdk1.6.0_30/jre/lib/*.jar
2013-10-28 21:30:06,206 - INFO  [main:Environment@97] - Client environment:java.library.path=/home/hadoop/installation/jdk1.6.0_30/jre/lib/i386/client:/home/hadoop/installation/jdk1.6.0_30/jre/lib/i386:/home/hadoop/installation/jdk1.6.0_30/jre/../lib/i386:/usr/java/packages/lib/i386:/lib:/usr/lib
2013-10-28 21:30:06,213 - INFO  [main:Environment@97] - Client environment:java.io.tmpdir=/tmp
2013-10-28 21:30:06,216 - INFO  [main:Environment@97] - Client environment:java.compiler=<NA>
2013-10-28 21:30:06,235 - INFO  [main:Environment@97] - Client environment:os.name=Linux
2013-10-28 21:30:06,244 - INFO  [main:Environment@97] - Client environment:os.arch=i386
2013-10-28 21:30:06,246 - INFO  [main:Environment@97] - Client environment:os.version=3.0.0-14-generic
2013-10-28 21:30:06,251 - INFO  [main:Environment@97] - Client environment:user.name=hadoop
2013-10-28 21:30:06,254 - INFO  [main:Environment@97] - Client environment:user.home=/home/hadoop
2013-10-28 21:30:06,255 - INFO  [main:Environment@97] - Client environment:user.dir=/home/hadoop/installation/zookeeper-3.3.4
2013-10-28 21:30:06,264 - INFO  [main:ZooKeeper@379] - Initiating client connection, connectString=dynamic:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@bf32c
2013-10-28 21:30:06,339 - INFO  [main-SendThread():ClientCnxn$SendThread@1061] - Opening socket connection to server dynamic/192.168.0.107:2181
Welcome to ZooKeeper!
2013-10-28 21:30:06,397 - INFO  [main-SendThread(dynamic:2181):ClientCnxn$SendThread@950] - Socket connection established to dynamic/192.168.0.107:2181, initiating session
JLine support is enabled
2013-10-28 21:30:06,492 - INFO  [main-SendThread(dynamic:2181):ClientCnxn$SendThread@739] - Session establishment complete on server dynamic/192.168.0.107:2181, sessionid = 0x134b9b714f9000c, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: dynamic:2181(CONNECTED) 0] 

接着,可以使用help查看Zookeeper客户端可以使用的基本操作命令。

ZooKeeper Distributed模式

ZooKeeper分布式模式安装(ZooKeeper集群)也比较容易,这里说明一下基本要点。
首先要明确的是,ZooKeeper集群是一个独立的分布式协调服务集群,“独立”的含义就是说,如果想使用ZooKeeper实现分布式应用的协调与管理,简化协调与管理,任何分布式应用都可以使用,这就要归功于Zookeeper的数据模型(Data Model)和层次命名空间(Hierarchical Namespace)结构,详细可以参考http://zookeeper.apache.org/doc/trunk/zookeeperOver.html。在设计你的分布式应用协调服务时,首要的就是考虑如何组织层次命名空间。
下面说明分布式模式的安装配置,过程如下所示:
第一步:主机名称到IP地址映射配置
ZooKeeper集群中具有两个关键的角色:Leader和Follower。集群中所有的结点作为一个整体对分布式应用提供服务,集群中每个结点之间都互相连接,所以,在配置的ZooKeeper集群的时候,每一个结点的host到IP地址的映射都要配置上集群中其它结点的映射信息。
例如,我的ZooKeeper集群中每个结点的配置,以zk-01为例,/etc/hosts内容如下所示:

192.168.0.179   zk-01
192.168.0.178   zk-02
192.168.0.177   zk-03

ZooKeeper采用一种称为Leader election的选举算法。在整个集群运行过程中,只有一个Leader,其他的都是Follower,如果ZooKeeper集群在运行过程中Leader出了问题,系统会采用该算法重新选出一个Leader。因此,各个结点之间要能够保证互相连接,必须配置上述映射。
ZooKeeper集群启动的时候,会首先选出一个Leader,在Leader election过程中,某一个满足选举算的结点就能成为Leader。整个集群的架构可以参考http://zookeeper.apache.org/doc/trunk/zookeeperOver.html#sc_designGoals。
第二步:修改ZooKeeper配置文件
在其中一台机器(zk-01)上,解压缩zookeeper-3.3.4.tar.gz,修改配置文件conf/zoo.cfg,内容如下所示:


tickTime=2000
dataDir=/home/hadoop/storage/zookeeper
clientPort=2181
initLimit=5
syncLimit=2
server.1=zk-01:2888:3888
server.2=zk-02:2888:3888
server.3=zk-03:2888:3888

上述配置内容说明,可以参考http://zookeeper.apache.org/doc/trunk/zookeeperStarted.html#sc_RunningReplicatedZooKeeper。
第三步:远程复制分发安装文件
上面已经在一台机器zk-01上配置完成ZooKeeper,现在可以将该配置好的安装文件远程拷贝到集群中的各个结点对应的目录下:

cd /home/hadoop/installation/
scp -r zookeeper-3.3.4/ shirdrn@zk-02:/home/hadoop/installation/
scp -r zookeeper-3.3.4/ shirdrn@zk-03:/home/hadoop/installation/

第四步:设置myid
在我们配置的dataDir指定的目录下面,创建一个myid文件,里面内容为一个数字,用来标识当前主机,conf/zoo.cfg文件中配置的server.X中X为什么数字,则myid文件中就输入这个数字,例如:

shirdrn@zk-01:~/installation/zookeeper-3.3.4$ echo "1" > /home/hadoop/storage/zookeeper/myid
shirdrn@zk-02:~/installation/zookeeper-3.3.4$ echo "2" > /home/hadoop/storage/zookeeper/myid
shirdrn@zk-03:~/installation/zookeeper-3.3.4$ echo "3" > /home/hadoop/storage/zookeeper/myid

按照上述进行配置即可。
第五步:启动ZooKeeper集群
在ZooKeeper集群的每个结点上,执行启动ZooKeeper服务的脚本,如下所示:

shirdrn@zk-01:~/installation/zookeeper-3.3.4$ bin/zkServer.sh start
shirdrn@zk-02:~/installation/zookeeper-3.3.4$ bin/zkServer.sh start
shirdrn@zk-03:~/installation/zookeeper-3.3.4$ bin/zkServer.sh start

以结点zk-01为例,日志如下所示:


我启动的顺序是zk-01>zk-02>zk-03,由于ZooKeeper集群启动的时候,每个结点都试图去连接集群中的其它结点,先启动的肯定连不上后面还没启动的,所以上面日志前面部分的异常是可以忽略的。通过后面部分可以看到,集群在选出一个Leader后,最后稳定了。
其他结点可能也出现类似问题,属于正常。
第六步:安装验证
可以通过ZooKeeper的脚本来查看启动状态,包括集群中各个结点的角色(或是Leader,或是Follower),如下所示,是在ZooKeeper集群中的每个结点上查询的结果:
shirdrn@zk-01:~/installation/zookeeper-3.3.4$ bin/zkServer.sh status
JMX enabled by default
Using config: /home/hadoop/installation/zookeeper-3.3.4/bin/../conf/zoo.cfg
Mode: follower
 
shirdrn@zk-02:~/installation/zookeeper-3.3.4$  bin/zkServer.sh status
JMX enabled by default
Using config: /home/hadoop/installation/zookeeper-3.3.4/bin/../conf/zoo.cfg
Mode: leader
 
shirdrn@zk-03:~/installation/zookeeper-3.3.4$  bin/zkServer.sh status
JMX enabled by default
Using config: /home/hadoop/installation/zookeeper-3.3.4/bin/../conf/zoo.cfg
Mode: follower

通过上面状态查询结果可见,zk-02是集群的Leader,其余的两个结点是Follower。
另外,可以通过客户端脚本,连接到ZooKeeper集群上。对于客户端来说,ZooKeeper是一个整体(ensemble),连接到ZooKeeper集群实际上感觉在独享整个集群的服务,所以,你可以在任何一个结点上建立到服务集群的连接,例如:
shirdrn@zk-03:~/installation/zookeeper-3.3.4$ bin/zkCli.sh -server zk-01:2181
Connecting to zk-01:2181
2013-10-28 07:14:21,068 - INFO  [main:Environment@97] - Client environment:zookeeper.version=3.3.3-1203054, built on 11/17/2011 05:47 GMT
2013-10-28 07:14:21,080 - INFO  [main:Environment@97] - Client environment:host.name=zk-03
2013-10-28 07:14:21,085 - INFO  [main:Environment@97] - Client environment:java.version=1.6.0_30
2013-10-28 07:14:21,089 - INFO  [main:Environment@97] - Client environment:java.vendor=Sun Microsystems Inc.
2013-10-28 07:14:21,095 - INFO  [main:Environment@97] - Client environment:java.home=/home/hadoop/installation/jdk1.6.0_30/jre
2013-10-28 07:14:21,104 - INFO  [main:Environment@97] - Client environment:java.class.path=/home/hadoop/installation/zookeeper-3.3.4/bin/../build/classes:/home/hadoop/installation/zookeeper-3.3.4/bin/../build/lib/*.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../zookeeper-3.3.4.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/log4j-1.2.15.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/jline-0.9.94.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/commons-lang-2.4.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/commons-collections-3.2.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/commons-cli-1.1.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/apache-rat-tasks-0.6.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../lib/apache-rat-core-0.6.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../src/java/lib/*.jar:/home/hadoop/installation/zookeeper-3.3.4/bin/../conf:/home/hadoop/installation/jdk1.6.0_30/lib/*.jar:/home/hadoop/installation/jdk1.6.0_30/jre/lib/*.jar
2013-10-28 07:14:21,111 - INFO  [main:Environment@97] - Client environment:java.library.path=/home/hadoop/installation/jdk1.6.0_30/jre/lib/i386/client:/home/hadoop/installation/jdk1.6.0_30/jre/lib/i386:/home/hadoop/installation/jdk1.6.0_30/jre/../lib/i386:/usr/java/packages/lib/i386:/lib:/usr/lib
2013-10-28 07:14:21,116 - INFO  [main:Environment@97] - Client environment:java.io.tmpdir=/tmp
2013-10-28 07:14:21,124 - INFO  [main:Environment@97] - Client environment:java.compiler=<NA>
2013-10-28 07:14:21,169 - INFO  [main:Environment@97] - Client environment:os.name=Linux
2013-10-28 07:14:21,175 - INFO  [main:Environment@97] - Client environment:os.arch=i386
2013-10-28 07:14:21,177 - INFO  [main:Environment@97] - Client environment:os.version=3.0.0-14-generic
2013-10-28 07:14:21,185 - INFO  [main:Environment@97] - Client environment:user.name=hadoop
2013-10-28 07:14:21,188 - INFO  [main:Environment@97] - Client environment:user.home=/home/hadoop
2013-10-28 07:14:21,190 - INFO  [main:Environment@97] - Client environment:user.dir=/home/hadoop/installation/zookeeper-3.3.4
2013-10-28 07:14:21,197 - INFO  [main:ZooKeeper@379] - Initiating client connection, connectString=zk-01:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@bf32c
2013-10-28 07:14:21,305 - INFO  [main-SendThread():ClientCnxn$SendThread@1061] - Opening socket connection to server zk-01/192.168.0.179:2181
Welcome to ZooKeeper!
2013-10-28 07:14:21,376 - INFO  [main-SendThread(zk-01:2181):ClientCnxn$SendThread@950] - Socket connection established to zk-01/192.168.0.179:2181, initiating session
JLine support is enabled
[zk: zk-01:2181(CONNECTING) 0] 2013-10-28 07:14:21,872 - INFO  [main-SendThread(zk-01:2181):ClientCnxn$SendThread@739] - Session establishment complete on server zk-01/192.168.0.179:2181, sessionid = 0x134bdcd6b730000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null

[zk: zk-01:2181(CONNECTED) 0] ls /
[zookeeper]

当前根路径为/zookeeper。

总结说明

主机名与IP地址映射配置问题
启动ZooKeeper集群时,如果ZooKeeper集群中zk-01结点的日志出现如下错误:
java.net.SocketTimeoutException
        at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:109)
        at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:371)
        at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(QuorumCnxManager.java:404)
        at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader(FastLeaderElection.java:688)
        at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:622)
2013-10-28 06:37:46,026 - INFO  [QuorumPeer:/0:0:0:0:0:0:0:0:2181:FastLeaderElection@697] - Notification time out: 6400
2013-10-28 06:37:57,431 - WARN  [QuorumPeer:/0:0:0:0:0:0:0:0:2181:QuorumCnxManager@384] - Cannot open channel to 2 at election address zk-02/202.106.199.35:3888
java.net.SocketTimeoutException
        at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:109)
        at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:371)
        at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(QuorumCnxManager.java:404)
        at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader(FastLeaderElection.java:688)
        at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:622)
2013-10-28 06:38:02,442 - WARN  [QuorumPeer:/0:0:0:0:0:0:0:0:2181:QuorumCnxManager@384] - Cannot open channel to 3 at election address zk-03/202.106.199.35:3888

很显然,zk-01在启动时连接集群中其他结点(zk-02、zk-03)时,主机名映射的IP与我们实际配置的不一致,所以集群中各个结点之间无法建立链路,整个ZooKeeper集群启动是失败的。
上面错误日志中zk-02/202.106.199.35:3888实际应该是zk-02/202.192.168.0.178:3888就对了,但是在进行域名解析的时候映射有问题,修改每个结点的/etc/hosts文件,将ZooKeeper集群中所有结点主机名到IP地址的映射配置上。

参考链接

下面是我整理搜集的有关ZooKeeper相关内容的网址,可以学习参考。
中文链接:

http://agapple.iteye.com/blog/1111377
http://agapple.iteye.com/blog/1112032
http://agapple.iteye.com/blog/1292129
http://agapple.iteye.com/blog/1184023
http://agapple.iteye.com/blog/1184040
http://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/
http://luzengyi.blog.163.com/blog/static/529188201064113744373/
http://luzengyi.blog.163.com/blog/static/529188201061155444869/
英文链接:

http://zookeeper.apache.org/doc/trunk/zookeeperStarted.html
http://zookeeper.apache.org/doc/r3.3.4/zookeeperOver.html
http://zookeeper.apache.org/doc/r3.3.4/recipes.html
http://zookeeper.apache.org/doc/trunk/
http://wiki.apache.org/hadoop/ZooKeeper/Tutorial
http://wiki.apache.org/hadoop/ZooKeeper/FAQ
http://wiki.apache.org/hadoop/ZooKeeper/Troubleshooting
分享到:
评论

相关推荐

    zookeeper-3.3.4.tar.zip

    三、Zookeeper-3.3.4的安装与配置 1. 解压文件:首先,你需要从提供的资源包中解压`zookeeper-3.3.4.tar.gz`,在命令行中执行`tar -zxvf zookeeper-3.3.4.tar.gz`完成解压。 2. 配置环境变量:在系统环境变量中...

    ZooKeeper集群安装配置

    ZooKeeper 集群安装配置 ZooKeeper 是一个分布式开源框架,提供了协调分布式应用的基本服务,它向外部应用暴露一组通用服务——分布式同步(Distributed Synchronization)、命名服务(Naming Service)、集群维护...

    zookeeper集群升级方案

    为了提升系统的稳定性和扩展性,并充分利用Zookeeper新版本所带来的性能优化和新特性支持,决定将Zookeeper集群从3.3.4版本升级至3.4.8版本。 #### 二、升级前准备 1. **需求分析**: - **目标**:确保升级过程...

    PyPI 官网下载 | fugue-icons-3.3.4.tar.gz

    “Zookeeper”是一个分布式协调服务,常用于管理分布式系统的配置信息、命名服务、集群同步等。“分布式”指的是由多台计算机组成的系统,它们协同工作以完成一个共同的任务,这在云环境中很常见。“云原生”(Cloud...

    zookeeper 例子 源码 jar

    ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它是集群的管理者,监视着集群中各个节点的状态根据节点提交的反馈进行下一步合理操作。最终将简单易用的接口和性能高效、功能稳定的系统提供给用户。...

    Jstorm集群安装文档

    本文档详细介绍了如何在多台虚拟机上搭建JStorm集群的过程,包括软件需求、集群规划、虚拟机的安装与配置、软件的安装与配置、以及最后的服务启动与验证。通过以上步骤,可以成功地部署一个功能完善的JStorm集群,...

    hadoop-3.1.4.tar.zip

    这个压缩文件"hadoop-3.1.4.tar.zip"包含了Hadoop的源代码、文档、配置文件以及相关的依赖库,使得用户可以方便地在本地环境中安装和运行Hadoop。 在解压文件"hadoop-3.1.4.tar.gz"后,我们可以得到Hadoop的完整...

    hive和hbase整合

    &lt;value&gt;file:///usr/lib/hive/lib/hive-hbase-handler-0.7.1-cdh3u3.jar,file:///usr/lib/hbase/hbase-0.90.4-cdh3u3.jar,file:///usr/lib/zookeeper/zookeeper-3.3.4-cdh3u3.jar,file:///usr/lib/hbase/lib/guava-...

    windows环境下hadoop依赖

    本篇文章将深入探讨Windows环境下安装和配置Hadoop 2.7.x版本时所涉及的关键知识点,以及如何解决可能遇到的各种版本问题。 首先,我们需要理解Hadoop的核心组件,包括HDFS(Hadoop Distributed File System)和...

    hadoop 所用的jar包

    - ZooKeeper:一个分布式的协调服务,用于命名、配置管理、组服务等,Hadoop有时会依赖它来实现集群间的同步和管理。 在给定的压缩包文件“wuchen001-6807725-hadoopjar包_1600660835”中,很可能包含了上述提到的...

Global site tag (gtag.js) - Google Analytics