Zookeeper单机、集群安装教程

JAVA herman 751浏览 0评论

ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是HadoopHbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
ZooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户。
ZooKeeper包含一个简单的原语集,提供Java和C的接口。
ZooKeeper代码版本中,提供了分布式独享锁、选举、队列的接口,代码在zookeeper-3.4.3\src\recipes。其中分布锁和队列有Java和C两个版本,选举只有Java版本。

今天为大家分享一下,如何安装单机的Zookeeper和如何搭建Zookeeper集群?

准备安装软件

  • Ubuntu 14.04.4 64位
  • Oracle JDK 1.8.0_112
  • Zookeeper 3.4.9

单机安装教程

Step 1:环境准备,准备一台ubuntu系统的服务器,ip地址为:192.168.10.100,并且已经安装并配置好Oracle JDK 1.8.0_112。

Step 2:去Zookeeper官网下载最新的稳定版本,当前最新的稳定版本是Release 3.4.9。

Step 3:解压缩zookeeper-3.4.9.tar.gz到/opt/下(可以安装到其他目录,我这里安装到/opt/下),进入/conf/目录,创建zoo.cfg:cp zoo_sample.cfg zoo.cfg。

Step 4:修改zoo.cfg配置文件:vim zoo.cfg,内容如下:

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=5
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=2
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=/opt/zookeeper_data
# 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

上面配置修改了默认的dataDir到/opt/zookeeper_data。

Step 5:进入/bin/目录下,启动zookeeper:./zkServer.sh start。

Step 6:连接zookeeper: ./zkCli.sh -server 127.0.0.1:2181,如果成功连接,则zookeeper单机配置并启动成功。

集群安装教程

准备3台机器,IP分别为:192.168.10.100、192.168.10.101、192.168.10.102,系统均为Ubuntu 14.04.4 64位,并安装配置好Oracle JDK 1.8.0_112。

Step 1:参考前文的单机安装步骤,分别下载Zookeeper3.4.9并解压缩到/opt/下,并创建好/conf/zoo.cfg文件。

Step 2:三台机器的/conf/zoo.cfg都修改为如下内容:

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=5
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=2
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=/opt/zookeeper_data
# 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=192.168.10.100:2888:3888
server.2=192.168.10.101:2888:3888
server.3=192.168.10.102:2888:3888

Step 3:分别在/opt/zookeeper_data(根据自己的dataDir确定)下创建myid:

192.168.10.100:echo 1 > myid

192.168.10.101:echo 2 > myid

192.168.10.102:echo 3 > myid

Step 4:分别启动各个机器下的Zookeeper:./bin/zkServer.sh start

Step 5:查看状态:./zkServer.sh status