ZooKeeper集群的一些基本概念
zookeeper集群搭建:
- zk集群,主从节点,心跳机制(选举模式)
- 配置数据文件 myid 1/2/3 对应 server.1/2/3
- 通过 zkCli.sh -server [ip]:[port] 命令检测集群是否配置成功
和其他大多数集群结构一样,zookeeper集群也是主从结构。搭建集群时,机器数量最低也是三台,因为小于三台就无法进行选举。选举就是当集群中的master节点挂掉之后,剩余的两台机器会进行选举,在这两台机器中选举出一台来做master节点。而当原本挂掉的master恢复正常后,也会重新加入集群当中。但是不会再作为master节点,而是作为slave节点。如下:
单机伪分布式搭建zookeeper集群
本节介绍单机伪分布式的zookeeper安装,官方下载地址如下:
https://archive.apache.org/dist/zookeeper/
我这里使用的是3.4.11版本,所以找到相应的版本点击进去,复制到.tar.gz的下载链接到Linux上进行下载。操作命令如下:
[[email protected] ~]# cd /usr/local/src/ [[email protected] /usr/local/src]# wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11.tar.gz
下载完成之后将其解压到/usr/local/目录下:
[[email protected] /usr/local/src]# tar -zxvf zookeeper-3.4.11.tar.gz -C /usr/local/ [[email protected] /usr/local/src]# cd ../zookeeper-3.4.11/ [[email protected] /usr/local/zookeeper-3.4.11]# ls bin dist-maven lib README_packaging.txt zookeeper-3.4.11.jar.asc build.xml docs LICENSE.txt recipes zookeeper-3.4.11.jar.md5 conf ivysettings.xml NOTICE.txt src zookeeper-3.4.11.jar.sha1 contrib ivy.xml README.md zookeeper-3.4.11.jar [[email protected] /usr/local/zookeeper-3.4.11]#
然后给目录重命名一下:
[[email protected] ~]# cd /usr/local/ [[email protected] /usr/local]# mv zookeeper-3.4.11/ zookeeper00
接着进行一系列的配置:
[[email protected] /usr/local]# cd zookeeper00/ [[email protected] /usr/local/zookeeper00]# cd conf/ [[email protected] /usr/local/zookeeper00/conf]# cp zoo_sample.cfg zoo.cfg # 拷贝官方提供的模板配置文件 [[email protected] /usr/local/zookeeper00/conf]# vim zoo.cfg # 增加或修改成如下内容 tickTime=2000 initLimit=10 syncLimit=5 dataDir=/usr/local/zookeeper00/dataDir dataLogDir=/usr/local/zookeeper00/dataLogDir clientPort=2181 4lw.commands.whitelist=* server.1=192.168.190.129:2888:3888 # master节点,ip后面跟的是集群通信的端口 server.2=192.168.190.129:2889:3889 server.3=192.168.190.129:2890:3890 [[email protected] /usr/local/zookeeper00/conf]# cd ../ [[email protected] /usr/local/zookeeper00]# mkdir {dataDir,dataLogDir} [[email protected] /usr/local/zookeeper00]# cd dataDir/ [[email protected] /usr/local/zookeeper00/dataDir]# vim myid # 配置该节点的id 1 [[email protected] /usr/local/zookeeper00/dataDir]#
配置完之后,拷贝多个目录出来,因为是单机的伪分布式所以需要在一台机器上安装多个zookeeper:
[[email protected] /usr/local]# cp zookeeper00 zookeeper01 -rf [[email protected] /usr/local]# cp zookeeper00 zookeeper02 -rf
配置 zookeeper01:
[[email protected] /usr/local]# cd zookeeper01/conf/ [[email protected] /usr/local/zookeeper01/conf]# vim zoo.cfg # 修改内容如下 tickTime=2000 initLimit=10 syncLimit=5 dataDir=/usr/local/zookeeper01/dataDir dataLogDir=/usr/local/zookeeper01/dataLogDir clientPort=2182 # 端口号必须要修改 4lw.commands.whitelist=* server.1=192.168.190.129:2888:3888 server.2=192.168.190.129:2889:3889 server.3=192.168.190.129:2890:3890 [[email protected] /usr/local/zookeeper01/conf]# cd ../dataDir/ [[email protected] /usr/local/zookeeper01/dataDir]# vim myid 2 [[email protected] /usr/local/zookeeper01/dataDir]#
配置 zookeeper02:
[[email protected] /usr/local]# cd zookeeper02/conf/ [[email protected] /usr/local/zookeeper02/conf]# vim zoo.cfg tickTime=2000 initLimit=10 syncLimit=5 dataDir=/usr/local/zookeeper02/dataDir dataLogDir=/usr/local/zookeeper02/dataLogDir clientPort=2183 # 端口号必须要修改 4lw.commands.whitelist=* server.1=192.168.190.129:2888:3888 server.2=192.168.190.129:2889:3889 server.3=192.168.190.129:2890:3890 [[email protected] /usr/local/zookeeper02/conf]# cd ../dataDir/ [[email protected] /usr/local/zookeeper02/dataDir]# vim myid 3 [[email protected] /usr/local/zookeeper02/dataDir]#
以上就在单机上配置好三个zookeeper集群节点了,现在我们来测试一下,这个伪分布式的zookeeper集群能否正常运作起来:
[[email protected] ~]# cd /usr/local/zookeeper00/bin/ [[email protected] /usr/local/zookeeper00/bin]# ./zkServer.sh start # 启动第一个节点 ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper00/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [[email protected] /usr/local/zookeeper00/bin]# netstat -lntp |grep java # 查看监听的端口 tcp6 0 0 192.168.190.129:3888 :::* LISTEN 3191/java # 集群通信的端口 tcp6 0 0 :::44793 :::* LISTEN 3191/java tcp6 0 0 :::2181 :::* LISTEN 3191/java [[email protected] /usr/local/zookeeper00/bin]# cd ../../zookeeper01/bin/ [[email protected] /usr/local/zookeeper01/bin]# ./zkServer.sh start ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper01/bin/../conf/zoo.cfg # 启动第二个节点 Starting zookeeper ... STARTED [roo[email protected] /usr/local/zookeeper01/bin]# cd ../../zookeeper02/bin/ [[email protected] /usr/local/zookeeper02/bin]# ./zkServer.sh start # 启动第三个节点 [[email protected] /usr/local/zookeeper02/bin]# netstat -lntp |grep java # 查看监听的端口 tcp6 0 0 192.168.190.129:2889 :::* LISTEN 3232/java tcp6 0 0 :::48463 :::* LISTEN 3232/java tcp6 0 0 192.168.190.129:3888 :::* LISTEN 3191/java tcp6 0 0 192.168.190.129:3889 :::* LISTEN 3232/java tcp6 0 0 192.168.190.129:3890 :::* LISTEN 3286/java tcp6 0 0 :::44793 :::* LISTEN 3191/java tcp6 0 0 :::60356 :::* LISTEN 3286/java tcp6 0 0 :::2181 :::* LISTEN 3191/java tcp6 0 0 :::2182 :::* LISTEN 3232/java tcp6 0 0 :::2183 :::* LISTEN 3286/java [[email protected] /usr/local/zookeeper02/bin]# jps # 查看进程 3232 QuorumPeerMain 3286 QuorumPeerMain 3191 QuorumPeerMain 3497 Jps [[email protected] /usr/local/zookeeper02/bin]#
如上,可以看到,三个节点都正常启动成功了,接下来我们进入客户端,创建一些znode,看看是否会同步到集群中的其他节点上去:
[[email protected] /usr/local/zookeeper02/bin]# ./zkCli.sh -server localhost:2181 # 登录第一个节点的客户端 [zk: localhost:2181(CONNECTED) 0] ls / [zookeeper] [zk: localhost:2181(CONNECTED) 1] create /data test-data Created /data [zk: localhost:2181(CONNECTED) 2] ls / [zookeeper, data] [zk: localhost:2181(CONNECTED) 3] quit [[email protected] /usr/local/zookeeper02/bin]# ./zkCli.sh -server localhost:2182 # 登录第二个节点的客户端 [zk: localhost:2182(CONNECTED) 0] ls / # 可以查看到我们在第一个节点上创建的znode,代表集群中的节点能够正常同步数据 [zookeeper, data] [zk: localhost:2182(CONNECTED) 1] get /data # 数据也是一致的 test-data cZxid = 0x100000002 ctime = Tue Apr 24 18:35:56 CST 2018 mZxid = 0x100000002 mtime = Tue Apr 24 18:35:56 CST 2018 pZxid = 0x100000002 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 9 numChildren = 0 [zk: localhost:2182(CONNECTED) 2] quit [[email protected] /usr/local/zookeeper02/bin]# ./zkCli.sh -server localhost:2183 # 登录第三个节点的客户端 [zk: localhost:2183(CONNECTED) 0] ls / # 第三个节点也能查看到我们在第一个节点上创建的znode [zookeeper, data] [zk: localhost:2183(CONNECTED) 1] get /data # 数据也是一致的 test-data cZxid = 0x100000002 ctime = Tue Apr 24 18:35:56 CST 2018 mZxid = 0x100000002 mtime = Tue Apr 24 18:35:56 CST 2018 pZxid = 0x100000002 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 9 numChildren = 0 [zk: localhost:2183(CONNECTED) 2] quit [[email protected]1 /usr/local/zookeeper02/bin]#
查看集群的状态、主从信息需要使用
./zkServer.sh status
命令,但是多个节点的话,逐个查看有些费劲,所以我们写一个简单的shell脚本来批量执行命令。如下:[[email protected] ~]# vim checked.sh # 脚本内容如下 #!/bin/bash /usr/local/zookeeper00/bin/zkServer.sh status /usr/local/zookeeper01/bin/zkServer.sh status /usr/local/zookeeper02/bin/zkServer.sh status [[email protected] ~]# sh ./checked.sh # 执行脚本 ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper00/bin/../conf/zoo.cfg Mode: follower # 从节点 ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper01/bin/../conf/zoo.cfg Mode: leader # 主节点 ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper02/bin/../conf/zoo.cfg Mode: follower [[email protected] ~]#
到此为止,我们就成功完成了单机zookeeper伪分布式集群的搭建,并且也测试成功了。
原文地址:https://www.cnblogs.com/syf-com/p/9058267.html