本文提供一个快捷的、小化的安装、配置与部署。

下载

从官网选择一个binary版本进行下载
https://zookeeper.apache.org/releases.html

安装

将下载的tar包解压至节点的指定目录即可

  1. tar zxvf zookeeper-3.4.14.tar.gz -C /opt

配置

ZooKeeper home目录的conf下有一个配置样例文件 zoo_sample.cfg。ZooKeeper 会以zoo.cfg文件的配置信息启动进程,我们可以使用这个样例文件创建配置文件 zoo.cfg

  1. cd /opt/zookeeper-3.4.14/
  2. cp conf/zoo_sample.cfg conf/zoo.cfg
  3. vim zoo.cfg

这里主要调整了两个地方,一是 dataDir 目录设置到了一个非tmp的目录下,生产环境可以将其放至数据盘所挂载的目录下。这里同时也指定了日志的目录。二是添加了server的信息,server.*=hostname:2888:3888,也可以使用ip地址。

  1. # The number of milliseconds of each tick
  2. tickTime=2000
  3. # The number of ticks that the initial
  4. # synchronization phase can take
  5. initLimit=10
  6. # The number of ticks that can pass between
  7. # sending a request and getting an acknowledgement
  8. syncLimit=5
  9. # the directory where the snapshot is stored.
  10. # do not use /tmp for storage, /tmp here is just
  11. # example sakes.
  12. dataDir=/opt/dfs/zk/data
  13. dataLogDir=/opt/dfs/zk/log
  14. # the port at which the clients will connect
  15. clientPort=2181
  16. # the maximum number of client connections.
  17. # increase this if you need to handle more clients
  18. #maxClientCnxns=60
  19. #
  20. # Be sure to read the maintenance section of the
  21. # administrator guide before turning on autopurge.
  22. #
  23. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
  24. #
  25. # The number of snapshots to retain in dataDir
  26. #autopurge.snapRetainCount=3
  27. # Purge task interval in hours
  28. # Set to "0" to disable auto purge feature
  29. #autopurge.purgeInterval=1
  30. server.0=node1:2888:3888
  31. server.1=node2:2888:3888
  32. server.2=node3:2888:3888

将配置好的ZooKeeper分发至每一台节点,如果已完成解压到所有节点则分发 zoo.cfg文件至每一个节点。
保证每一个节点有ZooKeeper存放数据的目录

  1. mkdir -p /opt/zk/data
  2. mkdir -p /opt/zk/log

在data目录创建myid文件,写入节点对应的id。这个id就是配置文件中 server.{id}={hostname}:2888:3888 {id}所对应的数字。

  1. touch myid
  2. echo "0" > myid

启动

每个节点home目录下启动ZooKeeper

  1. bin/zkServer.sh start

Reference

https://zookeeper.apache.org/doc/r3.4.14/zookeeperStarted.html