搭建三台机器集群,三台主机分别为hadoop101、hadoop102、hadoop103 官网:https://zookeeper.apache.org/doc/current/zookeeperStarted.html

下载安装包

https://zookeeper.apache.org/releases.html

下载地址:https://www.apache.org/dyn/closer.lua/zookeeper/zookeeper-3.6.1/apache-zookeeper-3.6.1-bin.tar.gz
下载后解压到 /usr/local/apache-zookeeper-3.6.1 目录

配置Conf文件

配置路径:apache-zookeeper-3.6.1/conf/zoo.cfg

  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=/home/data/zookeeper
  13. # the port at which the clients will connect
  14. clientPort=2181
  15. # the maximum number of client connections.
  16. # increase this if you need to handle more clients
  17. #maxClientCnxns=60
  18. #
  19. # Be sure to read the maintenance section of the
  20. # administrator guide before turning on autopurge.
  21. #
  22. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
  23. #
  24. # The number of snapshots to retain in dataDir
  25. #autopurge.snapRetainCount=3
  26. # Purge task interval in hours
  27. # Set to "0" to disable auto purge feature
  28. #autopurge.purgeInterval=1
  29. ## Metrics Providers
  30. #
  31. # https://prometheus.io Metrics Exporter
  32. #metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
  33. #metricsProvider.httpPort=7000
  34. #metricsProvider.exportJvmInfo=true
  35. server.1=hadoop101:2888:3888
  36. server.2=hadoop102:2888:3888
  37. server.3=hadoop103:2888:3888

其中:

  • 37~39行:添加集群机器地址,格式:server.编号=主机:端口:端口
  • 12行:dataDir参数为数据存储目录

配置myid文件

在数据存储目录下新建一个文件myid,文件内容为当前server的编号,三台主机分别对应1、2、3

  1. [root@hadoop101 conf]# cat /home/data/zookeeper/myid
  2. 1

以上为主机1的id为1

分发安装包

  1. 拷贝整个目录到机器2、和机器3中
  2. 修改对应的数据存储目录下的myid文件内容

启动集群

  1. 在hadoop101上建立群起脚本:startZkAll.sh
  2. 脚本内容如下 ```shell

    !/bin/bash

for host in “hadoop101” “hadoop102” “hadoop103” do echo “** Start server $host **“ ssh $host “source /etc/profile;/usr/local/apache-zookeeper-3.6.1/bin/zkServer.sh start” echo “** /finish $host **“ done

  1. 3. 启动集群
  2. ```shell
  3. [root@hadoop101 ~]# ./startZkAll.sh
  4. ********** Start server hadoop101 **********
  5. ZooKeeper JMX enabled by default
  6. Using config: /usr/local/apache-zookeeper-3.6.1/bin/../conf/zoo.cfg
  7. Starting zookeeper ... STARTED
  8. ********** /finish hadoop101 **********
  9. ********** Start server hadoop102 **********
  10. ZooKeeper JMX enabled by default
  11. Using config: /usr/local/apache-zookeeper-3.6.1/bin/../conf/zoo.cfg
  12. Starting zookeeper ... STARTED
  13. ********** /finish hadoop102 **********
  14. ********** Start server hadoop103 **********
  15. ZooKeeper JMX enabled by default
  16. Using config: /usr/local/apache-zookeeper-3.6.1/bin/../conf/zoo.cfg
  17. Starting zookeeper ... STARTED
  18. ********** /finish hadoop103 **********

查看状态

  1. 在hadoop101上查看: ```shell [root@hadoop101 ~]# /usr/local/apache-zookeeper-3.6.1/bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/local/apache-zookeeper-3.6.1/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Mode: follower

2. 在hadoop102上查看:
```shell
[root@hadoop102 ~]# /usr/local/apache-zookeeper-3.6.1/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/apache-zookeeper-3.6.1/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader
  1. 在hadoop103上查看: ```shell [root@hadoop103 apache-zookeeper-3.6.1]# /usr/local/apache-zookeeper-3.6.1/bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/local/apache-zookeeper-3.6.1/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Mode: follower
<a name="hTYW2"></a>
# 停止集群

1. 在hadoop101上建立群停脚本:stopZkAll.sh
1. 脚本内容如下
```shell
[root@hadoop101 ~]# cat stopZkAll.sh
#! /bin/bash
for host in "hadoop101" "hadoop102" "hadoop103"
do
        echo "********** Start server $host **********"
        ssh $host "source /etc/profile;/usr/local/apache-zookeeper-3.6.1/bin/zkServer.sh stop"
        echo "********** /finish $host **********"
done
  1. 停止集群 ```shell [root@hadoop101 ~]# ./stopZkAll.sh ** Start server hadoop101 ** ZooKeeper JMX enabled by default Using config: /usr/local/apache-zookeeper-3.6.1/bin/../conf/zoo.cfg Stopping zookeeper … STOPPED ** /finish hadoop101 ** ** Start server hadoop102 ** ZooKeeper JMX enabled by default Using config: /usr/local/apache-zookeeper-3.6.1/bin/../conf/zoo.cfg Stopping zookeeper … STOPPED ** /finish hadoop102 ** ** Start server hadoop103 ** ZooKeeper JMX enabled by default Using config: /usr/local/apache-zookeeper-3.6.1/bin/../conf/zoo.cfg Stopping zookeeper … STOPPED ** /finish hadoop103 **

```


至此,完成了Zookeeper集群的安装和启停。