zookeeper安装

zookeeper描述


ZooKeeper是用Java编写的,运行在Java环境上,因此,在部署zk的机器上需要安装Java运行环境。为了正常运行zk,我们需要JRE1.6或者以上的版本。
对于集群模式下的ZooKeeper部署,3个ZooKeeper服务进程是建议的最小进程数量,而且不同的服务进程建议部署在不同的物理机器上面,以减少机器宕机带来的风险,以实现ZooKeeper集群的高可用。
ZooKeeper对于机器的硬件配置没有太大的要求。例如,在Yahoo!内部,ZooKeeper部署的机器其配置通常如下:双核处理器,2GB内存,80GB硬盘


一、下载包

地址: 从https://zookeeper.apache.org/releases.html 下载ZooKeeper

二、配置文件说明及修改

ZooKeeper软件的文件和目录说明

  • bin目录
    zk的可执行脚本目录,包括zk服务进程,zk客户端,等脚本。其中,.sh是Linux环境下的脚本,.cmd是Windows环境下的脚本。
  • conf目录
    配置文件目录。zoo_sample.cfg为样例配置文件,需要修改为自己的名称,一般为zoo.cfg。log4j.properties为日志配置文件。
  • lib
    zk依赖的包。
  • contrib目录
    一些用于操作zk的工具包。
  • recipes目录
    zk某些用法的代码示例

集群模式

集群说明:至少需要3个集群节点,可在单机部署多个zookeeper,但是端口必须不同。生产集群模式无非就是实例IP地址不同,搭建方法无异同

步骤1:部署java环境

此步步骤省略

步骤2:下载并解压zookeeper安装包

cd /home/data/
wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/apache-zookeeper-3.5.6-bin.tar.gz
tar -zxvf apache-zookeeper-3.5.6-bin.tar.gz zookeeper

步骤3:复制配置示例,并修改配置文件

cd conf
cp conf/zoo_sample.cfg conf/zoo-1.cfg

配置说明

# The number of milliseconds of each tick
#  Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
# Leader 的 Follower 服务器初始化连接时最长能忍受多少个心跳时间间隔数
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
# 这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 5*2000=10秒

syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.

#数据和日志目录
dataDir=/home/data/zookeeper/dataDir
dataLogDir=/home/data/zookeeper/dataLogDir
# the port at which the clients will connect

#监听端口号,如果有多个zookeeper实例部署在同一台机器上,这个端口必须唯一
clientPort=2182

#admin端口,如果有多个zookeeper实例部署在同一台机器上,这个端口必须唯一

admin.serverPort=8081

#zookeeper cluster

#集群配置 

server.1=192.168.186.101:2889:3889

server.2=192.168.137.78:2889:3889

server.3=192.168.137.79:2889:3889

#server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号。

# 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

步骤4:分别于上台集群服务器创建标识serverID和对应目录

mkdir -p /home/data/zookeeper/{dataDir,dataLogDir}
#下面的数字对应上一步server.*
touch 1 > /home/data/zookeeper/dataDir/myid

步骤5:分别启动zookeeper

cd bin
./zkServer.sh start

步骤6:检查集群

# 其中一台选举成leader
$./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/data/zookeeper/bin/../conf/zoo.cfg
Client port found: 2182. Client address: localhost.
Mode: leader

# 另外两台被选举成follower
$./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/data/zookeeper/bin/../conf/zoo.cfg
Client port found: 2182. Client address: localhost.
Mode: follower

原文地址:https://www.cnblogs.com/cnhope/p/12059463.html