zookeeper叢集安裝、配置、主要Shell操作
阿新 • • 發佈:2018-12-04
1.下載
http://apache.fayea.com/zookeeper/
這裡選擇 stable 目錄下的版本,下載並上傳到 Linux。
解壓壓縮包到 /usr/local/ 目錄(注意許可權問題),並修改名稱為 zk,擁有者為登入使用者:
在 ~/.bashrc 檔案末尾新增:
export ZOOKEEPER_HOME=/usr/local/zk
export PATH=$PATH:$ZOOKEEPER_HOME/bin/
執行命令 source ~/.bashrc,即可在任意路徑使用zookeeper命令。
2.配置
進入 /usr/local/zk/目錄:
-
建立 data 目錄,並新增檔案myid,內容為 0:
-
進入 /usr/local/zk/conf/ 目錄:
將 zoo_sample.cfg 刪除(或修改名稱)
- 建立配置檔案 zoo.cfg,內容如下,最後節點數量,名稱根據自身叢集設定(這裡設定了共4個節點):
[email protected]ster:/usr/local/zk/conf$ cat zoo.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 ian acknowledgement syncLimit=5 # the directory where the snapshot is stored. dataDir=/usr/local/zk/data # the ort at which the clients will connect clientPort=2183 # the location of the log file dataLogDir=/usr/local/zk/log server.0=master:2288:3388 server.1=slave4:2288:3388 server.2=slave5:2288:3388 server.3=slave7:2288:3388
- 將 zk 目錄傳遞給其他節點:
注:可能涉及到許可權問題,可先將 zk 傳遞至 /home/hadoop/
scp -r /usr/local/zk slave4:/usr/local/
scp -r /usr/local/zk slave5:/usr/local/
scp -r /usr/local/zk slave7:/usr/local/
將各個節點的 zk/data/myid 檔案內容改為 zoo.cfg 末行對應的數值,如這裡的三個資料節點的配置:
[[email protected] ~]$ cat /usr/local/zk/data/myid
1
[[email protected] ~]$ cat /usr/local/zk/data/myid
2
[[email protected] ~]$ cat /usr/local/zk/data/myid
3
3. 啟動
-
將HBase自帶的zookeeper關閉
註釋掉 hbase-env.sh 檔案下的如下行:
-
啟動
- 開啟叢集的Hbase
- 在配置的每個節點開啟 zookeeper
在每個節點執行命令,若前面配置的無須路徑啟動沒有成功,則使用 /usr/local/zk/bin/zkServer.sh start:
zkServer.sh start
關閉:
zkServer.sh stop
檢視JPS:
配置成功!
4. 主要Shell操作
-
連線服務:
-
執行到 WatchedEvent state: SyncConneted type:None path:null 如果卡住,請按回車鍵:
-
檢視當前zookeeper所包含的內容:
-
建立一個Znode節點及它的內容:
[zk: localhost:2181(CONNECTED) 1] create /zk myData
Created /zk
[zk: localhost:2181(CONNECTED) 2] ls /
[zk, zookeeper, hbase]
- 檢視 /zk 的內容:
[zk: localhost:2181(CONNECTED) 3] get /zk
myData
cZxid = 0x40000004d
ctime = Sun Nov 18 19:44:34 CST 2018
mZxid = 0x40000004d
mtime = Sun Nov 18 19:44:34 CST 2018
pZxid = 0x40000004d
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
- 設定(更改)/zk 內容:
[zk: localhost:2181(CONNECTED) 5] set /zk hadoopData
cZxid = 0x40000004d
ctime = Sun Nov 18 19:44:34 CST 2018
mZxid = 0x40000004e
mtime = Sun Nov 18 19:46:55 CST 2018
pZxid = 0x40000004d
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 10
numChildren = 0
[zk: localhost:2181(CONNECTED) 6] get /zk
hadoopData
cZxid = 0x40000004d
ctime = Sun Nov 18 19:44:34 CST 2018
mZxid = 0x40000004e
mtime = Sun Nov 18 19:46:55 CST 2018
pZxid = 0x40000004d
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 10
numChildren = 0
- 刪除 /zk
[zk: localhost:2181(CONNECTED) 7] delete /zk
[zk: localhost:2181(CONNECTED) 8] ls /
[zookeeper, hbase]
5. 節點之間的關聯
既然單個節點能夠連線 ZK 服務,那各個節點之間有什麼關聯呢?
通過一個例子展示:
- 在 master、slave4 節點分別連線服務:
zkCli.sh -server localhost:2181
- 在 master 節點建立新的Znode:
master:
[zk: localhost:2181(CONNECTED) 15] create /zk master
Created /zk
[zk: localhost:2181(CONNECTED) 16] get /zk
master
cZxid = 0x400000054
ctime = Sun Nov 18 20:25:38 CST 2018
mZxid = 0x400000054
mtime = Sun Nov 18 20:25:38 CST 2018
pZxid = 0x400000054
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
slave4:
[zk: localhost:2181(CONNECTED) 5] get /zk
master
cZxid = 0x400000054
ctime = Sun Nov 18 20:25:38 CST 2018
mZxid = 0x400000054
mtime = Sun Nov 18 20:25:38 CST 2018
pZxid = 0x400000054
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
可以看出,各個節點在 ZK 服務上的Znode節點資訊可以互相檢視,甚至修改、刪除。