文章目录
一、单机环境搭建
1.1 下载
下载对应版本 Zookeeper,这里我下载的版本 3.4.14
。官方下载地址:
# https://www.apache.org/dyn/closer.lua/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
#或者
wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
1.2 解压
# tar -zxvf zookeeper-3.4.14.tar.gz -C /home/hadoop/
1.3 配置环境变量
# vim /etc/profile
添加环境变量:
export ZOOKEEPER_HOME=/home/hadoop/zookeeper-3.4.14
export PATH=$ZOOKEEPER_HOME/bin:$PATH
使得配置的环境变量生效:
# source /etc/profile
1.4 修改配置
进入安装目录的 conf/
目录下,拷贝配置样本并进行修改:
# cp zoo_sample.cfg zoo.cfg
指定数据存储目录和日志文件目录(目录不用预先创建,程序会自动创建),修改后完整配置如下:
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/data/zookeeper/data
dataLogDir=/data/zookeeper/log
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
配置参数说明:
- tickTime:用于计算的基础时间单元。比如 session 超时:N*tickTime;
- initLimit:用于集群,允许从节点连接并同步到 master 节点的初始化连接时间,以 tickTime 的倍数来表示;
- syncLimit:用于集群, master 主节点与从节点之间发送消息,请求和应答时间长度(心跳机制);
- dataDir:数据存储位置;
- dataLogDir:日志目录;
- clientPort:用于客户端连接的端口,默认 2181
1.5 启动
由于已经配置过环境变量,直接使用下面命令启动即可:
zkServer.sh start
1.6 验证
使用 JPS 验证进程是否已经启动,出现 QuorumPeerMain
则代表启动成功。
[hadoop@hadoop-nn-01 conf]$ jps -m
37193 QuorumPeerMain /home/hadoop/zookeeper-3.4.14/bin/../conf/zoo.cfg
37210 Jps -m
二、集群环境搭建
为保证集群高可用,Zookeeper 集群的节点数最好是奇数,最少有三个节点,所以这里演示搭建一个三个节点的集群。这里我使用三台主机进行搭建,主机名分别为 hadoop-nn-01,hadoop-nn-02,hadoop-dn-01。
2.1 修改配置
解压一份 zookeeper 安装包,修改其配置文件 zoo.cfg
,内容如下。之后使用 scp 命令将安装包分发到三台服务器上、在三台机器上都配置好环境变量:
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/data/zookeeper/