一、安装准备
下载地址:https://www.apache.org/dyn/closer.cgi/zookeeper/
官方文档:https://zookeeper.apache.org/doc/r3.4.5/
二、解压安装
解压文件
代码语言:javascript复制cd /usr/local/hadoop
tar zxpf zookeeper-3.4.5.tar.gz
创建软连接
代码语言:javascript复制ln -s zookeeper-3.4.5 zookeeper
三、配置zoo.cfg
代码语言:javascript复制cd /usr/local/hadoop/zookeeper/conf
vim /usr/local/hadoop/zookeeper/conf/zoo.cfg
给zoo.cfg文件添加一下内容
代码语言:javascript复制# 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=/usr/local/hadoop/zookeeper/data
dataLogDir=/usr/local/hadoop/zookeeper/logs
# the port at which the clients will connect
clientPort=2181
#
# 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
server.1=hadoop001:2888:3888
server.2=hadoop002:2888:3888
server.3=hadoop003:2888:3888
配置文件说明
tickTime:心跳时间,默认2000毫秒即2秒 initLimit:初始化时follower和leader之间的最多心跳数 syncLimit:follower和leader之间请求响应的最多心跳数 dataDir:存放myid、版本信息和数据持久化的目录 dataLogDir:zookeeper存放日志的目录 clientPort:zookeeper监听的端口,默认2181 server.N=ip:2888:3888:server节点配置,N表示第N台zk的节点
四、配置myid
zookeeper根据配置文件自动创建dataDir、dataLogDir目录,需要自己手动创建
代码语言:javascript复制mkdir /usr/local/hadoop/zookeeper/data
mkdir /usr/local/hadoop/zookeeper/logs
给每台zk节点配置myid,每个节点myid的值和zoo.cfg中的server.N一致
代码语言:javascript复制echo "1" > /usr/local/hadoop/zookeeper/data/myid
代码语言:javascript复制echo "2" > /usr/local/hadoop/zookeeper/data/myid
代码语言:javascript复制echo "3" > /usr/local/hadoop/zookeeper/data/myid
代码语言:javascript复制cat /usr/local/hadoop/zookeeper/data/myid
zookeeper的选举机制和myid密切关联,myid值大的相对更容易成为leader,具体这里就不详细讨论了。
如果不配置myid,会在 zookeeper.out 文件中抛出错误
[myid:] - ERROR [main:QuorumPeerMain@85] - Invalid config, exiting abnormally org.apache.zookeeper.server.quorum.QuorumPeerConfig$ConfigException: Error processing /usr/local/hadoop/zookeeper/bin/../conf/zoo.cfg
五、配置环境变量
编辑 /etc/profile 文件
代码语言:javascript复制vim /etc/profile
添加一下内容
代码语言:javascript复制export ZOO_HOME=/usr/local/hadoop/zookeeper
export PATH=$PATH:$ZOO_HOME/bin
六、启动zookeeper
代码语言:javascript复制zkServer.sh start
代码语言:javascript复制zkServer.sh status
代码语言:javascript复制jps
这里第二个节点选举成了leader