搭建三台机器集群,三台主机分别为hadoop101、hadoop102、hadoop103 官网:https://zookeeper.apache.org/doc/current/zookeeperStarted.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
# The number of milliseconds of each tick
tickTime=2000
# 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=/home/data/zookeeper
# 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
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
server.1=hadoop101:2888:3888
server.2=hadoop102:2888:3888
server.3=hadoop103:2888:3888
其中:
- 37~39行:添加集群机器地址,格式:server.编号=主机:端口:端口
- 12行:dataDir参数为数据存储目录
配置myid文件
在数据存储目录下新建一个文件myid,文件内容为当前server的编号,三台主机分别对应1、2、3
[root@hadoop101 conf]# cat /home/data/zookeeper/myid
1
以上为主机1的id为1
分发安装包
- 拷贝整个目录到机器2、和机器3中
- 修改对应的数据存储目录下的myid文件内容
启动集群
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
3. 启动集群
```shell
[root@hadoop101 ~]# ./startZkAll.sh
********** Start server hadoop101 **********
ZooKeeper JMX enabled by default
Using config: /usr/local/apache-zookeeper-3.6.1/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
********** /finish hadoop101 **********
********** Start server hadoop102 **********
ZooKeeper JMX enabled by default
Using config: /usr/local/apache-zookeeper-3.6.1/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
********** /finish hadoop102 **********
********** Start server hadoop103 **********
ZooKeeper JMX enabled by default
Using config: /usr/local/apache-zookeeper-3.6.1/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
********** /finish hadoop103 **********
查看状态
- 在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
- 在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
- 停止集群 ```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集群的安装和启停。