一、单节点安装
1、下载解压
tar -zxvf zookeeper-3.4.6.tar.gz
2、创建配置文件
$ZOOKEEPER_HOME/conf/下创建zoo.cfg
配置
tickTime=2000
dataDir=/usr/local/soft/job/zookeeper-3.4.6/data
dataLogDir=/usr/local/soft/job/zookeeper-3.4.6/logs
clientPort=4180
data和logs目录需要手动创建,否则服务不能正常启动
客户端连接会报错:Will not attempt to authenticate using SASL
环境变量:
ZOOKEEPER_HOME=/usr/local/soft/job/zookeeper-3.4.6
export ZOOKEEPER_HOME
export PATH=${PATH}:${ZOOKEEPER_HOME}/bin
3、启动
./zkServer.sh start
4、客户端连接
./zkCli.sh -server localhost:4180
Connecting to localhost:4180
2015-09-23 15:29:33,386 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
2015-09-23 15:29:33,392 [myid:] - INFO [main:Environment@100] - Client environment:host.name=<NA>
2015-09-23 15:29:33,393 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_60
2015-09-23 15:29:33,395 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2015-09-23 15:29:33,395 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/local/soft/java/jdk1.8.0_60/jre
2015-09-23 15:29:33,395 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/usr/local/soft/job/zookeeper-3.4.6/bin/../build/classes:/usr/local/soft/job/zookeeper-3.4.6/bin/../build/lib/*.jar:/usr/local/soft/job/zookeeper-3.4.6/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/soft/job/zookeeper-3.4.6/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/soft/job/zookeeper-3.4.6/bin/../lib/netty-3.7.0.Final.jar:/usr/local/soft/job/zookeeper-3.4.6/bin/../lib/log4j-1.2.16.jar:/usr/local/soft/job/zookeeper-3.4.6/bin/../lib/jline-0.9.94.jar:/usr/local/soft/job/zookeeper-3.4.6/bin/../zookeeper-3.4.6.jar:/usr/local/soft/job/zookeeper-3.4.6/bin/../src/java/lib/*.jar:/usr/local/soft/job/zookeeper-3.4.6/bin/../conf:.:/usr/local/soft/java/jdk1.8.0_60/lib/dt.jar:/usr/local/soft/java/jdk1.8.0_60/lib/tools.jar
2015-09-23 15:29:33,395 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2015-09-23 15:29:33,395 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2015-09-23 15:29:33,396 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2015-09-23 15:29:33,396 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2015-09-23 15:29:33,396 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2015-09-23 15:29:33,396 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-229.el7.x86_64
2015-09-23 15:29:33,396 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2015-09-23 15:29:33,396 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2015-09-23 15:29:33,396 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/usr/local/soft/job/zookeeper-3.4.6/bin
2015-09-23 15:29:33,399 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:4180 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@799f7e29
Welcome to ZooKeeper!
JLine support is enabled
2015-09-23 15:29:33,547 [myid:] - INFO [main-SendThread(localhost:4180):ClientCnxn$SendThread@975] - Opening socket connection to server localhost/192.168.137.105:4180. Will not attempt to authenticate using SASL (unknown error)
2015-09-23 15:29:33,654 [myid:] - INFO [main-SendThread(localhost:4180):ClientCnxn$SendThread@852] - Socket connection established to localhost/192.168.137.105:4180, initiating session
2015-09-23 15:29:33,716 [myid:] - INFO [main-SendThread(localhost:4180):ClientCnxn$SendThread@1235] - Session establishment complete on server localhost/192.168.137.105:4180, sessionid = 0x14ff913a33c0000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:4180(CONNECTED) 0]
[zk: localhost:4180(CONNECTED) 0]
[zk: localhost:4180(CONNECTED) 0]
[zk: localhost:4180(CONNECTED) 0]
[zk: localhost:4180(CONNECTED) 0]
[zk: localhost:4180(CONNECTED) 0]
二、为分布式安装
1、创建环境目录
[root@localhost zookeeper-3.4.6]# mkdir zk1
[root@localhost zookeeper-3.4.6]# mkdir zk2
[root@localhost zookeeper-3.4.6]# mkdir zk3
2、创建myid文件
[root@localhost zookeeper-3.4.6]# echo "1" > ./zk1/myid
[root@localhost zookeeper-3.4.6]# echo "2" > ./zk2/myid
[root@localhost zookeeper-3.4.6]# echo "3" > ./zk3/myid
3、分别修改配置文件
修改:dataDir,clientPort
增加:集群的实例,server.X,”X”表示每个目录中的myid的值
[root@localhost conf]# vi zk1.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/soft/job/zookeeper-3.4.6/zk1
clientPort=2181
server.1=192.168.137.105:2888:3888
server.2=192.168.137.105:2889:3889
server.3=192.168.137.105:2890:3890
[root@localhost conf]# vi zk2.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/soft/job/zookeeper-3.4.6/zk2
clientPort=2182
server.1=192.168.137.105:2888:3888
server.2=192.168.137.105:2889:3889
server.3=192.168.137.105:2890:3890
[root@localhost conf]# vi zk3.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/soft/job/zookeeper-3.4.6/zk3
clientPort=2183
server.1=192.168.137.105:2888:3888
server.2=192.168.137.105:2889:3889
server.3=192.168.137.105:2890:3890
4、启动
[root@localhost bin]# ./zkServer.sh start zk1.cfg
[root@localhost bin]# ./zkServer.sh start zk2.cfg
[root@localhost bin]# ./zkServer.sh start zk3.cfg
5、查看状态
[root@localhost bin]# jps
8130 Jps
7833 QuorumPeerMain
7805 QuorumPeerMain
7853 QuorumPeerMain
[root@localhost bin]# ./zkServer.sh status zk1.cfg
JMX enabled by default
Using config: /usr/local/soft/job/zookeeper-3.4.6/bin/../conf/zk1.cfg
Mode: follower
[root@localhost bin]# ./zkServer.sh status zk2.cfg
JMX enabled by default
Using config: /usr/local/soft/job/zookeeper-3.4.6/bin/../conf/zk2.cfg
Mode: leader
[root@localhost bin]# ./zkServer.sh status zk3.cfg
JMX enabled by default
Using config: /usr/local/soft/job/zookeeper-3.4.6/bin/../conf/zk3.cfg
Mode: follower
可以看到zk2是leader,zk1和zk3是follower
三、分布式安装
1、创建myid文件
Data目录下创建myid
分别写1,2,3
2、修改配置文件
tickTime=2000 dataDir=/usr/local/soft/job/zookeeper-3.4.6/data dataLogDir=/usr/local/soft/job/zookeeper-3.4.6/logs clientPort=4180 initLimit=10 syncLimit=5 server.1=192.168.137.105:2888:3888 server.2=192.168.137.106:2888:3888 server.3=192.168.137.107:2888:3888
3、启动
zkServer.sh start
4、连接
zkCli.sh -server 192.168.137.107:4180
5、查看状态
jps
四、操作
[zk: 192.168.137.105:2181(CONNECTED) 0] 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: 192.168.137.105:2181(CONNECTED) 1]