zookeeper叢集搭建和常用命令(筆記)
1.下載zookeeper安裝包
linux的套路是,一般都會把安裝檔案放到/opt下
wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/zookeeper-3.4.12.tar.gz
2.配置zoo.cfg
解壓,mv重新命名一下 然後進到 /opt/zookeeper/conf 把zoo_sample.cfg重新命名為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 an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/var/zookeeper # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1 server.1=172.16.30.100:2888:3888 server.2=172.16.30.101:2888:3888 server.3=172.16.30.102:2888:3888
主要修改加粗部分,配置三個節點
3.啟動節點
zookeeper是基於java開發的,所以執行zookeeper需要java環境。安裝好jdk之後,在/etc/bashrc里加入java環境
export JAVA_HOME=/home/hadoop/env/jdk1.7.0_80
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH
然後通過./bin/zkServer.sh start
啟動zookeeper節點。
啟動之後,可以通過telnet 驗證服務
telnet 172.16.30.100:2181
Trying 172.16.30.100...
Connected to 172.16.30.100.
Escape character is '^]'.
如上,說明zookeeper節點服務已經啟動,鍵入stat,如果返回下面內容,說明叢集已經正常工作
Zookeeper version: 3.4.12-e5259e437540f349646870ea94dc2658c4e44b3b, built on 03/27/2018 03:55 GMT Clients: /172.16.30.100:39281[0](queued=0,recved=1,sent=0) Latency min/avg/max: 0/0/0 Received: 2 Sent: 1 Connections: 1 Outstanding: 0 Zxid: 0x100000040 Mode: follower Node count: 19 Connection closed by foreign host.
正常情況下,當叢集裡超過半數節點啟動,zookeeper叢集才會正常執行,允許外部連線。
4.常用命令
通過zkCli.sh連線zookeeper服務
./bin/zkCli.sh -timeout 5000 -server 172.16.30.101:2181
WatchedEvent state:SyncConnected type:None path:null
[zk: 172.16.30.101:2181(CONNECTED) 0]
如上所示,連線成功,等待執行命令。
鍵入h,回車
ZooKeeper -server host:port cmd args
connect host:port
get path [watch]
ls path [watch]
set path data [version]
rmr path
delquota [-n|-b] path
quit
printwatches on|off
create [-s] [-e] path data acl
stat path [watch]
close
ls2 path [watch]
history
listquota path
setAcl path acl
getAcl path
sync path
redo cmdno
addauth scheme auth
delete path [version]
setquota -n|-b val path
可以看到一堆命令
connect …是連線到其他節點 get path 檢視當前路徑znode的資訊(包含data) ls path 檢視當前路徑的子znode set path… 設定當前路徑znode資料 rmr path… 遞迴刪除當前路徑節點及所有子節點 delquota… 刪除策略,-n代表子節點數量,-b代表資料長度 quit 退出 create 建立節點,-s代表持久,-e代表臨時 stat path… 檢視某路徑節點狀態 close 關閉連線 ls2 path… 檢視當前路徑子節點和當前節點狀態 history 列出歷史執行命令清單 listquota… 檢視當前路徑節點策略情況 delete path刪除當前路徑節點 setquota 設定路徑節點策略