- Hadoop大数据技术开发实战
- 张伟洋
- 2306字
- 2025-02-23 10:46:16
6.2 ZooKeeper安装配置
本节讲解ZooKeeper三种模式的安装,分别为单机模式、伪分布模式和集群模式。
6.2.1 单机模式
单机模式是指只部署一个ZooKeeper进程,客户端直接与该ZooKeeper进程进行通信。在单机模式下配置和安装ZooKeeper相对来说比较简单且易于理解。在开发测试环境下,如果没有较多的物理资源,可以使用单机模式。但是在生产环境下不可用单机模式,因为无论是系统可靠性还是读写性能,单机模式都不能满足生产的需求。
1. 下载ZooKeeper
从Apache官网下载一个ZooKeeper的稳定版本,下载网址为:
https://zookeeper.apache.org/releases.html
本书使用的是zookeeper-3.4.10版本。
2. 安装ZooKeeper
ZooKeeper需要有Java环境才能运行,并且是Java 6以上版本,Java环境的安装此处不再赘述。
将下载的ZooKeeper安装文件zookeeper-3.4.10.tar.gz上传到操作系统的目录/opt/softwares/中,并进入该目录,将其解压到目录/opt/modules/,解压命令如下:

为了以后的操作方便,可以对ZooKeeper的环境变量进行配置,在/etc/profile文件中加入以下内容:

加入后执行source /etc/profile命令对环境变量文件进行刷新操作,使修改生效。
安装ZooKeeper服务还需要创建一个配置文件,在ZooKeeper安装目录下的conf文件夹中创建zoo.cfg文件,并向文件中添加以下内容:

上述配置属性解析如下:
- tickTime:基本事件单元,用来指示一个心跳的时长。以毫秒为单位,默认是2000。
- dataDir:ZooKeeper数据文件的存储位置。
- clientPort:ZooKeeper供客户端连接的端口,默认是2181。
配置好后,执行以下命令,启动ZooKeeper服务:

启动后如果要检查ZooKeeper服务是否已经启动,可以通过执行以下命令查看是否有 2181端口号在监听服务:

ZooKeeper服务启动后就可以启动客户端进行连接了,命令如下:

6.2.2 伪分布模式
所谓伪分布模式,就是在单台计算机上运行多个ZooKeeper实例,并组成一个集群。本节以启动三个ZooKeeper进程为例进行讲解。
1. 安装ZooKeeper
将ZooKeeper安装文件解压到相应目录下,并配置环境变量,步骤参考单机模式。
2. 建立配置文件
在安装目录的conf文件夹下分别新建三个配置文件zoo1.cfg、zoo2.cfg、zoo3.cfg。
zoo1.cfg内容如下:

zoo2.cfg内容如下:

zoo3.cfg内容如下:

上述配置属性解析可参考本章6.2.3节。
3. 建立数据和日志目录
在ZooKeeper安装目录下分别建立1.data、2.data、3.data数据目录,分别建立1.logs、2.logs、3.logs日志目录,并分别在每个数据目录下新建myid文件,对1.data目录下的myid文件写入数字1,对2.data目录下的myid文件写入数字2,对3.data目录下的myid文件写入数字3。
4. 启动服务并查看状态
分别执行以下命令启动ZooKeeper服务:

分别执行以下命令查看服务状态:

6.2.3 集群模式
由于在ZooKeeper集群中,会有一个Leader服务器负责管理和协调其他集群服务器,因此服务器的数量通常都是单数,例如3,5,7等,这样数量为2n+1的服务器就可以允许最多n台服务器的失效。
本例仍然使用三个节点(centos01、centos02、centos03)搭建部署ZooKeeper集群,搭建步骤如下。
1. 上传ZooKeeper安装文件
在centos01节点中,上传ZooKeeper安装文件zookeeper-3.4.10.tar.gz到目录/opt/softwares/中,并进入该目录,将其解压到目录/opt/modules/,解压命令如下:

2. 编写配置文件
(1)在ZooKeeper安装目录下新建文件夹dataDir,用于存放ZooKeeper相关数据。
(2)在ZooKeeper安装目录下的conf文件夹中新建配置文件zoo.cfg,加入以下内容:

上述配置属性解析如下。
- initLimit:集群中的Follower服务器初始化连接Leader服务器时能等待的最大心跳数(连接超时时长)。默认为10,即如果经过10个心跳之后Follower服务器仍然没有收到Leader服务器的返回信息,则连接失败。本例中该参数值为5,参数tickTime为2000(毫秒),则连接超时时长为5×2000=10秒(即tickTime×initLimit=10秒)。
- syncLimit:集群中的Follower服务器与Leader服务器之间发送消息以及请求/应答时所能等待的最多心跳数。本例中,最多心跳时长为2×2000=4秒。
- server.id=host:port1:port2:标识不同的ZooKeeper服务器。ZooKeeper可以从“server.id=host:port1:port2”中读取相关信息。其中,id值必须在整个集群中是唯一的,且大小在1到255之间;host是服务器的名称或IP地址;第一个端口(port1)是Leader端口,即该服务器作为Leader时供Follower连接的端口;第二个端口(port2)是选举端口,即选举Leader服务器时供其他Follower连接的端口。
- dataDir:ZooKeeper存储数据的目录。
- clientPort:客户端连接ZooKeeper服务器的端口。ZooKeeper会监听这个端口,接收客户端的请求。
(3)在配置文件zoo.cfg中的参数dataDir指定的目录下(此处为ZooKeeper安装目录下的dataDir文件夹)新建一个名为myid的文件,这个文件仅包含一行内容,即当前服务器的id值,与参数server.id中的id值相同。本例中,当前服务器(centos01)的id值为1,则应该在myid文件中写入数字1。ZooKeeper启动时会读取该文件,将其中的数据与zoo.cfg里写入的配置信息进行对比,从而获取当前服务器的身份信息。
3. 复制ZooKeeper安装信息到其他节点
centos01节点安装完成后,需要复制整个ZooKeeper安装目录到centos02和centos03节点,命令如下:

4. 修改其他节点配置
复制完成后,需要将centos02和centos03节点中的myid文件的值修改为对应的数字,即作出以下操作:
修改centos02节点中的opt/modules/zookeeper-3.4.10/dataDir/myid文件中的值为2。
修改centos03节点中的opt/modules/zookeeper-3.4.10/dataDir/myid文件中的值为3。
5. 启动ZooKeeper
分别进入每个节点的ZooKeeper安装目录,执行以下命令启动各个节点的ZooKeeper:

启动时输出以下信息代表启动成功:

注意
ZooKeeper集群的启动与Hadoop不同,其需要在每台装有ZooKeeper的服务器上都执行一次启动命令,这样才能使得整个集群启动起来。
6. 查看启动状态
分别在各个节点上执行以下命令,查看ZooKeeper服务的状态:

在centos01节点上查看服务状态,输出了以下信息:

在centos02服务器上查看服务状态,输出了以下信息:

在centos03服务器上查看服务状态,输出了以下信息:

由此可见,本例中centos03服务器上的ZooKeeper服务为Leader,其余两个ZooKeeper服务为Follower。
如果在查看启动状态时输出以下信息,说明ZooKeeper集群启动不成功,出现错误。

此时需要修改ZooKeeper安装目录下的bin/zkEvn.sh文件中的以下内容:

将上述内容中的CONSOLE修改为ROLLINGFILE,使其将错误信息输出到日志文件,修改后的内容如下:

修改完成后重新启动ZooKeeper集群,查看在ZooKeeper安装目录下生成的日志文件zookeeper.log,发现出现以下错误:

产生上述错误的原因是,系统没有关闭防火墙,导致ZooKeeper集群间连接不成功。因此需要关闭系统防火墙(为了防止出错,在最初的集群环境配置的时候可以直接将防火墙关闭),CentOS 7关闭防火墙的命令如下:

关闭各节点的防火墙后,重新启动ZooKeeper,再一次查看启动状态,发现一切正常了。
7. 测试客户端连接
在centos01节点上(其他节点也可以),进入ZooKeeper安装目录,执行以下命令,连接ZooKeeper服务器,连接成功后可以输入ZooKeeper的Shell命令进行操作与测试。
