Zookeeper是一个开源的分布式协调服务,在Hadoop和各种分布式系统中应用广泛。
本文介绍在Ubuntu系统上搭建一个三节点的Zookeeper集群。主要步骤包括:
java -version
命令查看。192.168.1.100 hadoop100
192.168.1.200 hadoop200
192.168.1.201 hadoop201
tar -zxf /home/c914/Desktop/zookeeper-3.4.12.tar.gz -C /usr/local
这样就会在/usr/local目录下生成一个zookeeper-3.4.12的文件夹,为便于配置,文件名改为zookeeper。
export ZOOKEEPER_HOME=/usr/local/zookeeper
export PATH=$PATH:$ZOOKEEPER_HOME/bin
然后执行source /etc/profile
命令使配置生效。
cd $ZOOKEEPER_HOME/conf
cp zoo_sample.cfg zoo.cfg
vim zoo.cfg
zoo.cfg文件的主要配置项及其含义如下:
配置项 | 含义 |
---|---|
tickTime | Zookeeper使用的基本时间单位,以毫秒为单位 |
initLimit | 集群中的follower和leader之间初始连接时能容忍的最多心跳数(tickTime的倍数) |
syncLimit | 集群中的follower和leader之间请求和应答之间能容忍的最多心跳数(tickTime的倍数) |
dataDir | Zookeeper保存数据的目录,即myid文件所在的目录 |
clientPort | Zookeeper监听客户端连接的端口,默认为2181 |
server.x | 集群中每个节点的信息,x是节点编号,格式为host:port1:port2,host是节点主机名或IP地址,port1是节点与集群中leader交换信息的端口,port2是选举时节点互相通信的端口 |
根据上述配置项,我们可以将zoo.cfg文件修改为如下内容:
# The number of milliseconds of each tick
tickTime=20000
# 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/zookeeper/data
datalogDir=/usr/local/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
server.1=hadoop100:2888:3888
server.2=hadoop200:2888:3888
server.3=hadoop201:2888:3888
注意,这里的server.x的配置在每个节点上都是相同的,不需要根据节点编号修改
touch /usr/local/zookeeper/data/myid
注意,这个编号必须是1到255之间的整数,并且不能重复。
cd /usr/local/zookeeper/bin
zkServer.sh start
cd /usr/local/zookeeper/bin
zkServer.sh status
如果输出中显示Mode为leader或follower,说明该节点已经加入到集群中。如果输出中显示Mode为standalone,说明该节点还没有加入到集群中,可能是由于网络或配置问题导致的。
cd /usr/local/zookeeper/bin
./zkServer.sh stop