ZooKeeper Distributed模式

ZooKeeper Distributed模式

ZooKeeper分布式模式安装(ZooKeeper集群)也比较容易,这里说明一下基本要点。 首先要明确的是,ZooKeeper集群是一个独立的分布式协调服务集群,“独立”的含义就是说,如果想使用ZooKeeper实现分布式应用的协调与管理,简化协调与管理,任何分布式应用都可以使用,这就要归功于Zookeeper的数据模型(Data Model)和层次命名空间(Hierarchical Namespace)结构,详细可以参考Zookeeper。在设计你的分布式应用协调服务时,首要的就是考虑如何组织层次命名空间。 下面说明分布式模式的安装配置,过程如下所示:

1.主机名称到IP地址映射配置 ZooKeeper集群中具有两个关键的角色:Leader和Follower。集群中所有的结点作为一个整体对分布式应用提供服务,集群中每个结点之间都互相连接,所以,在配置的ZooKeeper集群的时候,每一个结点的host到IP地址的映射都要配置上集群中其它结点的映射信息。 例如,我的ZooKeeper集群中每个结点的配置,以slave-01为例,/etc/hosts内容如下所示:

192.168.130.62 slave-01
192.168.130.63 slave-02
192.168.130.68 slave-03

ZooKeeper采用一种称为Leader election的选举算法。在整个集群运行过程中,只有一个Leader,其他的都是Follower,如果ZooKeeper集群在运行过程中Leader出了问题,系统会采用该算法重新选出一个Leader。因此,各个结点之间要能够保证互相连接,必须配置上述映射。 ZooKeeper集群启动的时候,会首先选出一个Leader,在Leader election过程中,某一个满足选举算的结点就能成为Leader。整个集群的架构可以参考sc_designGoals

2.修改ZooKeeper配置文件 在其中一台机器(slave-01)上,解压缩zookeeper-3.4.5.tar.gz,修改配置文件conf/zoo.cfg,内容如下所示:

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper/data
dataLogDir=/tmp/zookeeper/dataLog
clientPort=2181
server.1=slave-01:2888:3888
server.2=slave-02:2888:3888
server.3=slave-03:2888:3888

配置说明请点击

3.远程复制分发安装文件 上面已经在一台机器slave-01上配置完成ZooKeeper,现在可以将该配置好的安装文件远程拷贝到集群中的各个结点对应的目录下

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

hadoop@slave-01:~ echo "1" > /tmp/zookeeper/data/myid  
hadoop@slave-02:~ echo "2" > /tmp/zookeeper/data/myid  
hadoop@slave-03:~ echo "3" > /tmp/zookeeper/data/myid 

5.启动ZooKeeper集群 在ZooKeeper集群的每个结点上,执行启动ZooKeeper服务的脚本,如下所示:

hadoop@slave-01:~ bin/zkServer.sh start
hadoop@slave-02:~ bin/zkServer.sh start
hadoop@slave-03:~ bin/zkServer.sh start 

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

hadoop@slave-01:~ tail -f zookeeper.out 
2013-10-30 09:28:41,944 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxnFactory@197] - Accepted socket connection from /192.168.130.63:55204
2013-10-30 09:28:41,965 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:ZooKeeperServer@839] - Client attempting to establish new session at /192.168.130.63:55204
2013-10-30 09:28:41,968 [myid:] - INFO  [SyncThread:0:FileTxnLog@199] - Creating new log file: log.3
2013-10-30 09:28:42,073 [myid:] - INFO  [SyncThread:0:ZooKeeperServer@595] - Established session 0x14206f84fd10000 with negotiated timeout 30000 for client /192.168.130.63:55204
2013-10-30 09:39:00,842 [myid:] - WARN  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@349] - caught end of stream exception
EndOfStreamException: Unable to read additional data from client sessionid 0x14206f84fd10000, likely client has closed socket
        at org.apache.zookeeper.server.NIOServerCnxn.doIO(NIOServerCnxn.java:220)
        at org.apache.zookeeper.server.NIOServerCnxnFactory.run(NIOServerCnxnFactory.java:208)
        at java.lang.Thread.run(Thread.java:636)
2013-10-30 09:39:00,845 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@1001] - Closed socket connection for client /192.168.130.63:55204 which had sessionid 0x14206f84fd10000

我启动的顺序是slave-01>slave-02>slave-03,由于ZooKeeper集群启动的时候,每个结点都试图去连接集群中的其它结点,先启动的肯定连不上后面还没启动的,所以上面日志前面部分的异常是可以忽略的。通过后面部分可以看到,集群在选出一个Leader后,最后稳定了。 其他结点可能也出现类似问题,属于正常。

6.安装验证 可以通过ZooKeeper的脚本来查看启动状态,包括集群中各个结点的角色(或是Leader,或是Follower),如下所示,是在ZooKeeper集群中的每个结点上查询的结果:

hadoop@slave-01:~ sh bin/zkServer.sh status
JMX enabled by default
Using config: /root/zookeeper/bin/../conf/zoo.cfg
Mode: leader

hadoop@slave-03:~ sh bin/zkServer.sh status
JMX enabled by default
Using config: /root/zookeeper/bin/../conf/zoo.cfg
Mode: follower

hadoop@slave-03:~ sh bin/zkServer.sh status
JMX enabled by default
Using config: /root/zookeeper/bin/../conf/zoo.cfg
Mode: leader

通过上面状态查询结果可见,slave-02是集群的Leader,其余的两个结点是Follower。

另外,可以通过客户端脚本,连接到ZooKeeper集群上。对于客户端来说,ZooKeeper是一个整体(ensemble),连接到ZooKeeper集群实际上感觉在独享整个集群的服务,所以,你可以在任何一个结点上建立到服务集群的连接,例如:

[root@localhost zookeeper]# bin/zkCli.sh -server slave-01:2181
Connecting to slave-01:2181
2013-10-29 22:03:15,704 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT
2013-10-29 22:03:15,724 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=localhost
2013-10-29 22:03:15,784 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.5.0
2013-10-29 22:03:15,788 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Free Software Foundation, Inc.
2013-10-29 22:03:15,791 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre
2013-10-29 22:03:15,794 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/root/zookeeper/bin/../build/classes:/root/zookeeper/bin/../build/lib/*.jar:/root/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/root/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/root/zookeeper/bin/../lib/netty-3.2.2.Final.jar:/root/zookeeper/bin/../lib/log4j-1.2.15.jar:/root/zookeeper/bin/../lib/jline-0.9.94.jar:/root/zookeeper/bin/../zookeeper-3.4.5.jar:/root/zookeeper/bin/../src/java/lib/*.jar:/root/zookeeper/bin/../conf:
2013-10-29 22:03:15,797 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/lib64/gcj-4.4.7
2013-10-29 22:03:15,799 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2013-10-29 22:03:15,802 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2013-10-29 22:03:15,804 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
2013-10-29 22:03:15,807 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=x86_64
2013-10-29 22:03:15,809 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=2.6.32-71.el6.x86_64
2013-10-29 22:03:15,812 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root
2013-10-29 22:03:15,815 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root
2013-10-29 22:03:15,817 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/root/zookeeper
2013-10-29 22:03:15,823 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=slave-01:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@164718c6
Welcome to ZooKeeper!
2013-10-29 22:03:15,888 [myid:] - INFO  [main-SendThread(192.168.130.62:2181):ClientCnxn$SendThread@966] - Opening socket connection to server /192.168.130.62:2181. Will not attempt to authenticate using SASL (unknown error)
2013-10-29 22:03:15,891 [myid:] - INFO  [main-SendThread(192.168.130.62:2181):ClientCnxn$SendThread@849] - Socket connection established to /192.168.130.62:2181, initiating session
JLine support is enabled
2013-10-29 22:03:15,926 [myid:] - INFO  [main-SendThread(192.168.130.62:2181):ClientCnxn$SendThread@1207] - Session establishment complete on server /192.168.130.62:2181, sessionid = 0x142070ef9660000, negotiated timeout = 30000

WATCHER::

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

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

当前根路径为/zookeeper

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值