|
posted on 2012-05-03 10:28 云云 阅读(78847) 评论(11)
zookeeper是一个分布式开源框架,提供了协调分布式应用的基本服务,它向外部应用暴露一组通用服务——分布式同步(distributed synchronization)、命名服务(naming service)、集群维护(group maintenance)等,简化分布式应用协调及其管理的难度,提供高性能的分布式服务。zookeeper本身可以以standalone模式安装运行,不过它的长处在于通过分布式zookeeper集群(一个leader,多个follower),基于一定的策略来保证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是我的主机名,如果在本机执行,则执行如下命令即可:
客户端连接信息如下所示:
接着,可以使用help查看zookeeper客户端可以使用的基本操作命令。
zookeeper distributed模式
zookeeper分布式模式安装(zookeeper集群)也比较容易,这里说明一下基本要点。
首先要明确的是,zookeeper集群是一个独立的分布式协调服务集群,“独立”的含义就是说,如果想使用zookeeper实现分布式应用的协调与管理,简化协调与管理,任何分布式应用都可以使用,这就要归功于zookeeper的数据模型(data model)和层次命名空间(hierarchical namespace)结构,详细可以参考。在设计你的分布式应用协调服务时,首要的就是考虑如何组织层次命名空间。
下面说明分布式模式的安装配置,过程如下所示:
第一步:主机名称到ip地址映射配置
zookeeper集群中具有两个关键的角色:leader和follower。集群中所有的结点作为一个整体对分布式应用提供服务,集群中每个结点之间都互相连接,所以,在配置的zookeeper集群的时候,每一个结点的host到ip地址的映射都要配置上集群中其它结点的映射信息。
例如,我的zookeeper集群中每个结点的配置,以slave-01为例,/etc/hosts内容如下所示:
- 192.168.0.179 slave-01
- 192.168.0.178 slave-02
- 192.168.0.177 slave-03
zookeeper采用一种称为leader election的选举算法。在整个集群运行过程中,只有一个leader,其他的都是follower,如果zookeeper集群在运行过程中leader出了问题,系统会采用该算法重新选出一个leader。因此,各个结点之间要能够保证互相连接,必须配置上述映射。
zookeeper集群启动的时候,会首先选出一个leader,在leader election过程中,某一个满足选举算的结点就能成为leader。整个集群的架构可以参考。
第二步:修改zookeeper配置文件
在其中一台机器(slave-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=slave-01:2888:3888
- server.2=slave-02:2888:3888
- server.3=slave-03:2888:3888
上述配置内容说明,可以参考。
第三步:远程复制分发安装文件
上面已经在一台机器slave-01上配置完成zookeeper,现在可以将该配置好的安装文件远程拷贝到集群中的各个结点对应的目录下:
- cd /home/hadoop/installation/
- scp -r zookeeper-3.3.4/ hadoop@slave-02:/home/hadoop/installation/
- scp -r zookeeper-3.3.4/ hadoop@slave-03:/home/hadoop/installation/
第四步:设置myid
在我们配置的datadir指定的目录下面,创建一个myid文件,里面内容为一个数字,用来标识当前主机,conf/zoo.cfg文件中配置的server.x中x为什么数字,则myid文件中就输入这个数字,例如:
- hadoop@slave-01:~/installation/zookeeper-3.3.4$ echo "1" > /home/hadoop/storage/zookeeper/myid
- hadoop@slave-02:~/installation/zookeeper-3.3.4$ echo "2" > /home/hadoop/storage/zookeeper/myid
- hadoop@slave-03:~/installation/zookeeper-3.3.4$ echo "3" > /home/hadoop/storage/zookeeper/myid
按照上述进行配置即可。
第五步:启动zookeeper集群
在zookeeper集群的每个结点上,执行启动zookeeper服务的脚本,如下所示:
- hadoop@slave-01:~/installation/zookeeper-3.3.4$ bin/zkserver.sh start
- hadoop@slave-02:~/installation/zookeeper-3.3.4$ bin/zkserver.sh start
- hadoop@slave-03:~/installation/zookeeper-3.3.4$ bin/zkserver.sh start
以结点slave-01为例,日志如下所示:
- hadoop@slave-01:~/installation/zookeeper-3.3.4$ tail -500f zookeeper.out
- 2012-01-08 06:51:19,117 - info [main:quorumpeerconfig@90] - reading configuration from: /home/hadoop/installation/zookeeper-3.3.4/bin/../conf/zoo.cfg
- 2012-01-08 06:51:19,133 - info [main:quorumpeerconfig@310] - defaulting to majority quorums
- 2012-01-08 06:51:19,167 - info [main:quorumpeermain@119] - starting quorum peer
- 2012-01-08 06:51:19,227 - info [main:nioservercnxn$factory@143] - binding to port 0.0.0.0/0.0.0.0:2181
- 2012-01-08 06:51:19,277 - info [main:quorumpeer@819] - ticktime set to 2000
- 2012-01-08 06:51:19,278 - info [main:quorumpeer@830] - minsessiontimeout set to -1
- 2012-01-08 06:51:19,279 - info [main:quorumpeer@841] - maxsessiontimeout set to -1
- 2012-01-08 06:51:19,281 - info [main:quorumpeer@856] - initlimit set to 5
- 2012-01-08 06:51:19,347 - info [thread-1:quorumcnxmanager$listener@473] - my election bind port: 3888
- 2012-01-08 06:51:19,393 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:quorumpeer@621] - looking
- 2012-01-08 06:51:19,396 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:fastleaderelection@663] - new election. my id = 1, proposed zxid = 0
- 2012-01-08 06:51:19,400 - info [workerreceiver thread:fastleaderelection@496] - notification: 1 (n.leader), 0 (n.zxid), 1 (n.round), looking (n.state), 1 (n.sid), looking (my state)
- 2012-01-08 06:51:19,416 - warn [workersender thread:quorumcnxmanager@384] - cannot open channel to 2 at election address slave-02/192.168.0.178:3888
- java.net.connectexception: connection refused
- at sun.nio.ch.socketchannelimpl.checkconnect(native method)
- at sun.nio.ch.socketchannelimpl.finishconnect(socketchannelimpl.java:567)
- at sun.nio.ch.socketadaptor.connect(socketadaptor.java:100)
- at org.apache.zookeeper.server.quorum.quorumcnxmanager.connectone(quorumcnxmanager.java:371)
- at org.apache.zookeeper.server.quorum.quorumcnxmanager.tosend(quorumcnxmanager.java:340)
- at org.apache.zookeeper.server.quorum.fastleaderelection$messenger$workersender.process(fastleaderelection.java:360)
- at org.apache.zookeeper.server.quorum.fastleaderelection$messenger$workersender.run(fastleaderelection.java:333)
- at java.lang.thread.run(thread.java:662)
- 2012-01-08 06:51:19,420 - warn [workersender thread:quorumcnxmanager@384] - cannot open channel to 3 at election address slave-03/192.168.0.177:3888
- java.net.connectexception: connection refused
- at sun.nio.ch.socketchannelimpl.checkconnect(native method)
- at sun.nio.ch.socketchannelimpl.finishconnect(socketchannelimpl.java:567)
- at sun.nio.ch.socketadaptor.connect(socketadaptor.java:100)
- at org.apache.zookeeper.server.quorum.quorumcnxmanager.connectone(quorumcnxmanager.java:371)
- at org.apache.zookeeper.server.quorum.quorumcnxmanager.tosend(quorumcnxmanager.java:340)
- at org.apache.zookeeper.server.quorum.fastleaderelection$messenger$workersender.process(fastleaderelection.java:360)
- at org.apache.zookeeper.server.quorum.fastleaderelection$messenger$workersender.run(fastleaderelection.java:333)
- at java.lang.thread.run(thread.java:662)
- 2012-01-08 06:51:19,612 - warn [quorumpeer:/0:0:0:0:0:0:0:0:2181:quorumcnxmanager@384] - cannot open channel to 2 at election address slave-02/192.168.0.178:3888
- java.net.connectexception: connection refused
- at sun.nio.ch.socketchannelimpl.checkconnect(native method)
- at sun.nio.ch.socketchannelimpl.finishconnect(socketchannelimpl.java:567)
- at sun.nio.ch.socketadaptor.connect(socketadaptor.java:100)
- 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)
- 2012-01-08 06:51:19,615 - warn [quorumpeer:/0:0:0:0:0:0:0:0:2181:quorumcnxmanager@384] - cannot open channel to 3 at election address slave-03/192.168.0.177:3888
- java.net.connectexception: connection refused
- at sun.nio.ch.socketchannelimpl.checkconnect(native method)
- at sun.nio.ch.socketchannelimpl.finishconnect(socketchannelimpl.java:567)
- at sun.nio.ch.socketadaptor.connect(socketadaptor.java:100)
- 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)
- 2012-01-08 06:51:19,616 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:fastleaderelection@697] - notification time out: 400
- 2012-01-08 06:51:20,019 - warn [quorumpeer:/0:0:0:0:0:0:0:0:2181:quorumcnxmanager@384] - cannot open channel to 2 at election address slave-02/192.168.0.178:3888
- java.net.connectexception: connection refused
- at sun.nio.ch.socketchannelimpl.checkconnect(native method)
- at sun.nio.ch.socketchannelimpl.finishconnect(socketchannelimpl.java:567)
- at sun.nio.ch.socketadaptor.connect(socketadaptor.java:100)
- 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)
- 2012-01-08 06:51:20,021 - warn [quorumpeer:/0:0:0:0:0:0:0:0:2181:quorumcnxmanager@384] - cannot open channel to 3 at election address slave-03/192.168.0.177:3888
- java.net.connectexception: connection refused
- at sun.nio.ch.socketchannelimpl.checkconnect(native method)
- at sun.nio.ch.socketchannelimpl.finishconnect(socketchannelimpl.java:567)
- at sun.nio.ch.socketadaptor.connect(socketadaptor.java:100)
- 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)
- 2012-01-08 06:51:20,022 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:fastleaderelection@697] - notification time out: 800
- 2012-01-08 06:51:20,825 - warn [quorumpeer:/0:0:0:0:0:0:0:0:2181:quorumcnxmanager@384] - cannot open channel to 2 at election address slave-02/192.168.0.178:3888
- java.net.connectexception: connection refused
- at sun.nio.ch.socketchannelimpl.checkconnect(native method)
- at sun.nio.ch.socketchannelimpl.finishconnect(socketchannelimpl.java:567)
- at sun.nio.ch.socketadaptor.connect(socketadaptor.java:100)
- 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)
- 2012-01-08 06:51:20,827 - warn [quorumpeer:/0:0:0:0:0:0:0:0:2181:quorumcnxmanager@384] - cannot open channel to 3 at election address slave-03/192.168.0.177:3888
- java.net.connectexception: connection refused
- at sun.nio.ch.socketchannelimpl.checkconnect(native method)
- at sun.nio.ch.socketchannelimpl.finishconnect(socketchannelimpl.java:567)
- at sun.nio.ch.socketadaptor.connect(socketadaptor.java:100)
- 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)
- 2012-01-08 06:51:20,828 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:fastleaderelection@697] - notification time out: 1600
- 2012-01-08 06:51:22,435 - warn [quorumpeer:/0:0:0:0:0:0:0:0:2181:quorumcnxmanager@384] - cannot open channel to 2 at election address slave-02/192.168.0.178:3888
- java.net.connectexception: connection refused
- at sun.nio.ch.socketchannelimpl.checkconnect(native method)
- at sun.nio.ch.socketchannelimpl.finishconnect(socketchannelimpl.java:567)
- at sun.nio.ch.socketadaptor.connect(socketadaptor.java:100)
- 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)
- 2012-01-08 06:51:22,439 - warn [quorumpeer:/0:0:0:0:0:0:0:0:2181:quorumcnxmanager@384] - cannot open channel to 3 at election address slave-03/192.168.0.177:3888
- java.net.connectexception: connection refused
- at sun.nio.ch.socketchannelimpl.checkconnect(native method)
- at sun.nio.ch.socketchannelimpl.finishconnect(socketchannelimpl.java:567)
- at sun.nio.ch.socketadaptor.connect(socketadaptor.java:100)
- 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)
- 2012-01-08 06:51:22,441 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:fastleaderelection@697] - notification time out: 3200
- 2012-01-08 06:51:22,945 - info [workerreceiver thread:fastleaderelection@496] - notification: 2 (n.leader), 0 (n.zxid), 1 (n.round), looking (n.state), 2 (n.sid), looking (my state)
- 2012-01-08 06:51:22,946 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:fastleaderelection@721] - updating proposal
- 2012-01-08 06:51:22,949 - warn [workersender thread:quorumcnxmanager@384] - cannot open channel to 3 at election address slave-03/192.168.0.177:3888
- java.net.connectexception: connection refused
- at sun.nio.ch.socketchannelimpl.checkconnect(native method)
- at sun.nio.ch.socketchannelimpl.finishconnect(socketchannelimpl.java:567)
- at sun.nio.ch.socketadaptor.connect(socketadaptor.java:100)
- at org.apache.zookeeper.server.quorum.quorumcnxmanager.connectone(quorumcnxmanager.java:371)
- at org.apache.zookeeper.server.quorum.quorumcnxmanager.tosend(quorumcnxmanager.java:340)
- at org.apache.zookeeper.server.quorum.fastleaderelection$messenger$workersender.process(fastleaderelection.java:360)
- at org.apache.zookeeper.server.quorum.fastleaderelection$messenger$workersender.run(fastleaderelection.java:333)
- at java.lang.thread.run(thread.java:662)
- 2012-01-08 06:51:22,951 - info [workerreceiver thread:fastleaderelection@496] - notification: 2 (n.leader), 0 (n.zxid), 1 (n.round), looking (n.state), 1 (n.sid), looking (my state)
- 2012-01-08 06:51:23,156 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:quorumpeer@643] - following
- 2012-01-08 06:51:23,170 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:learner@80] - tcp nodelay set to: true
- 2012-01-08 06:51:23,206 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:environment@97] - server environment:zookeeper.version=3.3.3-1203054, built on 11/17/2011 05:47 gmt
- 2012-01-08 06:51:23,207 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:environment@97] - server environment:host.name=slave-01
- 2012-01-08 06:51:23,207 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:environment@97] - server environment:java.version=1.6.0_30
- 2012-01-08 06:51:23,208 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:environment@97] - server environment:java.vendor=sun microsystems inc.
- 2012-01-08 06:51:23,208 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:environment@97] - server environment:java.home=/home/hadoop/installation/jdk1.6.0_30/jre
- 2012-01-08 06:51:23,209 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:environment@97] - server 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
- 2012-01-08 06:51:23,210 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:environment@97] - server 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
- 2012-01-08 06:51:23,210 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:environment@97] - server environment:java.io.tmpdir=/tmp
- 2012-01-08 06:51:23,212 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:environment@97] - server environment:java.compiler=
- 2012-01-08 06:51:23,212 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:environment@97] - server environment:os.name=linux
- 2012-01-08 06:51:23,212 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:environment@97] - server environment:os.arch=i386
- 2012-01-08 06:51:23,213 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:environment@97] - server environment:os.version=3.0.0-14-generic
- 2012-01-08 06:51:23,213 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:environment@97] - server environment:user.name=hadoop
- 2012-01-08 06:51:23,214 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:environment@97] - server environment:user.home=/home/hadoop
- 2012-01-08 06:51:23,214 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:environment@97] - server environment:user.dir=/home/hadoop/installation/zookeeper-3.3.4
- 2012-01-08 06:51:23,223 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:zookeeperserver@151] - created server with ticktime 2000 minsessiontimeout 4000 maxsessiontimeout 40000 datadir /home/hadoop/storage/zookeeper/version-2 snapdir /home/hadoop/storage/zookeeper/version-2
- 2012-01-08 06:51:23,339 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:learner@294] - getting a snapshot from leader
- 2012-01-08 06:51:23,358 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:learner@325] - setting leader epoch 1
- 2012-01-08 06:51:23,358 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:filetxnsnaplog@254] - snapshotting: 0
- 2012-01-08 06:51:25,511 - info [workerreceiver thread:fastleaderelection@496] - notification: 3 (n.leader), 0 (n.zxid), 1 (n.round), looking (n.state), 3 (n.sid), following (my state)
- 2012-01-08 06:51:42,584 - info [workerreceiver thread:fastleaderelection@496] - notification: 3 (n.leader), 0 (n.zxid), 2 (n.round), looking (n.state), 3 (n.sid), following (my state)
我启动的顺序是slave-01>slave-02>slave-03,由于zookeeper集群启动的时候,每个结点都试图去连接集群中的其它结点,先启动的肯定连不上后面还没启动的,所以上面日志前面部分的异常是可以忽略的。通过后面部分可以看到,集群在选出一个leader后,最后稳定了。
其他结点可能也出现类似问题,属于正常。
第六步:安装验证
可以通过zookeeper的脚本来查看启动状态,包括集群中各个结点的角色(或是leader,或是follower),如下所示,是在zookeeper集群中的每个结点上查询的结果:
- hadoop@slave-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
-
- hadoop@slave-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
-
- hadoop@slave-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
通过上面状态查询结果可见,slave-02是集群的leader,其余的两个结点是follower。
另外,可以通过客户端脚本,连接到zookeeper集群上。对于客户端来说,zookeeper是一个整体(ensemble),连接到zookeeper集群实际上感觉在独享整个集群的服务,所以,你可以在任何一个结点上建立到服务集群的连接,例如:
- hadoop@slave-03:~/installation/zookeeper-3.3.4$ bin/zkcli.sh -server slave-01:2181
- connecting to slave-01:2181
- 2012-01-08 07:14:21,068 - info [main:environment@97] - client environment:zookeeper.version=3.3.3-1203054, built on 11/17/2011 05:47 gmt
- 2012-01-08 07:14:21,080 - info [main:environment@97] - client environment:host.name=slave-03
- 2012-01-08 07:14:21,085 - info [main:environment@97] - client environment:java.version=1.6.0_30
- 2012-01-08 07:14:21,089 - info [main:environment@97] - client environment:java.vendor=sun microsystems inc.
- 2012-01-08 07:14:21,095 - info [main:environment@97] - client environment:java.home=/home/hadoop/installation/jdk1.6.0_30/jre
- 2012-01-08 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
- 2012-01-08 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
- 2012-01-08 07:14:21,116 - info [main:environment@97] - client environment:java.io.tmpdir=/tmp
- 2012-01-08 07:14:21,124 - info [main:environment@97] - client environment:java.compiler=
- 2012-01-08 07:14:21,169 - info [main:environment@97] - client environment:os.name=linux
- 2012-01-08 07:14:21,175 - info [main:environment@97] - client environment:os.arch=i386
- 2012-01-08 07:14:21,177 - info [main:environment@97] - client environment:os.version=3.0.0-14-generic
- 2012-01-08 07:14:21,185 - info [main:environment@97] - client environment:user.name=hadoop
- 2012-01-08 07:14:21,188 - info [main:environment@97] - client environment:user.home=/home/hadoop
- 2012-01-08 07:14:21,190 - info [main:environment@97] - client environment:user.dir=/home/hadoop/installation/zookeeper-3.3.4
- 2012-01-08 07:14:21,197 - info [main:zookeeper@379] - initiating client connection, connectstring=slave-01:2181 sessiontimeout=30000 watcher=org.apache.zookeeper.zookeepermain$mywatcher@bf32c
- 2012-01-08 07:14:21,305 - info [main-sendthread():clientcnxn$sendthread@1061] - opening socket connection to server slave-01/192.168.0.179:2181
- welcome to zookeeper!
- 2012-01-08 07:14:21,376 - info [main-sendthread(slave-01:2181):clientcnxn$sendthread@950] - socket connection established to slave-01/192.168.0.179:2181, initiating session
- jline support is enabled
- [zk: slave-01:2181(connecting) 0] 2012-01-08 07:14:21,872 - info [main-sendthread(slave-01:2181):clientcnxn$sendthread@739] - session establishment complete on server slave-01/192.168.0.179:2181, sessionid = 0x134bdcd6b730000, negotiated timeout = 30000
-
- watcher::
-
- watchedevent state:syncconnected type:none path:null
-
- [zk: slave-01:2181(connected) 0] ls /
- [zookeeper]
当前根路径为/zookeeper。
总结说明
主机名与ip地址映射配置问题
启动zookeeper集群时,如果zookeeper集群中slave-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)
- 2012-01-08 06:37:46,026 - info [quorumpeer:/0:0:0:0:0:0:0:0:2181:fastleaderelection@697] - notification time out: 6400
- 2012-01-08 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 slave-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)
- 2012-01-08 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 slave-03/202.106.199.35:3888
很显然,slave-01在启动时连接集群中其他结点(slave-02、slave-03)时,主机名映射的ip与我们实际配置的不一致,所以集群中各个结点之间无法建立链路,整个zookeeper集群启动是失败的。
上面错误日志中slave-02/202.106.199.35:3888实际应该是slave-02/202.192.168.0.178:3888就对了,但是在进行域名解析的时候映射有问题,修改每个结点的/etc/hosts文件,将zookeeper集群中所有结点主机名到ip地址的映射配置上。
评论
# re: zookeeper 集群安装(单点与分布式成功安装)摘录[未登录] 回复
2012-11-28 15:54 by
写的很好,顶博主
# re: zookeeper 集群安装(单点与分布式成功安装)摘录 回复
2012-12-07 11:17 by
太感谢了!!赞!
# re: zookeeper 集群安装(单点与分布式成功安装)摘录 回复
2013-04-19 17:36 by
写的很好,赞!!!!!!!
# re: zookeeper 集群安装(单点与分布式成功安装)摘录 回复
2013-07-11 15:40 by
写的不错,赞!!
请问楼主在配置zookeeper集群时,leader和follower是怎么决定的? 不是按服务器启动顺序,是随机分配吗?
# re: zookeeper 集群安装(单点与分布式成功安装)摘录 回复
2014-11-27 01:29 by
我的ipy=映射是对的但还是出现这种异常 [myid=1]/0:0:0:0:0:0:0:0:2181:learner@233] - unexpected exception, tries=0, connecting to slave4/192.168.20.13:2888 java.net.noroutetohostexception: 没有到主机的路由 at java.net.plainsocketimpl.socketconnect(native method) at java.net.abstractplainsocketimpl.doconnect(abstractplainsocketimpl.java:345) at java.net.abstractplainsocketimpl.connecttoaddress(abstractplainsocketimpl.java:206) at java.net.abstractplainsocketimpl.connect(abstractplainsocketimpl.java:188) at java.net.sockssocketimpl.connect(sockssocketimpl.java:392) at java.net.socket.connect(socket.java:589) at org.apache.zookeeper.server.quorum.learner.connecttoleader(learner.java:225) at org.apache.zookeeper.server.quorum.follower.followleader(follower.java:71) at org.apache.zookeeper.server.quorum.quorumpeer.run(quorumpeer.java:786) 2014-11-27 01:19:30,946 [myid:1] - warn [quorumpeer[myid=1]/0:0:0:0:0:0:0:0:2181:learner@233] - unexpected exception, tries=1, connecting to slave4/192.168.20.13:2888 java.net.noroutetohostexception: 没有到主机的路由 at java.net.plainsocketimpl.socketconnect(native method) at java.net.abstractplainsocketimpl.doconnect(abstractplainsocketimpl.java:345) at java.net.abstractplainsocketimpl.connecttoaddress(abstractplainsocketimpl.java:206)
# re: zookeeper 集群安装(单点与分布式成功安装)摘录 回复
2015-10-20 20:10 by
@龙斌 这个问题,我也遇到了,你最后怎么解决的?
# re: zookeeper 集群安装(单点与分布式成功安装)摘录[未登录] 回复
2015-12-07 12:28 by
我解决了 就是关闭防火墙就可以了。
# re: zookeeper 集群安装(单点与分布式成功安装)摘录 回复
2015-12-17 14:29 by
zookeeper spring跨机房容灾系统以及灰度发布课程
课程学习地址:
一、课程使用到的软件及版本: centos系统,zookeeper 3.4.6,spring4
二、课程目标: 1、熟练开发各种分布式应用 2、掌控zookeeper java客户端开发 3、熟练运用curator的各种特性
三、课程目录
第1节、zookeepeer集群简介,跨机房容灾系统架构设计 第2节、zookeeper的java客户端与spring整合(上) 第3节、zookeeper的java客户端与spring整合(下) 第4节、zookeeper-3watcher使用(上) 第5节、zookeeper-3watcher使用(下) 第6节、curator介绍与使用 第7节、zookeeper跨机房容灾系统设计两种方案详细讲解 第8节、zookeeper跨机房容灾具体实现 第9节、zookeeper跨机房容灾系统的调试,以及分布式锁的简单介绍 第10节、zookeeper分布式锁(上) 第11节、zookeeper分布式锁(下) 第12节、zookeeper实现灰度指定服务器发布 第13节、课程总结以及未来展望
|