zookeeper執行環境2、3:單節點安裝和偽分散式叢集安裝
阿新 • • 發佈:2019-01-13
問題導讀:
1.什麼是zookeeper2.zookeeper有幾種安裝方式?
3.zookeeper偽分佈如何配置myid?
4.zookeeper包含哪些常用操作命令?
前言
ZooKeeper是Hadoop家族的一款高效能的分散式協作的產品。在單機中,系統協作大都是程序級的操作。分散式系統中,服務協作都是跨伺服器才能完成的。在ZooKeeper之前,我們對於協作服務大都使用訊息中介軟體,隨著分散式系統的普及,用訊息中介軟體完成協作,會有大量的程式開發。ZooKeeper直接面向於分散式系統,可以減少我們自己的開發,幫助我們更好完成分散式系統的資料管理問題。 目錄
- zookeeper介紹
- zookeeper單節點安裝
- zookeeper偽分散式叢集安裝
- zookeeper命令列操作
-
Java程式設計現實命令列操作
-
~ uname -a
-
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
-
~ cat /etc/issue
-
Ubuntu 12.04.2 LTS \n \l
-
~ java -version
-
java version "1.6.0_29"
-
Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
- Java HotSpot(TM) 64-Bit Server VM (build 20.4-b02, mixed mode)
下載zookeeper
-
~ mkdir /home/conan/toolkit
-
~ cd /home/conan/toolkit
-
~ wget http://apache.dataguru.cn/zookeeper/stable/zookeeper-3.4.5.tar.gz
-
~ tar xvf zookeeper-3.4.5.tar.gz
-
~ mv zookeeper-3.4.5 zookeeper345
-
~ cd zookeeper345
-
~ ls -l
-
drwxr-xr-x 2 conan conan 4096 Aug 12 04:34 bin
-
-rw-r--r-- 1 conan conan 75988 Aug 12 04:34 build.xml
-
-rw-r--r-- 1 conan conan 70223 Aug 12 04:34 CHANGES.txt
-
drwxr-xr-x 2 conan conan 4096 Aug 12 04:34 conf
-
drwxr-xr-x 10 conan conan 4096 Aug 12 04:34 contrib
-
drwxr-xr-x 2 conan conan 4096 Aug 12 04:34 dist-maven
-
drwxr-xr-x 6 conan conan 4096 Aug 12 04:34 docs
-
-rw-r--r-- 1 conan conan 1953 Aug 12 04:34 ivysettings.xml
-
-rw-r--r-- 1 conan conan 3120 Aug 12 04:34 ivy.xml
-
drwxr-xr-x 4 conan conan 4096 Aug 12 04:34 lib
-
-rw-r--r-- 1 conan conan 11358 Aug 12 04:34 LICENSE.txt
-
-rw-r--r-- 1 conan conan 170 Aug 12 04:34 NOTICE.txt
-
-rw-r--r-- 1 conan conan 1770 Aug 12 04:34 README_packaging.txt
-
-rw-r--r-- 1 conan conan 1585 Aug 12 04:34 README.txt
-
drwxr-xr-x 5 conan conan 4096 Aug 12 04:34 recipes
-
drwxr-xr-x 8 conan conan 4096 Aug 12 04:34 src
-
-rw-r--r-- 1 conan conan 1315806 Aug 12 04:34 zookeeper-3.4.5.jar
-
-rw-r--r-- 1 conan conan 833 Aug 12 04:34 zookeeper-3.4.5.jar.asc
-
-rw-r--r-- 1 conan conan 33 Aug 12 04:34 zookeeper-3.4.5.jar.md5
- -rw-r--r-- 1 conan conan 41 Aug 12 04:34 zookeeper-3.4.5.jar.sha1
修改配置檔案conf/zoo.cfg
-
~ mkdir /home/conan/zoo/zk0
-
~ cp conf/zoo_sample.cfg conf/zoo.cfg
-
~ vi conf/zoo.cfg
-
tickTime=2000
-
initLimit=10
-
syncLimit=5
-
dataDir=/home/conan/zoo/zk0
- clientPort=2181
非常簡單,我們已經配置好了的zookeeper單節點 啟動zookeeper
-
~ bin/zkServer.sh
-
JMX enabled by default
-
Using config: /home/conan/zoo/zk0/zookeeper345/bin/../conf/zoo.cfg
-
Usage: bin/zkServer.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}
-
[email protected]:~/zoo/zk0/zookeeper345$ bin/zkServer.sh start
-
JMX enabled by default
-
Using config: /home/conan/zoo/zk0/zookeeper345/bin/../conf/zoo.cfg
-
Starting zookeeper ... STARTED
-
#zk的服務顯示為QuorumPeerMain
-
~ jps
-
5321 QuorumPeerMain
-
5338 Jps
-
#檢視執行狀態
-
~ bin/zkServer.sh status
-
JMX enabled by default
-
Using config: /home/conan/zoo/zk0/zookeeper345/bin/../conf/zoo.cfg
- Mode: standalone
單節點的時,Mode會顯示為standalone 停止ZooKeeper服務
-
~ bin/zkServer.sh stop
-
JMX enabled by default
-
Using config: /home/conan/zoo/zk0/zookeeper345/bin/../conf/zoo.cfg
- Stopping zookeeper ... STOPPED
3. zookeeper偽分散式叢集安裝 所謂 “偽分散式叢集” 就是在,在一臺PC中,啟動多個ZooKeeper的例項。“完全分散式叢集” 是每臺PC,啟動一個ZooKeeper例項。 由於我的測試環境PC數量有限,所以在一臺PC中,啟動3個ZooKeeper的例項。 建立環境目錄
-
~ mkdir /home/conan/zoo/zk1
-
~ mkdir /home/conan/zoo/zk2
-
~ mkdir /home/conan/zoo/zk3
-
#新建myid檔案
-
~ echo "1" > /home/conan/zoo/zk1/myid
-
~ echo "2" > /home/conan/zoo/zk2/myid
- ~ echo "3" > /home/conan/zoo/zk3/myid
分別修改配置檔案
修改:dataDir,clientPort
增加:叢集的例項,server.X,”X”表示每個目錄中的myid的值
-
~ vi /home/conan/toolkit/zookeeper345/conf/zk1.cfg
-
tickTime=2000
-
initLimit=10
-
syncLimit=5
-
dataDir=/home/conan/zoo/zk1
-
clientPort=2181
-
server.1=192.168.1.201:2888:3888
-
server.2=192.168.1.201:2889:3889
-
server.3=192.168.1.201:2890:3890
-
~ vi /home/conan/toolkit/zookeeper345/conf/zk2.cfg
-
tickTime=2000
-
initLimit=10
-
syncLimit=5
-
dataDir=/home/conan/zoo/zk2
-
clientPort=2182
-
server.1=192.168.1.201:2888:3888
-
server.2=192.168.1.201:2889:3889
-
server.3=192.168.1.201:2890:3890
-
~ vi /home/conan/toolkit/zookeeper345/conf/zk3.cfg
-
tickTime=2000
-
initLimit=10
-
syncLimit=5
-
dataDir=/home/conan/zoo/zk3
-
clientPort=2183
-
server.1=192.168.1.201:2888:3888
-
server.2=192.168.1.201:2889:3889
- server.3=192.168.1.201:2890:3890
3個節點的ZooKeeper叢集配置完成,接下來我們的啟動服務。 啟動叢集
-
~ /home/conan/toolkit/zookeeper345/bin/zkServer.sh start zk1.cfg
-
~ /home/conan/toolkit/zookeeper345/bin/zkServer.sh start zk2.cfg
-
~ /home/conan/toolkit/zookeeper345/bin/zkServer.sh start zk3.cfg
-
~ jps
-
5422 QuorumPeerMain
-
5395 QuorumPeerMain
-
5463 QuorumPeerMain
-
5494 Jps
-
#檢視節點狀態
-
~ /home/conan/toolkit/zookeeper345/bin/zkServer.sh status zk1.cfg
-
JMX enabled by default
-
Using config: /home/conan/toolkit/zookeeper345/bin/../conf/zk1.cfg
-
Mode: follower
-
~ /home/conan/toolkit/zookeeper345/bin/zkServer.sh status zk2.cfg
-
JMX enabled by default
-
Using config: /home/conan/toolkit/zookeeper345/bin/../conf/zk2.cfg
-
Mode: leader
-
~ /home/conan/toolkit/zookeeper345/bin/zkServer.sh status zk3.cfg
-
JMX enabled by default
-
Using config: /home/conan/toolkit/zookeeper345/bin/../conf/zk3.cfg
- Mode: follower
我們可以看到zk2是leader,zk1和zk3是follower 檢視ZooKeeper物理檔案目錄結構
-
~ tree -L 3 /home/conan/zoo
-
/home/conan/zoo
-
├── zk0
-
├── zk1
-
│ ├── myid
-
│ ├── version-2
-
│ │ ├── acceptedEpoch
-
│ │ ├── currentEpoch
-
│ │ ├── log.100000001
-
│ │ └── snapshot.0
-
│ └── zookeeper_server.pid
-
├── zk2
-
│ ├── myid
-
│ ├── version-2
-
│ │ ├── acceptedEpoch
-
│ │ ├── currentEpoch
-
│ │ ├── log.100000001
-
│ │ └── snapshot.0
-
│ └── zookeeper_server.pid
-
└── zk3
-
├── myid
-
├── version-2
-
│ ├── acceptedEpoch
-
│ ├── currentEpoch
-
│ ├── log.100000001
-
│ └── snapshot.100000000
-
└── zookeeper_server.pid
- 7 directories, 18 files
4. zookeeper命令列操作 我們通過客戶端連線ZooKeeper的叢集,我們可以任意的zookeeper是進行連線。
-
~ /home/conan/toolkit/zookeeper345/bin/zkCli.sh -server 192.168.1.201:2181
-
Connecting to 192.168.1.201
-
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
-
2013-08-12 05:25:39,267 [myid:] - INFO [main:[email protected]] - Client environment:host.name=conan
-
2013-08-12 05:25:39,269 [myid:] - INFO [main:[email protected]] - Client environment:java.version=1.6.0_29
-
2013-08-12 05:25:39,269 [myid:] - INFO [main:[email protected]] - Client environment:java.vendor=Sun Microsystems Inc.
-
2013-08-12 05:25:39,270 [myid:] - INFO [main:[email protected]] - Client environment:java.home=/home/conan/toolkit/jdk16/jre
-
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:
-
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
-
2013-08-12 05:25:39,275 [myid:] - INFO [main:[email protected]] - Client environment:java.io.tmpdir=/tmp
-
2013-08-12 05:25:39,276 [myid:] - INFO [main:[email protected]] - Client environment:java.compiler=
-
2013-08-12 05:25:39,276 [myid:] - INFO [main:[email protected]] - Client environment:os.name=Linux
-
2013-08-12 05:25:39,277 [myid:] - INFO [main:[email protected]] - Client environment:os.arch=amd64
-
2013-08-12 05:25:39,281 [myid:] - INFO [main:[email protected]] - Client environment:os.version=3.5.0-23-generic
-
2013-08-12 05:25:39,282 [myid:] - INFO [main:[email protected]] - Client environment:user.name=conan
-
2013-08-12 05:25:39,282 [myid:] - INFO [main:[email protected]] - Client environment:user.home=/home/conan
-
2013-08-12 05:25:39,283 [myid:] - INFO [main:[email protected]] - Client environment:user.dir=/home/conan/zoo
-
2013-08-12 05:25:39,284 [myid:] - INFO [main:[email protected]] - Initiating client connection, connectString=192.168.1.201 sessionTi[email protected]22ba6c83
-
Welcome to ZooKeeper!
-
JLine support is enabled