ZooKeeper 的环境搭建
搭建-安装JDK
安装Zookeeper之前,我们需要安装JDK,JDK的安装在实验一已经介绍,本实验的环境已经默认安装了JDK,只需要按照下面的安装步骤即可安装Zookeeper。
安装步骤
1、切换到zookeeper压缩包所在位置,解压文件
cd /opt/
tar -zxvf /opt/zookeeper-3.4.9.tar.gz -C /opt


2、创建data和log文件
2.1、切换到zookeeper的安装目录下。
cd /opt/zookeeper-3.4.9/

2.2、在目录下新建一个data的文件夹。
mkdir data

2.3、在目录下面新建一个log的文件夹。
mkdir log

3、修改zookeeper配置文件
3.1、切换到/opt/zookeeper-3.4.9/data目录下。

3.2、在该目录下,创建一个myid的文件。
touch myid

3.3、编辑/opt/zookeeper-3.4.9/data目录下的myid文件
在master节点下,编辑myid文件。
vim myid

这里指定master的id为1。

4、重命名/opt/zookeeper-3.4.9/conf目录下的zoo_sample.cfg文件为zoo.cfg,并编辑zoo.cfg文件。
4.1、切换到zoo_sample.cfg文件所在的目录
cd /opt/zookeeper-3.4.9/conf

4.2、重命名zoo_sample.cfg文件为zoo.cfg
mv zoo_sample.cfg zoo.cfg

4.3、编辑zoo.cfg文件。
vim zoo.cfg


5、复制master节点的zookeeper-3.4.9文件夹到slave1节点的/opt目录下。(复制时,如果没有配置免密码登录,则会被要求输入密码,默认slave1的密码是123456
)。
scp -r /opt/zookeeper-3.4.9 root@192.168.1.3:/opt

6、复制master节点的zookeeper-3.4.9文件夹到slave2节点的/opt目录下。(复制时,如果没有配置免密码登录,则会被要求输入密码,默认slave2的密码是123456)。
scp -r /opt/zookeeper-3.4.9 root@192.168.1.4:/opt

7、切换到slave1节点,修改slave1节点下面的myid文件,将slave1的id修改为2。
7.1、切换到slave1节点下面的/opt/zookeeper-3.4.9/data目录下
cd /opt/zookeeper-3.4.9/data/

7.2、编辑该目录下的myid文件
vim myid


8、切换到slave2节点,修改slave2节点下面的myid文件,将slave2的id修改为3。
8.1、切换到slave2节点下面的/opt/zookeeper-3.4.9/data目录下
cd /opt/zookeeper-3.4.9/data/

8.2、编辑改目录下的myid文件
vim myid


启动zookeeper
注:三个节点都执行如下操作,以master为例。
1、切换到安装目录的bin目录下(/opt/zookeeper-3.4.9/bin/)

2、执行启动命令
./zkServer.sh start

jps查看zookeeper进程
Master节点下:
jps

Slave1节点下:
jps

Slave2节点下:
jps

检查运行状态
1、切换到安装目录的bin目录下(/opt/zookeeper-3.4.9/bin/)
cd /opt/zookeeper-3.4.9/bin/

2、检查运行状态
Master节点:
./zkServer.sh status

Slave1节点:
./zkServer.sh status

Slave2节点:
./zkServer.sh status

客户端连接测试
对于客户端来说,ZooKeeper是一个整体(ensemble),无论连接到那个节点,实际上都在独享整个ZooKeeper集群的服务,所以,我们可以在任何一个结点上建立到服务集群的连接。
1、采用zkCli.sh -server master:2181连接到ZeeKeeper。
zkCli.sh -server master:2181
[图片上传中...(image-436c57-1509675092309-3)]
2、退出连接
quit
[图片上传中...(image-4cd1af-1509675092309-2)]
停止zookeeper
注:三台节点都执行如下操作,以master为例。
1、切换到安装目录的bin目录下(/opt/zookeeper-3.4.9/bin)
cd /opt/zookeeper-3.4.9/bin/

2、执行停止命令
./zkServer.sh stop
[图片上传中...(image-9cd07-1509675092309-0)]
小结:
在我们启动zookeeper时,返回消息表明zookeeper已经启动,但是检查状态时发现,zookeeper并没有运行。
解决办法:
在/opt/zookeeper-3.4.9/bin目录下查看zookeeper.out日志文件。