ZooKeeper搭建和使用笔记
写在前面
ZooKeeper是整个大数据体系结构中十分重要的一环,这个组件的存在主要是作为一个控制中心,对整个大数据体系结构进行统一的管理。这篇笔记主要记录了如何搭建一个ZooKeeper集群,以及基本的Shell操作命令,因为Java API的操作十分简单,估计也不会很常用,就不再记录了。
搭建ZooKeeper集群
准备工作
首先要准备三台虚拟机,我们之前搭建的hadoop集群正好就可以使用,hadoop集群的搭建教程博客可以在这里了解,我们先把zookeeper的安装包拷贝到/opt/software下。(ZooKeeper的运行也是基于JDK的,但我们的集群早就配置好了JDK,这里就不再赘述了。)
开始安装
首先把ZooKeeper的安装包进行解压:
[liuge@hadoop03 software]$ tar -zxvf zookeeper-3.4.10.tar.gz -C /opt/module/
打开/opt/module/zookeeper-3.4.10/conf目录,将zoo_sample.cfg改名为zoo.cfg:
[liuge@hadoop03 conf]$ mv zoo_sample.cfg zoo.cfg
打开zoo.cfg文件,修改dataDir路径:
[liuge@hadoop03 zookeeper-3.4.10]$ vim zoo.cfg
dataDir=/opt/module/zookeeper-3.4.10/zkData
之后在/opt/module/zookeeper-3.4.10/下创建zkData目录:
[liuge@hadoop03 zookeeper-3.4.10]$ mkdir zkData
创建好之后,我们回到module目录下,把zookeeper文件夹分发到其他结点:
[liuge@hadoop03 module]$ xsync zookeeper-3.4.10/
然后在/opt/module/zookeeper-3.4.10/zkData目录下创建一个叫myid的文件,并在里面写上一个编号:
[liuge@hadoop03 zkData]$ touch myid
[liuge@hadoop03 zkData]$ vim myid
3
这里面的ID随便设置,只要是唯一的且你自己记得住就行。
之后分发这个文件:
[liuge@hadoop03 zkData]$ xsync myid
并在hadoop04,hadoop05上修改myid文件中内容分别为4和5
之后,再次编辑conf下的zoo.cfg文件,添加如下内容:
[liuge@hadoop03 conf]$ vim zoo.cfg
#######################cluster##########################
server.3=hadoop03:2888:3888
server.4=hadoop04:2888:3888
server.5=hadoop05:2888:3888
然后把这个文件再分发一次:
[liuge@hadoop03 zkData]$ xsync zoo.cfg
这里要注意,这个配置参数的意思为:
server.A=B:C:D,其中A是一个数字,代表这是第几号服务器,也就是我们在myid里配置的那个数字。B是这个服务器的ip地址,这里我配置了主机名与IP地址的映射,相当于ip地址。C是服务器与集群中的Leader服务器交换信息的端口,D是执行选举时服务器相互通信的端口。
启动集群
配置完成后,我们可以开始启动了。每个虚拟机挨个启动:
[liuge@hadoop03 zookeeper-3.4.10]$ bin/zkServer.sh start
[liuge@hadoop03 zookeeper-3.4.10]$ bin/zkServer.sh start
[liuge@hadoop03 zookeeper-3.4.10]$ bin/zkServer.sh start
shell操作ZooKeeper集群
-
启动客户端:
[liuge@hadoop04 zookeeper-3.4.10]$ bin/zkCli.sh
-
显示所有操作:
[zk: localhost:2181(CONNECTED) 1] help
-
显示当前znode中所包含的内容:
[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 /sanguo "jinlian" Created /sanguo [zk: localhost:2181(CONNECTED) 4] create /sanguo/shuguo "liubei" Created /sanguo/shuguo
-
获得节点的值:
[zk: localhost:2181(CONNECTED) 5] get /sanguo jinlian 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 = 7 numChildren = 1 [zk: localhost:2181(CONNECTED) 6] [zk: localhost:2181(CONNECTED) 6] get /sanguo/shuguo liubei 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 = 6 numChildren = 0
-
创建短暂节点:
[zk: localhost:2181(CONNECTED) 7] create -e /sanguo/wuguo "zhouyu" Created /sanguo/wuguo
-
创建带序号的节点:
[zk: localhost:2181(CONNECTED) 2] create -s /sanguo/weiguo/xiaoqiao "jinlian" Created /sanguo/weiguo/xiaoqiao0000000000 [zk: localhost:2181(CONNECTED) 3] create -s /sanguo/weiguo/daqiao "jinlian" Created /sanguo/weiguo/daqiao0000000001 [zk: localhost:2181(CONNECTED) 4] create -s /sanguo/weiguo/diaocan "jinlian" Created /sanguo/weiguo/diaocan0000000002
-
修改节点的数据值
[zk: localhost:2181(CONNECTED) 6] set /sanguo/weiguo "simayi"
-
监听节点的值的变化(只能监听一次)
[zk: localhost:2181(CONNECTED) 26] [zk: localhost:2181(CONNECTED) 8] get /sanguo watch
-
监听节点的子节点变化(只能监听一次)
[zk: localhost:2181(CONNECTED) 1] ls /sanguo watch [aa0000000001, server101]
-
删除节点
[zk: localhost:2181(CONNECTED) 4] delete /sanguo/jin
-
递归删除节点:
[zk: localhost:2181(CONNECTED) 15] rmr /sanguo/shuguo
-
查看节点状态:
[zk: localhost:2181(CONNECTED) 17] stat /sanguo 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
总结
总的来说,比起hadoop,ZooKeeper的搭建和配置都十分简单容易,且使用起来也十分简单。经过了配置Hadoop的摧残,这种程度的配置我觉得已经难不倒我们了。