1. 程式人生 > >zookeeper執行環境2、3:單節點安裝和偽分散式叢集安裝

zookeeper執行環境2、3:單節點安裝和偽分散式叢集安裝

轉載:http://www.aboutyun.com/thread-9097-1-1.html

問題導讀:

1.什麼是zookeeper
2.zookeeper有幾種安裝方式?
3.zookeeper偽分佈如何配置myid?
4.zookeeper包含哪些常用操作命令?




前言
ZooKeeper是Hadoop家族的一款高效能的分散式協作的產品。在單機中,系統協作大都是程序級的操作。分散式系統中,服務協作都是跨伺服器才能完成的。在ZooKeeper之前,我們對於協作服務大都使用訊息中介軟體,隨著分散式系統的普及,用訊息中介軟體完成協作,會有大量的程式開發。ZooKeeper直接面向於分散式系統,可以減少我們自己的開發,幫助我們更好完成分散式系統的資料管理問題。 目錄
  • zookeeper介紹
  • zookeeper單節點安裝
  • zookeeper偽分散式叢集安裝
  • zookeeper命令列操作
  • Java程式設計現實命令列操作
1. zookeeper介紹 ZooKeeper是一個為分散式應用所設計的分佈的、開源的協調服務,它主要是用來解決分散式應用中經常遇到的一些資料管理問題,簡化分散式應用協調及其管理的難度,提供高效能的分散式服務。ZooKeeper本身可以以Standalone模式安裝執行,不過它的長處在於通過分散式ZooKeeper叢集(一個Leader,多個Follower),基於一定的策略來保證ZooKeeper叢集的穩定性和可用性,從而實現分散式應用的可靠性。 ZooKeeper是作為分散式協調服務,是不需要依賴於Hadoop的環境,也可以為其他的分散式環境提供服務。 2. zookeeper單節點安裝Standalones模式
系統環境: Linux Ubuntu 12.04.2 LTS 64bit server Java: 1.6.0_29 64-Bit Server VM
  1. ~ uname -a
  2. Linux conan 3.5.0-23-generic #35~precise1-Ubuntu SMP Fri Jan 25 17:13:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
  3. ~ cat /etc/issue
  4. Ubuntu 12.04.2 LTS \n \l
  5. ~ java -version
  6. java version "1.6.0_29"
  7. Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
  8. Java HotSpot(TM) 64-Bit Server VM (build 20.4-b02, mixed mode)
複製程式碼
下載zookeeper

  1. ~ mkdir /home/conan/toolkit
  2. ~ cd /home/conan/toolkit
  3. ~ wget http://apache.dataguru.cn/zookeeper/stable/zookeeper-3.4.5.tar.gz
  4. ~ tar xvf zookeeper-3.4.5.tar.gz
  5. ~ mv zookeeper-3.4.5 zookeeper345
  6. ~ cd zookeeper345
  7. ~ ls -l
  8. drwxr-xr-x  2 conan conan    4096 Aug 12 04:34 bin
  9. -rw-r--r--  1 conan conan   75988 Aug 12 04:34 build.xml
  10. -rw-r--r--  1 conan conan   70223 Aug 12 04:34 CHANGES.txt
  11. drwxr-xr-x  2 conan conan    4096 Aug 12 04:34 conf
  12. drwxr-xr-x 10 conan conan    4096 Aug 12 04:34 contrib
  13. drwxr-xr-x  2 conan conan    4096 Aug 12 04:34 dist-maven
  14. drwxr-xr-x  6 conan conan    4096 Aug 12 04:34 docs
  15. -rw-r--r--  1 conan conan    1953 Aug 12 04:34 ivysettings.xml
  16. -rw-r--r--  1 conan conan    3120 Aug 12 04:34 ivy.xml
  17. drwxr-xr-x  4 conan conan    4096 Aug 12 04:34 lib
  18. -rw-r--r--  1 conan conan   11358 Aug 12 04:34 LICENSE.txt
  19. -rw-r--r--  1 conan conan     170 Aug 12 04:34 NOTICE.txt
  20. -rw-r--r--  1 conan conan    1770 Aug 12 04:34 README_packaging.txt
  21. -rw-r--r--  1 conan conan    1585 Aug 12 04:34 README.txt
  22. drwxr-xr-x  5 conan conan    4096 Aug 12 04:34 recipes
  23. drwxr-xr-x  8 conan conan    4096 Aug 12 04:34 src
  24. -rw-r--r--  1 conan conan 1315806 Aug 12 04:34 zookeeper-3.4.5.jar
  25. -rw-r--r--  1 conan conan     833 Aug 12 04:34 zookeeper-3.4.5.jar.asc
  26. -rw-r--r--  1 conan conan      33 Aug 12 04:34 zookeeper-3.4.5.jar.md5
  27. -rw-r--r--  1 conan conan      41 Aug 12 04:34 zookeeper-3.4.5.jar.sha1
複製程式碼
修改配置檔案conf/zoo.cfg

  1. ~ mkdir /home/conan/zoo/zk0
  2. ~ cp conf/zoo_sample.cfg conf/zoo.cfg
  3. ~ vi conf/zoo.cfg
  4. tickTime=2000
  5. initLimit=10
  6. syncLimit=5
  7. dataDir=/home/conan/zoo/zk0
  8. clientPort=2181
複製程式碼
非常簡單,我們已經配置好了的zookeeper單節點 啟動zookeeper
  1. ~ bin/zkServer.sh
  2. JMX enabled by default
  3. Using config: /home/conan/zoo/zk0/zookeeper345/bin/../conf/zoo.cfg
  4. Usage: bin/zkServer.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}
  5. [email protected]:~/zoo/zk0/zookeeper345$ bin/zkServer.sh start
  6. JMX enabled by default
  7. Using config: /home/conan/zoo/zk0/zookeeper345/bin/../conf/zoo.cfg
  8. Starting zookeeper ... STARTED
  9. #zk的服務顯示為QuorumPeerMain
  10. ~ jps
  11. 5321 QuorumPeerMain
  12. 5338 Jps
  13. #檢視執行狀態
  14. ~ bin/zkServer.sh status
  15. JMX enabled by default
  16. Using config: /home/conan/zoo/zk0/zookeeper345/bin/../conf/zoo.cfg
  17. Mode: standalone
複製程式碼

單節點的時,Mode會顯示為standalone 停止ZooKeeper服務
  1. ~ bin/zkServer.sh stop
  2. JMX enabled by default
  3. Using config: /home/conan/zoo/zk0/zookeeper345/bin/../conf/zoo.cfg
  4. Stopping zookeeper ... STOPPED
複製程式碼

3. zookeeper偽分散式叢集安裝 所謂 “偽分散式叢集” 就是在,在一臺PC中,啟動多個ZooKeeper的例項。“完全分散式叢集” 是每臺PC,啟動一個ZooKeeper例項。 由於我的測試環境PC數量有限,所以在一臺PC中,啟動3個ZooKeeper的例項。 建立環境目錄
  1. ~ mkdir /home/conan/zoo/zk1
  2. ~ mkdir /home/conan/zoo/zk2
  3. ~ mkdir /home/conan/zoo/zk3
  4. #新建myid檔案
  5. ~ echo "1" > /home/conan/zoo/zk1/myid
  6. ~ echo "2" > /home/conan/zoo/zk2/myid
  7. ~ echo "3" > /home/conan/zoo/zk3/myid
複製程式碼
分別修改配置檔案
修改:dataDir,clientPort
增加:叢集的例項,server.X,”X”表示每個目錄中的myid的值
  1. ~ vi /home/conan/toolkit/zookeeper345/conf/zk1.cfg
  2. tickTime=2000
  3. initLimit=10
  4. syncLimit=5
  5. dataDir=/home/conan/zoo/zk1
  6. clientPort=2181
  7. server.1=192.168.1.201:2888:3888
  8. server.2=192.168.1.201:2889:3889
  9. server.3=192.168.1.201:2890:3890
  10. ~ vi /home/conan/toolkit/zookeeper345/conf/zk2.cfg
  11. tickTime=2000
  12. initLimit=10
  13. syncLimit=5
  14. dataDir=/home/conan/zoo/zk2
  15. clientPort=2182
  16. server.1=192.168.1.201:2888:3888
  17. server.2=192.168.1.201:2889:3889
  18. server.3=192.168.1.201:2890:3890
  19. ~ vi /home/conan/toolkit/zookeeper345/conf/zk3.cfg
  20. tickTime=2000
  21. initLimit=10
  22. syncLimit=5
  23. dataDir=/home/conan/zoo/zk3
  24. clientPort=2183
  25. server.1=192.168.1.201:2888:3888
  26. server.2=192.168.1.201:2889:3889
  27. server.3=192.168.1.201:2890:3890
複製程式碼

3個節點的ZooKeeper叢集配置完成,接下來我們的啟動服務。 啟動叢集
  1. ~ /home/conan/toolkit/zookeeper345/bin/zkServer.sh start zk1.cfg
  2. ~ /home/conan/toolkit/zookeeper345/bin/zkServer.sh start zk2.cfg
  3. ~ /home/conan/toolkit/zookeeper345/bin/zkServer.sh start zk3.cfg
  4. ~ jps
  5. 5422 QuorumPeerMain
  6. 5395 QuorumPeerMain
  7. 5463 QuorumPeerMain
  8. 5494 Jps
  9. #檢視節點狀態
  10. ~ /home/conan/toolkit/zookeeper345/bin/zkServer.sh status zk1.cfg
  11. JMX enabled by default
  12. Using config: /home/conan/toolkit/zookeeper345/bin/../conf/zk1.cfg
  13. Mode: follower
  14. ~ /home/conan/toolkit/zookeeper345/bin/zkServer.sh status zk2.cfg
  15. JMX enabled by default
  16. Using config: /home/conan/toolkit/zookeeper345/bin/../conf/zk2.cfg
  17. Mode: leader
  18. ~ /home/conan/toolkit/zookeeper345/bin/zkServer.sh status zk3.cfg
  19. JMX enabled by default
  20. Using config: /home/conan/toolkit/zookeeper345/bin/../conf/zk3.cfg
  21. Mode: follower
複製程式碼

我們可以看到zk2是leader,zk1和zk3是follower 檢視ZooKeeper物理檔案目錄結構
  1. ~ tree  -L 3 /home/conan/zoo
  2. /home/conan/zoo
  3. ├── zk0
  4. ├── zk1
  5. │   ├── myid
  6. │   ├── version-2
  7. │   │   ├── acceptedEpoch
  8. │   │   ├── currentEpoch
  9. │   │   ├── log.100000001
  10. │   │   └── snapshot.0
  11. │   └── zookeeper_server.pid
  12. ├── zk2
  13. │   ├── myid
  14. │   ├── version-2
  15. │   │   ├── acceptedEpoch
  16. │   │   ├── currentEpoch
  17. │   │   ├── log.100000001
  18. │   │   └── snapshot.0
  19. │   └── zookeeper_server.pid
  20. └── zk3
  21.     ├── myid
  22.     ├── version-2
  23.     │   ├── acceptedEpoch
  24.     │   ├── currentEpoch
  25.     │   ├── log.100000001
  26.     │   └── snapshot.100000000
  27.     └── zookeeper_server.pid
  28. 7 directories, 18 files
複製程式碼

4. zookeeper命令列操作 我們通過客戶端連線ZooKeeper的叢集,我們可以任意的zookeeper是進行連線。
  1. ~ /home/conan/toolkit/zookeeper345/bin/zkCli.sh -server 192.168.1.201:2181
  2. Connecting to 192.168.1.201
  3. 2013-08-12 05:25:39,260 [myid:] - INFO  [main:[email protected]] - Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT
  4. 2013-08-12 05:25:39,267 [myid:] - INFO  [main:[email protected]] - Client environment:host.name=conan
  5. 2013-08-12 05:25:39,269 [myid:] - INFO  [main:[email protected]] - Client environment:java.version=1.6.0_29
  6. 2013-08-12 05:25:39,269 [myid:] - INFO  [main:[email protected]] - Client environment:java.vendor=Sun Microsystems Inc.
  7. 2013-08-12 05:25:39,270 [myid:] - INFO  [main:[email protected]] - Client environment:java.home=/home/conan/toolkit/jdk16/jre
  8. 2013-08-12 05:25:39,270 [myid:] - INFO  [main:[email protected]] - Client environment:java.class.path=/home/conan/toolkit/zookeeper345/bin/../build/classes:/home/conan/toolkit/zookeeper345/bin/../build/lib/*.jar:/home/conan/toolkit/zookeeper345/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/conan/toolkit/zookeeper345/bin/../lib/slf4j-api-1.6.1.jar:/home/conan/toolkit/zookeeper345/bin/../lib/netty-3.2.2.Final.jar:/home/conan/toolkit/zookeeper345/bin/../lib/log4j-1.2.15.jar:/home/conan/toolkit/zookeeper345/bin/../lib/jline-0.9.94.jar:/home/conan/toolkit/zookeeper345/bin/../zookeeper-3.4.5.jar:/home/conan/toolkit/zookeeper345/bin/../src/java/lib/*.jar:/home/conan/toolkit/zookeeper345/bin/../conf:
  9. 2013-08-12 05:25:39,271 [myid:] - INFO  [main:[email protected]] - Client environment:java.library.path=/home/conan/toolkit/jdk16/jre/lib/amd64/server:/home/conan/toolkit/jdk16/jre/lib/amd64:/home/conan/toolkit/jdk16/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
  10. 2013-08-12 05:25:39,275 [myid:] - INFO  [main:[email protected]] - Client environment:java.io.tmpdir=/tmp
  11. 2013-08-12 05:25:39,276 [myid:] - INFO  [main:[email protected]] - Client environment:java.compiler=
  12. 2013-08-12 05:25:39,276 [myid:] - INFO  [main:[email protected]] - Client environment:os.name=Linux
  13. 2013-08-12 05:25:39,277 [myid:] - INFO  [main:[email protected]] - Client environment:os.arch=amd64
  14. 2013-08-12 05:25:39,281 [myid:] - INFO  [main:[email protected]] - Client environment:os.version=3.5.0-23-generic
  15. 2013-08-12 05:25:39,282 [myid:] - INFO  [main:[email protected]] - Client environment:user.name=conan
  16. 2013-08-12 05:25:39,282 [myid:] - INFO  [main:[email protected]] - Client environment:user.home=/home/conan
  17. 2013-08-12 05:25:39,283 [myid:] - INFO  [main:[email protected]] - Client environment:user.dir=/home/conan/zoo
  18. 2013-08-12 05:25:39,284 [myid:] - INFO  [main:[email protected]] - Initiating client connection, connectString=192.168.1.201 sessionTi[email protected]22ba6c83
  19. Welcome to ZooKeeper!
  20. JLine support is enabled