ZooKeeper的伪分布式集群搭建

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

时间: 2024-10-10 05:26:09

ZooKeeper的伪分布式集群搭建的相关文章

ZooKeeper的伪分布式集群搭建以及真分布式集群搭建

zk集群的一些基本概念 zookeeper集群搭建: zk集群,主从节点,心跳机制(选举模式) 配置数据文件 myid 1/2/3 对应 server.1/2/3 通过 zkCli.sh -server [ip]:[port] 命令检测集群是否配置成功 和其他大多数集群结构一样,zookeeper集群也是主从结构.搭建集群时,机器数量最低也是三台,因为小于三台就无法进行选举.选举就是当集群中的master节点挂掉之后,剩余的两台机器会进行选举,在这两台机器中选举出一台来做master节点.而当原

Hadoop伪分布式集群搭建总结

Hadoop伪分布式集群搭建总结 一.所需软件VMware15!CentOS6.5JDK1.8Hadoop2.7.3二.安装注意:对文件进行编辑:输入a,表示对该文件进行编辑,最后保存该文件,操作为:点击键盘上的Esc按钮,然后输入英文的:字符,再输入wq,点击回车,完成文件的保存.1.关闭防火墙和禁用SELINUX(1).永久关闭防火墙,重启Linux系统(2) .禁用SELINUX:修改文件参数 重启Linux使其生效(3).检查防火墙是否运行,显示下图即为关闭2.配置hostname与IP

hadoop伪分布式集群搭建与安装(ubuntu系统)

1:Vmware虚拟软件里面安装好Ubuntu操作系统之后使用ifconfig命令查看一下ip; 2:使用Xsheel软件远程链接自己的虚拟机,方便操作.输入自己ubuntu操作系统的账号密码之后就链接成功了: 3:修改主机的名称vi /etc/hostname和域名和主机映射对应的关系 vi /etc/hosts,改过之后即生效,自己可以ping一下,我这里ip对应master,比如ping master之后发现可以ping通即可: 4:修改过主机名称和主机名与ip对应的关系之后:开始上传jd

Hadoop伪分布式集群搭建-此文章在个人51.cto转载

一.HDFS伪分布式环境搭建 Hadoop分布式文件系统(HDFS)被设计成适合运行在通用硬件(commodity hardware)上的分布式文件系统.它和现有的分布式文件系统有很多共同点.但同时,它和其他的分布式文件系统的区别也是很明显的.HDFS是一个高度容错性的系统,适合部署在廉价的机器上.HDFS能提供高吞吐量的数据访问,非常适合大规模数据集上的应用.HDFS放宽了一部分POSIX约束,来实现流式读取文件系统数据的目的.HDFS在最开始是作为Apache Nutch搜索引擎项目的基础架

Kafka 单机和伪分布式集群搭建

感觉自己越来越像个运维了,真的是所有的分布式的中间件基本都搭建了一遍   不得了了. 好 废话少说 , 还是来走Kafka 吧 , 经历了Rocket MQ , Kafka 的搭建真的是简单的不知道多少倍 , RocketMQ  实在 是太麻烦了. 还是老方案 , 先登录官网. https://archive.apache.org/dist/kafka 可以根据官网的指示文档  进行下载. 单机搭建 Step 1:下载代码 > wget https://archive.apache.org/di

Linux下redis5的安装及伪分布式集群搭建

redis5 官网下载地址:https://redis.io/download redis5-Linux安装:https://blog.csdn.net/qq_39135287/article/details/83474865 redis 集群搭建官网文档:https://redis.io/topics/cluster-tutorial redis5 集群搭建:https://blog.csdn.net/qq_39135287/article/details/84189397 redis5 集群

zookeeper伪分布式集群搭建

1.去官网下载zookeeper,我这里用的是3.4.6版本的 2.下载完成解压缩到一个目录 3.因为是伪分布式,所以我创建了3个服务器文件夹,比如server1,server2,server3.每个服务器文件夹下都创建了data,dataLog,logs目录.然后把解压完成的zookeeper拷贝到每个服务器文件夹下面. 4.在每个data目录中创建myid文件,里面写上一个数字,比如1,2, 3,server1就对应1.server2就对应2.server3对应3.这个数字不能重复.表示是服

ZooKeeper伪分布式集群安装

获取ZooKeeper安装包 下载地址:http://apache.dataguru.cn/zookeeper 选择一个稳定版本进行下载,我这里下载的是zookeeper-3.4.6版本. ZooKeeper伪分布式集群安装 伪分布式集群:在一台Server中,启动多个ZooKeeper的实例. 上传并解压安装包 cd /usr rz -by tar xf zookeeper-3.4.6.tar.gz 创建实例配置文件 cd zookeeper-3.4.6/conf cp zoo_sample.

kafka系列二:多节点分布式集群搭建

上一篇分享了单节点伪分布式集群搭建方法,本篇来分享一下多节点分布式集群搭建方法.多节点分布式集群结构如下图所示: 为了方便查阅,本篇将和上一篇一样从零开始一步一步进行集群搭建. 一.安装Jdk 具体安装步骤可参考 linux安装jdk. 二.安装与配置zookeeper 下载地址:https://www-us.apache.org/dist/zookeeper/stable/ 下载二进制压缩包 zookeeper-3.4.14.tar.gz,然后上传到linux服务器指定目录下,本次上传目录为