zookeeper的安裝、使用和結合sheepdog部署、使用

ZooKeeper的安裝模式分爲三種,分別爲:單機模式(stand-alone)、集羣模式和集羣僞分佈模式。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,配置文件內容如下所示:

[plain] view plaincopy
  1. tickTime=2000  
  2. dataDir=/home/hadoop/storage/zookeeper  
  3. clientPort=2181  
  4. initLimit=5  
  5. syncLimit=2  
上面各個配置參數的含義也非常簡單,引用如下所示:

[plain] view plaincopy
  1. 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.  
  2. dataDir —— the location to store the in-memory database snapshots and, unless specified otherwise, the transaction log of updates to the database.  
  3. clientPort —— the port to listen for client connections  
下面啓動ZooKeeper服務器進程:

[plain] view plaincopy
  1. cd zookeeper-3.3.4/  
  2. bin/zkServer.sh start  
通過jps命令可以查看ZooKeeper服務器進程,名稱爲QuorumPeerMain。

在客戶端連接ZooKeeper服務器,執行如下命令:

[plain] view plaincopy
  1. bin/zkCli.sh -server dynamic:2181  
上面dynamic是我的主機名,如果在本機執行,則執行如下命令即可:
[plain] view plaincopy
  1. bin/zkCli.sh  
客戶端連接信息如下所示:

[plain] view plaincopy
  1. hadoop@master:~/installation/zookeeper-3.3.4$ bin/zkCli.sh -server dynamic:2181  
  2. Connecting to dynamic:2181  
  3. 2012-01-08 21:30:06,178 - INFO  [main:Environment@97] - Client environment:zookeeper.version=3.3.3-1203054, built on 11/17/2011 05:47 GMT  
  4. 2012-01-08 21:30:06,188 - INFO  [main:Environment@97] - Client environment:host.name=master  
  5. 2012-01-08 21:30:06,191 - INFO  [main:Environment@97] - Client environment:java.version=1.6.0_30  
  6. 2012-01-08 21:30:06,194 - INFO  [main:Environment@97] - Client environment:java.vendor=Sun Microsystems Inc.  
  7. 2012-01-08 21:30:06,200 - INFO  [main:Environment@97] - Client environment:java.home=/home/hadoop/installation/jdk1.6.0_30/jre  
  8. 2012-01-08 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  
  9. 2012-01-08 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  
  10. 2012-01-08 21:30:06,213 - INFO  [main:Environment@97] - Client environment:java.io.tmpdir=/tmp  
  11. 2012-01-08 21:30:06,216 - INFO  [main:Environment@97] - Client environment:java.compiler=<NA>  
  12. 2012-01-08 21:30:06,235 - INFO  [main:Environment@97] - Client environment:os.name=Linux  
  13. 2012-01-08 21:30:06,244 - INFO  [main:Environment@97] - Client environment:os.arch=i386  
  14. 2012-01-08 21:30:06,246 - INFO  [main:Environment@97] - Client environment:os.version=3.0.0-14-generic  
  15. 2012-01-08 21:30:06,251 - INFO  [main:Environment@97] - Client environment:user.name=hadoop  
  16. 2012-01-08 21:30:06,254 - INFO  [main:Environment@97] - Client environment:user.home=/home/hadoop  
  17. 2012-01-08 21:30:06,255 - INFO  [main:Environment@97] - Client environment:user.dir=/home/hadoop/installation/zookeeper-3.3.4  
  18. 2012-01-08 21:30:06,264 - INFO  [main:ZooKeeper@379] - Initiating client connection, connectString=dynamic:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@bf32c  
  19. 2012-01-08 21:30:06,339 - INFO  [main-SendThread():ClientCnxn$SendThread@1061] - Opening socket connection to server dynamic/192.168.0.107:2181  
  20. Welcome to ZooKeeper!  
  21. 2012-01-08 21:30:06,397 - INFO  [main-SendThread(dynamic:2181):ClientCnxn$SendThread@950] - Socket connection established to dynamic/192.168.0.107:2181, initiating session  
  22. JLine support is enabled  
  23. 2012-01-08 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  
  24.   
  25. WATCHER::  
  26.   
  27. WatchedEvent state:SyncConnected type:None path:null  
  28. [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集羣中每個結點的配置,以slave-01爲例,/etc/hosts內容如下所示:

[plain] view plaincopy
  1. 192.168.0.179   slave-01  
  2. 192.168.0.178   slave-02  
  3. 192.168.0.177   slave-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配置文件

在其中一臺機器(slave-01)上,解壓縮zookeeper-3.3.4.tar.gz,修改配置文件conf/zoo.cfg,內容如下所示:

[plain] view plaincopy
  1. tickTime=2000  
  2. dataDir=/home/hadoop/storage/zookeeper  
  3. clientPort=2181  
  4. initLimit=5  
  5. syncLimit=2  
  6. server.1=slave-01:2888:3888  
  7. server.2=slave-02:2888:3888  
  8. server.3=slave-03:2888:3888  
上述配置內容說明,可以參考http://zookeeper.apache.org/doc/trunk/zookeeperStarted.html#sc_RunningReplicatedZooKeeper

第三步:遠程複製分發安裝文件

上面已經在一臺機器slave-01上配置完成ZooKeeper,現在可以將該配置好的安裝文件遠程拷貝到集羣中的各個結點對應的目錄下:

[plain] view plaincopy
  1. cd /home/hadoop/installation/  
  2. scp -r zookeeper-3.3.4/ hadoop@slave-02:/home/hadoop/installation/  
  3. scp -r zookeeper-3.3.4/ hadoop@slave-03:/home/hadoop/installation/  
第四步:設置myid

在我們配置的dataDir指定的目錄下面,創建一個myid文件,裏面內容爲一個數字,用來標識當前主機,conf/zoo.cfg文件中配置的server.X中X爲什麼數字,則myid文件中就輸入這個數字,例如:

[plain] view plaincopy
  1. hadoop@slave-01:~/installation/zookeeper-3.3.4$ echo "1" > /home/hadoop/storage/zookeeper/myid  
  2. hadoop@slave-02:~/installation/zookeeper-3.3.4$ echo "2" > /home/hadoop/storage/zookeeper/myid  
  3. hadoop@slave-03:~/installation/zookeeper-3.3.4$ echo "3" > /home/hadoop/storage/zookeeper/myid  
按照上述進行配置即可。

第五步:啓動ZooKeeper集羣

在ZooKeeper集羣的每個結點上,執行啓動ZooKeeper服務的腳本,如下所示:

[plain] view plaincopy
  1. hadoop@slave-01:~/installation/zookeeper-3.3.4$ bin/zkServer.sh start  
  2. hadoop@slave-02:~/installation/zookeeper-3.3.4$ bin/zkServer.sh start  
  3. hadoop@slave-03:~/installation/zookeeper-3.3.4$ bin/zkServer.sh start  
以結點slave-01爲例,日誌如下所示:
[plain] view plaincopy
  1. hadoop@slave-01:~/installation/zookeeper-3.3.4$ tail -500f zookeeper.out   
  2. 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  
  3. 2012-01-08 06:51:19,133 - INFO  [main:QuorumPeerConfig@310] - Defaulting to majority quorums  
  4. 2012-01-08 06:51:19,167 - INFO  [main:QuorumPeerMain@119] - Starting quorum peer  
  5. 2012-01-08 06:51:19,227 - INFO  [main:NIOServerCnxn$Factory@143] - binding to port 0.0.0.0/0.0.0.0:2181  
  6. 2012-01-08 06:51:19,277 - INFO  [main:QuorumPeer@819] - tickTime set to 2000  
  7. 2012-01-08 06:51:19,278 - INFO  [main:QuorumPeer@830] - minSessionTimeout set to -1  
  8. 2012-01-08 06:51:19,279 - INFO  [main:QuorumPeer@841] - maxSessionTimeout set to -1  
  9. 2012-01-08 06:51:19,281 - INFO  [main:QuorumPeer@856] - initLimit set to 5  
  10. 2012-01-08 06:51:19,347 - INFO  [Thread-1:QuorumCnxManager$Listener@473] - My election bind port: 3888  
  11. 2012-01-08 06:51:19,393 - INFO  [QuorumPeer:/0:0:0:0:0:0:0:0:2181:QuorumPeer@621] - LOOKING  
  12. 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  
  13. 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)  
  14. 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  
  15. java.net.ConnectException: Connection refused  
  16.         at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)  
  17.         at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:567)  
  18.         at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:100)  
  19.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:371)  
  20.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.toSend(QuorumCnxManager.java:340)  
  21.         at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.process(FastLeaderElection.java:360)  
  22.         at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.run(FastLeaderElection.java:333)  
  23.         at java.lang.Thread.run(Thread.java:662)  
  24. 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  
  25. java.net.ConnectException: Connection refused  
  26.         at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)  
  27.         at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:567)  
  28.         at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:100)  
  29.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:371)  
  30.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.toSend(QuorumCnxManager.java:340)  
  31.         at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.process(FastLeaderElection.java:360)  
  32.         at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.run(FastLeaderElection.java:333)  
  33.         at java.lang.Thread.run(Thread.java:662)  
  34. 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  
  35. java.net.ConnectException: Connection refused  
  36.         at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)  
  37.         at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:567)  
  38.         at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:100)  
  39.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:371)  
  40.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(QuorumCnxManager.java:404)  
  41.         at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader(FastLeaderElection.java:688)  
  42.         at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:622)  
  43. 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  
  44. java.net.ConnectException: Connection refused  
  45.         at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)  
  46.         at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:567)  
  47.         at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:100)  
  48.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:371)  
  49.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(QuorumCnxManager.java:404)  
  50.         at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader(FastLeaderElection.java:688)  
  51.         at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:622)  
  52. 2012-01-08 06:51:19,616 - INFO  [QuorumPeer:/0:0:0:0:0:0:0:0:2181:FastLeaderElection@697] - Notification time out: 400  
  53. 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  
  54. java.net.ConnectException: Connection refused  
  55.         at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)  
  56.         at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:567)  
  57.         at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:100)  
  58.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:371)  
  59.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(QuorumCnxManager.java:404)  
  60.         at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader(FastLeaderElection.java:688)  
  61.         at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:622)  
  62. 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  
  63. java.net.ConnectException: Connection refused  
  64.         at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)  
  65.         at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:567)  
  66.         at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:100)  
  67.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:371)  
  68.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(QuorumCnxManager.java:404)  
  69.         at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader(FastLeaderElection.java:688)  
  70.         at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:622)  
  71. 2012-01-08 06:51:20,022 - INFO  [QuorumPeer:/0:0:0:0:0:0:0:0:2181:FastLeaderElection@697] - Notification time out: 800  
  72. 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  
  73. java.net.ConnectException: Connection refused  
  74.         at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)  
  75.         at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:567)  
  76.         at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:100)  
  77.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:371)  
  78.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(QuorumCnxManager.java:404)  
  79.         at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader(FastLeaderElection.java:688)  
  80.         at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:622)  
  81. 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  
  82. java.net.ConnectException: Connection refused  
  83.         at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)  
  84.         at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:567)  
  85.         at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:100)  
  86.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:371)  
  87.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(QuorumCnxManager.java:404)  
  88.         at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader(FastLeaderElection.java:688)  
  89.         at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:622)  
  90. 2012-01-08 06:51:20,828 - INFO  [QuorumPeer:/0:0:0:0:0:0:0:0:2181:FastLeaderElection@697] - Notification time out: 1600  
  91. 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  
  92. java.net.ConnectException: Connection refused  
  93.         at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)  
  94.         at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:567)  
  95.         at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:100)  
  96.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:371)  
  97.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(QuorumCnxManager.java:404)  
  98.         at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader(FastLeaderElection.java:688)  
  99.         at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:622)  
  100. 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  
  101. java.net.ConnectException: Connection refused  
  102.         at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)  
  103.         at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:567)  
  104.         at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:100)  
  105.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:371)  
  106.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(QuorumCnxManager.java:404)  
  107.         at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader(FastLeaderElection.java:688)  
  108.         at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:622)  
  109. 2012-01-08 06:51:22,441 - INFO  [QuorumPeer:/0:0:0:0:0:0:0:0:2181:FastLeaderElection@697] - Notification time out: 3200  
  110. 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)  
  111. 2012-01-08 06:51:22,946 - INFO  [QuorumPeer:/0:0:0:0:0:0:0:0:2181:FastLeaderElection@721] - Updating proposal  
  112. 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  
  113. java.net.ConnectException: Connection refused  
  114.         at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)  
  115.         at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:567)  
  116.         at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:100)  
  117.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:371)  
  118.         at org.apache.zookeeper.server.quorum.QuorumCnxManager.toSend(QuorumCnxManager.java:340)  
  119.         at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.process(FastLeaderElection.java:360)  
  120.         at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.run(FastLeaderElection.java:333)  
  121.         at java.lang.Thread.run(Thread.java:662)  
  122. 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)  
  123. 2012-01-08 06:51:23,156 - INFO  [QuorumPeer:/0:0:0:0:0:0:0:0:2181:QuorumPeer@643] - FOLLOWING  
  124. 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  
  125. 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  
  126. 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  
  127. 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  
  128. 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.  
  129. 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  
  130. 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  
  131. 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  
  132. 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  
  133. 2012-01-08 06:51:23,212 - INFO  [QuorumPeer:/0:0:0:0:0:0:0:0:2181:Environment@97] - Server environment:java.compiler=<NA>  
  134. 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  
  135. 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  
  136. 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  
  137. 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  
  138. 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  
  139. 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  
  140. 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  
  141. 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  
  142. 2012-01-08 06:51:23,358 - INFO  [QuorumPeer:/0:0:0:0:0:0:0:0:2181:Learner@325] - Setting leader epoch 1  
  143. 2012-01-08 06:51:23,358 - INFO  [QuorumPeer:/0:0:0:0:0:0:0:0:2181:FileTxnSnapLog@254] - Snapshotting: 0  
  144. 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)  
  145. 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集羣中的每個結點上查詢的結果:

[plain] view plaincopy
  1. hadoop@slave-01:~/installation/zookeeper-3.3.4$ bin/zkServer.sh status  
  2. JMX enabled by default  
  3. Using config: /home/hadoop/installation/zookeeper-3.3.4/bin/../conf/zoo.cfg  
  4. Mode: follower  
  5.   
  6. hadoop@slave-02:~/installation/zookeeper-3.3.4$  bin/zkServer.sh status  
  7. JMX enabled by default  
  8. Using config: /home/hadoop/installation/zookeeper-3.3.4/bin/../conf/zoo.cfg  
  9. Mode: leader  
  10.   
  11. hadoop@slave-03:~/installation/zookeeper-3.3.4$  bin/zkServer.sh status  
  12. JMX enabled by default  
  13. Using config: /home/hadoop/installation/zookeeper-3.3.4/bin/../conf/zoo.cfg  
  14. Mode: follower  
通過上面狀態查詢結果可見,slave-02是集羣的Leader,其餘的兩個結點是Follower。

另外,可以通過客戶端腳本,連接到ZooKeeper集羣上。對於客戶端來說,ZooKeeper是一個整體(ensemble),連接到ZooKeeper集羣實際上感覺在獨享整個集羣的服務,所以,你可以在任何一個結點上建立到服務集羣的連接,例如:

[plain] view plaincopy
  1. hadoop@slave-03:~/installation/zookeeper-3.3.4$ bin/zkCli.sh -server slave-01:2181  
  2. Connecting to slave-01:2181  
  3. 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  
  4. 2012-01-08 07:14:21,080 - INFO  [main:Environment@97] - Client environment:host.name=slave-03  
  5. 2012-01-08 07:14:21,085 - INFO  [main:Environment@97] - Client environment:java.version=1.6.0_30  
  6. 2012-01-08 07:14:21,089 - INFO  [main:Environment@97] - Client environment:java.vendor=Sun Microsystems Inc.  
  7. 2012-01-08 07:14:21,095 - INFO  [main:Environment@97] - Client environment:java.home=/home/hadoop/installation/jdk1.6.0_30/jre  
  8. 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  
  9. 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  
  10. 2012-01-08 07:14:21,116 - INFO  [main:Environment@97] - Client environment:java.io.tmpdir=/tmp  
  11. 2012-01-08 07:14:21,124 - INFO  [main:Environment@97] - Client environment:java.compiler=<NA>  
  12. 2012-01-08 07:14:21,169 - INFO  [main:Environment@97] - Client environment:os.name=Linux  
  13. 2012-01-08 07:14:21,175 - INFO  [main:Environment@97] - Client environment:os.arch=i386  
  14. 2012-01-08 07:14:21,177 - INFO  [main:Environment@97] - Client environment:os.version=3.0.0-14-generic  
  15. 2012-01-08 07:14:21,185 - INFO  [main:Environment@97] - Client environment:user.name=hadoop  
  16. 2012-01-08 07:14:21,188 - INFO  [main:Environment@97] - Client environment:user.home=/home/hadoop  
  17. 2012-01-08 07:14:21,190 - INFO  [main:Environment@97] - Client environment:user.dir=/home/hadoop/installation/zookeeper-3.3.4  
  18. 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  
  19. 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  
  20. Welcome to ZooKeeper!  
  21. 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  
  22. JLine support is enabled  
  23. [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  
  24.   
  25. WATCHER::  
  26.   
  27. WatchedEvent state:SyncConnected type:None path:null  
  28.   
  29. [zk: slave-01:2181(CONNECTED) 0] ls /  
  30. [zookeeper]  
當前根路徑爲/zookeeper。

集羣僞分佈模式

簡單來說,集羣僞分佈模式就是在單機下模擬集羣的ZooKeeper服務。

那麼,如何對配置 ZooKeeper 的集羣僞分佈模式呢?其實很簡單,在 zookeeper 配置文檔中, clientPort參數用來設置客戶端連接 zookeeper 的端口。 server.1=IP1:2887:3887 中, IP1 指示的是組成 ZooKeeper 服務的機器 IP 地址, 2887 爲用來進行 leader 選舉的端口, 3887 爲組成 ZooKeeper 服務的機器之間通信的端口。集羣僞分佈模式我們使用每個配置文檔模擬一臺機器,也就是說,需要在單臺機器上運行多個 zookeeper 實例。但是,我們必須要保證各個配置文檔的 clientPort 不能衝突。

下面是我們所配置的集羣僞分佈模式,通過 zoo1.cfg  zoo2.cfg  zoo3.cfg 模擬了三臺機器的 ZooKeeper集羣。詳見代碼清單 C 

代碼清單C  zoo1.cfg 

# The number of milliseconds of each tick

tickTime=2000 

# The number of ticks that the initial

# synchronization phase can take

initLimit=10

# The number of ticks that can pass between

# sending a request and getting an acknowledgement

syncLimit=5

# the directory where the snapshot is stored.

dataDir=/root/hadoop-0.20.2/zookeeper-3.3.1/d_1

# the port at which the clients will connect

clientPort=2181

server.1=localhost:2887:3887

server.2=localhost:2888:3888

server.3=localhost:2889:3889

zoo2.cfg 

# The number of milliseconds of each tick

tickTime=2000

# The number of ticks that the initial

# synchronization phase can take

initLimit=10

# The number of ticks that can pass between

# sending a request and getting an acknowledgement

syncLimit=5

# the directory where the snapshot is stored.

dataDir=/root/hadoop-0.20.2/zookeeper-3.3.1/d_2 

# the port at which the clients will connect

clientPort=2182 

#the location of the log file

dataLogDir=/root/hadoop-0.20.2/zookeeper-3.3.1/logs 

server.1=localhost:2887:3887 

server.2=localhost:2888:3888

server.3=localhost:2889:3889

 

zoo3.cfg 

# The number of milliseconds of each tick

tickTime=2000 

# The number of ticks that the initial

# synchronization phase can take

initLimit=10 

# The number of ticks that can pass between

# sending a request and getting an acknowledgement

syncLimit=5

# the directory where the snapshot is stored.

dataDir=/root/hadoop-0.20.2/zookeeper-3.3.1/d_2

# the port at which the clients will connect

clientPort=2183

#the location of the log file

dataLogDir=/root/hadoop-0.20.2/zookeeper-3.3.1/logs

server.1=localhost:2887:3887 

server.2=localhost:2888:3888

server.3=localhost:2889:3889

從上述三個代碼清單中可以看到,除了 clientPort 不同之外, dataDir 也不同。另外,不要忘記在 dataDir 所對應的目錄中創建 myid 文件來指定對應的 zookeeper 服務器實例。 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

下面來說明zookeeper結合sheepdog的安裝和使用:

首先下載源碼包:

wget http://mirror.bjtu.edu.cn/apache/zookeeper/zookeeper-3.4.5/zookeeper-3.4.5.tar.gz						

然後執行:
$ tar -zxvf zookeeper-x.x.x.tar.gz
$ cd zookeeper-x.x.x/src/c
$ ./configure --prefix=/usr
$ make
$ make install
然後修改配置文件,如下所示:
$ cd zookeeper-x.x.x/conf
$ mv zoo_sample.cfg zoo.cfg
#Setting this to 0 entirely removes the limit on concurrent connections.
$ echo "maxClientCnxns=0" >> zoo.cfg
$ mkdir -p /tmp/zookeeper
然後啓動:
$ sudo ./bin/zkServer.sh start 
安裝sheepdog:
$ git clone git://github.com/collie/sheepdog.git
$ cd sheepdog/
$ ./configure --enable-zookeeper
$ make
$ sudo make install
使用zookeeper作爲集羣控制器啓動sheepdog:
$ sudo sheep /store  -c zookeeper:IP1:2181,IP2:2181,IP3:2181 # use default 30s heartbeat or
$ sudo sheep /store  -c zookeeper:IP1:2181,IP2:2181,IP3:2181,timeout=10s # use 10s heartbeat for small sized cluster like 30 nodes.
做完這些事情後,就可以體驗一下,集羣的感覺了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章