- 启动ZOOKEEPER
配置好ZOOKEEPER后,进入./bin 目录,执行./zkServer.sh start
[hadoop@hadoop4 bin]$ ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/hadoop/apps/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
- 查看ZOOKEEPER配置
zkServer.sh status
[hadoop@hadoop2 bin]$ ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: leader
- jps
查看java进程
[hadoop@hadoop2 bin]$ jps
24538 Jps
22847 QuorumPeerMain
- 关闭ZOOKEEPER
./zkServer.sh stop
[hadoop@hadoop4 bin]$ ./zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /home/hadoop/apps/zookeeper-3.4.10/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[hadoop@hadoop4 bin]$ ./zkServer
客户端使用
登录客户端,默认登录本地的服务器
./zkCli.sh
登录远程ZOOKEEPER节点
./zkCli.sh -server hadoop3:2181
Connecting to hadoop3:2181
2018-01-15 22:32:42,666 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
2018-01-15 22:32:42,669 [myid:] - INFO [main:Environment@100] - Client environment:host.name=hadoop1
2018-01-15 22:32:42,670 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_151
2018-01-15 22:32:42,671 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2018-01-15 22:32:42,672 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/bigdata/jdk1.8.0_151/jre
2018-01-15 22:32:42,672 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper/bin/../build/classes:/usr/local/zookeeper/bin/../build/lib/*.jar:/usr/local/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper/bin/../lib/netty-3.10.5.Final.jar:/usr/local/zookeeper/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper/bin/../zookeeper-3.4.10.jar:/usr/local/zookeeper/bin/../src/java/lib/*.jar:/usr/local/zookeeper/bin/../conf:
2018-01-15 22:32:42,672 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2018-01-15 22:32:42,672 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2018-01-15 22:32:42,672 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2018-01-15 22:32:42,672 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2018-01-15 22:32:42,672 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2018-01-15 22:32:42,672 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-693.el7.x86_64
2018-01-15 22:32:42,672 [myid:] - INFO [main:Environment@100] - Client environment:user.name=hadoop
2018-01-15 22:32:42,672 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/home/hadoop
2018-01-15 22:32:42,672 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/home/hadoop/apps/zookeeper-3.4.10/bin
2018-01-15 22:32:42,675 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=hadoop3:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@1a86f2f1
Welcome to ZooKeeper!
2018-01-15 22:32:42,697 [myid:] - INFO [main-SendThread(hadoop3:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server hadoop3/10.211.55.8:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2018-01-15 22:32:42,760 [myid:] - INFO [main-SendThread(hadoop3:2181):ClientCnxn$SendThread@876] - Socket connection established to hadoop3/10.211.55.8:2181, initiating session
2018-01-15 22:32:42,784 [myid:] - INFO [main-SendThread(hadoop3:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server hadoop3/10.211.55.8:2181, sessionid = 0x460fa0697de0002, negotiated timeout = 30000
[zk: hadoop3:2181(CONNECTED) 0]
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
- 帮助
help
[zk: localhost:2181(CONNECTED) 1] help
ZooKeeper -server host:port cmd args
stat path [watch]
set path data [version]
ls path [watch]
delquota [-n|-b] path
ls2 path [watch]
setAcl path acl
setquota -n|-b val path
history
redo cmdno
printwatches on|off
delete path [version]
sync path
listquota path
rmr path
get path [watch]
create [-s] [-e] path data acl
addauth scheme auth
quit
getAcl path
close
connect host:port
[zk: localhost:2181(CONNECTED) 2]
- stat path
查看某个节点的属性
[zk: hadoop3:2181(CONNECTED) 2] stat /test
cZxid = 0x5d0000000f
ctime = Mon Jan 15 22:24:15 CST 2018
mZxid = 0x5d00000010
mtime = Mon Jan 15 22:24:27 CST 2018
pZxid = 0x5d0000000f
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
- stat paht watch
监听节点状态
[zk: hadoop3:2181(CONNECTED) 5] stat /test watch
cZxid = 0x5d0000000f
ctime = Mon Jan 15 22:24:15 CST 2018
mZxid = 0x5d0000001b
mtime = Mon Jan 15 22:35:33 CST 2018
pZxid = 0x5d0000000f
cversion = 0
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
[zk: hadoop3:2181(CONNECTED) 6]
监听节点,然后我们登录另外一个机器,修改/test节点的值,触发监听
[zk: hadoop3:2181(CONNECTED) 6]
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/test
set path data
设置某节点的值set path data version
根据version来设置某节点的值,版本号不一致,则不能修改
/test的版本是3
[zk: hadoop3:2181(CONNECTED) 8] stat /test
cZxid = 0x5d0000000f
ctime = Mon Jan 15 22:24:15 CST 2018
mZxid = 0x5d0000001e
mtime = Mon Jan 15 22:36:40 CST 2018
pZxid = 0x5d0000001d
cversion = 1
dataVersion = 3
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 1
[zk: hadoop3:2181(CONNECTED) 9] set /test 3333 0
version No is not valid : /test ---设置出错
[zk: hadoop3:2181(CONNECTED) 12] set /test 3333 3
cZxid = 0x5d0000000f
ctime = Mon Jan 15 22:24:15 CST 2018
mZxid = 0x5d00000021
mtime = Mon Jan 15 22:40:28 CST 2018
pZxid = 0x5d0000001d
cversion = 1
dataVersion = 4
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 1
- ls path
查看某节点的子节点
[zk: hadoop3:2181(CONNECTED) 13] ls /test
[a]
- ls path watch
监听目录结构的变化
监听
[zk: hadoop3:2181(CONNECTED) 14] ls /test watch
[a]
在另外一个客户端,在/test节点下创建新的节点
[zk: localhost:2181(CONNECTED) 7] create /test/c 123
Created /test/c
触发监听
[zk: hadoop3:2181(CONNECTED) 16]
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/test
delete
删除节点,如果存在子节点,不能删除rmr
删除节点以及子节点get path
获取某个节点的值get path watch
获取某个节点的值,并监听节点值的变化create [-s] [-e] path data acl
-s 创建顺序节点
-e 创建临时节点
[zk: hadoop3:2181(CONNECTED) 19] create -s /a aaa
Created /a0000000010
客户端取消连接后,节点被删除,临时节点下不能创建子节点
[zk: hadoop3:2181(CONNECTED) 16] create -e /t 123
Created /t
[zk: hadoop3:2181(CONNECTED) 17] create /t/a 123
Ephemerals cannot have children: /t/a
- connect
连接某个ZOOKEEPER服务节点
[zk: hadoop3(CONNECTED) 21] connect hadoop0 2181
2018-01-15 22:48:54,097 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x460fa0697de0003 closed
2018-01-15 22:48:54,097 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=hadoop0 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@182decdb
2018-01-15 22:48:54,099 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x460fa0697de0003
[zk: hadoop0(CONNECTING) 22] 2018-01-15 22:48:54,100 [myid:] - INFO [main-SendThread(hadoop0:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server hadoop0/10.211.55.5:2181. Will not attempt to authenticate using SASL (unknown error)
2018-01-15 22:48:54,101 [myid:] - INFO [main-SendThread(hadoop0:2181):ClientCnxn$SendThread@876] - Socket connection established to hadoop0/10.211.55.5:2181, initiating session
2018-01-15 22:48:54,145 [myid:] - INFO [main-SendThread(hadoop0:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server hadoop0/10.211.55.5:2181, sessionid = 0x160fa06156c0000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: hadoop0(CONNECTED) 22]
ZOOKEEPER 四字命令
- conf
显示ZOOKEEPER节点配置信息
[hadoop@hadoop1 bin]$ telnet hadoop3 2181
Trying 10.211.55.8...
Connected to hadoop3.
Escape character is '^]'.
conf
clientPort=2181
dataDir=/usr/local/zookeeper/data/version-2
dataLogDir=/usr/local/zookeeper/log/version-2
tickTime=2000
maxClientCnxns=60
minSessionTimeout=4000
maxSessionTimeout=40000
serverId=4
initLimit=10
syncLimit=5
electionAlg=3
electionPort=3888
quorumPort=2888
peerType=0
Connection closed by foreign host.
- cons
显示某个ZOOKEEPER节点连接信息
[hadoop@hadoop1 bin]$ telnet hadoop3 2181
Trying 10.211.55.8...
Connected to hadoop3.
Escape character is '^]'.
cons
/10.211.55.9:50720[1](queued=0,recved=1,sent=1,sid=0x460fa0697de0004,lop=SESS,est=1516027865069,to=30000,lcxid=0x0,lzxid=0x5d0000002c,lresp=1516027865081,llat=5,minlat=0,avglat=5,maxlat=5)
/10.211.55.10:33798[0](queued=0,recved=1,sent=0)
Connection closed by foreign host.
[hadoop@hadoop1 bin]$
- stat
输出ZOOKEEPER运行时信息
[hadoop@hadoop1 bin]$ telnet hadoop3 2181
Trying 10.211.55.8...
Connected to hadoop3.
Escape character is '^]'.
stat
Zookeeper version: 3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
Clients:
/10.211.55.10:33802[0](queued=0,recved=1,sent=0)
/10.211.55.5:58326[1](queued=0,recved=1,sent=1)
/10.211.55.9:50720[1](queued=0,recved=16,sent=16)
Latency min/avg/max: 0/2/31
Received: 146
Sent: 148
Connections: 3
Outstanding: 0
Zxid: 0x5d0000002d
Mode: follower
Node count: 8
Connection closed by foreign host.