一、本地模式安装部署
- 安装前准备
安装Jdk
安装Jdk
拷贝zookeeper安装包到Linux系统下
解压到指定目录
[jh@hadoop102 software]$ tar -zxvf zookeeper-3.4.10.tar.gz -C /opt/module/
将/opt/module/zookeeper-3.4.10/conf 这个路径下的 zoo_sample.cfg修改为zoo.cfg
[jh@hadoop102 conf]$ mv zoo_sample.cfg zoo.cfg
编辑zoo.cfg文件,修改dataDir路径:
[jh@hadoop102 zookeeper-3.4.10]$ vim zoo.cfg
dataDir=/opt/module/zookeeper-3.4.10/zkData
在/opt/module/zookeeper-3.4.10/这个目录上创建zkData文件夹
[jh@hadoop102 zookeeper-3.4.10]$ mkdir zkData
启动zookeeper
[jh@hadoop102 zookeeper-3.4.10]$ bin/zkServer.sh start
查看进程是否启动
[jh@hadoop102 zookeeper-3.4.10]$ jps
4020 Jps
4001 QuorumPeerMain
查看状态:
[jinghang@hadoop102 zookeeper-3.4.10]$ bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/…/conf/zoo.cfg
Mode: standalone
启动客户端:
[jinghang@hadoop102 zookeeper-3.4.10]$ bin/zkCli.sh
退出客户端:
[zk: localhost:2181(CONNECTED) 0] quit
停止Zookeeper
[jinghang@hadoop102 zookeeper-3.4.10]$ bin/zkServer.sh stop
zookeeper中的配置文件zoo.cfg中参数含义解读如下:
zookeeper 集群搭建指的是 zooKeeper 分布式模式安装,通常由 2n+l台 servers 组成。这是因为为了保证 Leader 选举(基于Paxos 算法的实现)能过得到多数的支持,所以 zookeeper集群的数量一般为奇数。 zookeeper运行需要 java环境,所以需要提前安装jdk。对于安装leader+follower 模式的集群,大致过程如下:
解压zookeeper安装包到/opt/module/目录下
[jh@hadoop102 software]$ tar -zxvf zookeeper-3.4.10.tar.gz -C /opt/module/
在/opt/module/zookeeper-3.4.10/这个目录下创建zkData
[jh@hadoop102 zookeeper-3.4.10]$ mkdir -p zkData
在/opt/module/zookeeper-3.4.10/zkData 目录下创建一个myid的文件
[jh@hadoop102 zkData]$ touch myid
编辑myid文件
[jh@hadoop102 zkData]$ vi myid
在文件中添加与server对应的编号:
2
拷贝配置好的zookeeper到其他机器上
[jh@hadoop102 zkData]$ rsync -av myid jh@hadoop04:/opt/module/zookeeper-3.4.10/conf/
并分别在hadoop103、hadoop104上修改myid文件中内容为3、4
重命名/opt/module/zookeeper-3.4.10/conf 这个目录下的zoo_sample.cfg 为zoo.cfg
[jh@hadoop102 conf]$ mv zoo_sample.cfg zoo.cfg
编辑zoo.cfg文件
[jh@hadoop102 conf]$ vim zoo.cfg
修改数据存储路径配置
dataDir=/opt/module/zookeeper-3.4.10/zkData
增加如下配置
#######################cluster##########################
server.2=hadoop102:2888:3888
server.3=hadoop103:2888:3888
server.4=hadoop104:2888:3888
同步zoo.cfg配置文件
[jh@hadoop102 conf]$ rsync -av zoo.cfg jh@hadoop04:/opt/module/zookeeper-3.4.10/conf/
配置参数解读
server.A=B:C:D
例如:server.2=hadoop102:2888:3888
A是一个数字,表示这个是第几号服务器;
集群模式下配置一个文件myid,这个文件在dataDir目录下,这个文件里面有一个数据就是A的值,zookeepe
启动时读取此文件,拿到里面的数据与zoo.cfg里面的配置信息比较从而判断到底是哪个server。
B是这个服务器的地址;
C是这个服务器Follower与集群中的Leader服务器交换信息的端口;
D是万一集群中的Leader服务器挂了,需要一个端口来重新进行选举,选出一个新的Leader,
而这个端口就是用来执行选举时服务器相互通信的端口。
分别启动Zookeeper
[jh@hadoop102 zookeeper-3.4.10]$ bin/zkServer.sh start
[jh@hadoop103 zookeeper-3.4.10]$ bin/zkServer.sh start
[jh@hadoop104 zookeeper-3.4.10]$ bin/zkServer.sh start
查看状态
[jh@hadoop102 zookeeper-3.4.10]# bin/zkServer.sh status
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/…/conf/zoo.cfg
Mode: follower
[jh@hadoop103 zookeeper-3.4.10]# bin/zkServer.sh status
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/…/conf/zoo.cfg
Mode: leader
[jh@hadoop104 zookeeper-3.4.5]# bin/zkServer.sh status
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/…/conf/zoo.cfg
Mode: follower
[jh@hadoop103 zookeeper-3.4.10]$ bin/zkCli.sh
[zk: localhost:2181(CONNECTED) 1] help
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] ls2 /
[zookeeper]
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1
[zk: localhost:2181(CONNECTED) 3] create /name "zhangsan"
Created /name
[zk: localhost:2181(CONNECTED) 4] create /name/girl “xiaohong”
Created /name/girl
[zk: localhost:2181(CONNECTED) 5] get /name
zhangsan
cZxid = 0x100000003
ctime = Wed Aug 29 00:03:23 CST 2018
mZxid = 0x100000003
mtime = Wed Aug 29 00:03:23 CST 2018
pZxid = 0x100000004
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 1
[zk: localhost:2181(CONNECTED) 6] get /name/girl
xiaohong
cZxid = 0x100000004
ctime = Wed Aug 29 00:04:35 CST 2018
mZxid = 0x100000004
mtime = Wed Aug 29 00:04:35 CST 2018
pZxid = 0x100000004
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 0
create [-s] [e] path data acl
其中,-s或-e分别指定节点特性,顺序或临时节点,若不指定,则表示持久节点;acl用来进行权限控制。
[zk: localhost:2181(CONNECTED) 7] create -e /name/boy “lisi”
Created /name/boy
在当前客户端是能查看到的
[zk: localhost:2181(CONNECTED) 3] ls /name
[boy, girl]
退出当前客户端然后再重启客户端
[zk: localhost:2181(CONNECTED) 12] quit
[jinghang@hadoop104 zookeeper-3.4.10]$ bin/zkCli.sh
再次查看根目录下短暂节点已经删除
[zk: localhost:2181(CONNECTED) 0] ls /name
[girl]
先创建一个普通的根节点/name/boy
[zk: localhost:2181(CONNECTED) 1] create /name/boy "lisi"
Created /sanguo/weiguo
创建带序号的节点
[zk: localhost:2181(CONNECTED) 2] create -s /name/boy/small “wangwu”
Created /name/boy/small0000000000
[zk: localhost:2181(CONNECTED) 3] create -s /name/boy/big "wangwu"
Created /name/boy/big0000000001
[zk: localhost:2181(CONNECTED) 4] create -s /name/boy/old "wangwu"
Created /name/boy/old0000000002
如果原来没有序号节点,序号从0开始依次递增。如果原节点下已有2个节点,则再排序时从2开始,以此类推。
创建临时有序节点
[zk: localhost:2181(CONNECTED) 4] create -s -e /name/boy/old “wangwu”
Created /name/boy/wangwu0000000003
[zk: localhost:2181(CONNECTED) 6] set /name/boy "zhaoliu"
在hadoop104主机上注册监听/name 节点数据变化
[zk: localhost:2181(CONNECTED) 26] [zk: localhost:2181(CONNECTED) 8] get /name watch
在hadoop103主机上修改/name 节点的数据
[zk: localhost:2181(CONNECTED) 1] set /name “zhangsan1”
观察hadoop104主机收到数据变化的监听
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/name
在hadoop104主机上注册监听/name 节点的子节点变化
[zk: localhost:2181(CONNECTED) 1] ls /name watch
[aa0000000001, server101]
在hadoop103主机/name 节点上创建子节点
[zk: localhost:2181(CONNECTED) 2] create /name/weizhi “ccccc”
Created /name/weizhi
观察hadoop104主机收到子节点变化的监听
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/name
[zk: localhost:2181(CONNECTED) 4] delete /name/weizhi
[zk: localhost:2181(CONNECTED) 15] rmr /name/boy
[zk: localhost:2181(CONNECTED) 17] stat /name
cZxid = 0x100000003
ctime = Wed Aug 29 00:03:23 CST 2018
mZxid = 0x100000011
mtime = Wed Aug 29 00:21:23 CST 2018
pZxid = 0x100000014
cversion = 9
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 1
setquota -n | -b val path
n:表示子节点的最大个数
b:表示数据值的最大长度
val:子节点最大个数以数据值的最大长度
path:节点路径
[zk: localhost:2181(CONNECTED) 9] setquota -n 4 /school
Comment: the parts are option -n val 4 path /school
listquota path
[zk: localhost:2181(CONNECTED) 10] listquota /school
absolute path is /zookeeper/quota/school/zookeeper_limits
Output quota for /school count=4,bytes=-1
Output stat for /school count=3,bytes=20
子节点个数为4.数据长度-1表示没限制
delquota [-n|-b] path
[zk: localhost:2181(CONNECTED) 0] delquota -n /school
[zk: localhost:2181(CONNECTED) 1] listquota /school
absolute path is /zookeeper/quota/school/zookeeper_limits
Output quota for /school count=-1,bytes=-1
Output stat for /school count=6,bytes=29
[zk: localhost:2181(CONNECTED) 2] history
0 - delquota -n /school
1 - listquota /school
2 - history
[zk: localhost:2181(CONNECTED) 4] history
0 - delquota -n /school
1 - listquota /school
2 - history
3 - help
4 - history