ZooKeeper集群部署
安装
rz上传hadoop到hadoop102服务器上
[hadoop@hadoop102 software]$ ls
hadoop-3.1.3.tar.gz jdk-8u212-linux-x64.tar.gz zookeeper-3.4.5-cdh5.12.1.tar.gz
[hadoop@hadoop102 software]$ tar -zxvf zookeeper-3.4.5-cdh5.12.1.tar.gz -C /opt/module/
配置服务器编号
(1)在/opt/module/zookeeper-3.4.5-cdh5.12.1/这个目录下创建zkData
[hadoop@hadoop102 zookeeper-3.4.5-cdh5.12.1]$ mkdir -p zkData
(2)在/opt/module/zookeeper-3.4.5-cdh5.12.1/zkData目录下创建一个myid的文件
[hadoop@hadoop102 zkData]$ touch myid
添加myid文件,注意一定要在linux里面创建,在notepad++里面很可能乱码
(3)编辑myid文件
[hadoop@hadoop102 zkData]$ vi myid
在文件中添加与server对应的编号:
2
(4)拷贝配置好的zookeeper到其他机器上
[hadoop@hadoop102 zkData]$ xsync /opt/module/zookeeper-3.4.5-cdh5.12.1
并分别在hadoop103、hadoop104上修改myid文件中内容为3、4
配置zoo.cfg
(1)重命名/opt/module/zookeeper-3.5.7/conf这个目录下的zoo_sample.cfg为zoo.cfg
[hadoop@hadoop102 conf]$ cp zoo_sample.cfg zoo.cfg
(2)打开zoo.cfg文件
[hadoop@hadoop102 conf]$ vi zoo.cfg
修改数据存储路径配置
dataDir=/opt/module/zookeeper-3.4.5-cdh5.12.1/zkData
增加如下配置
#######################cluster##########################
server.2=hadoop102:2888:3888
server.3=hadoop103:2888:3888
server.4=hadoop104:2888:3888
(3)同步zoo.cfg配置文件
[hadoop@hadoop102 conf]$ xsync zoo.cfg
(4)配置参数解读
server.A=B:C:D。
****A****是一个数字,表示这个是第几号服务器;
集群模式下配置一个文件myid,这个文件在dataDir目录下,这个文件里面有一个数据就是A的值,Zookeeper启动时读取此文件,拿到里面的数据与zoo.cfg里面的配置信息比较从而判断到底是哪个server。
****B****是这个服务器的地址;
****C****是这个服务器Follower与集群中的Leader服务器交换信息的端口;
****D****是万一集群中的Leader服务器挂了,需要一个端口来重新进行选举,选出一个新的Leader,而这个端口就是用来执行选举时服务器相互通信的端口。
分布式配置
在zkData目录下创建一个myid文件
touch /opt/module/apache-zookeeper-3.5.6-bin/zkData/myid
编辑myid文件,添加zk编号
vim /opt/module/apache-zookeeper-3.5.6-bin/zkData/myid
修改zoo.cfg
#################cluster####################
server.112=hadoop112:2888:3888
server.113=hadoop113:2888:3888
server.114=hadoop114:2888:3888
分发zoo.cfg
xsync /opt/module/apache-zookeeper-3.5.6-bin/conf/zoo.cfg
集群启动
分别启动Zookeeper
[hadoop@hadoop102 zookeeper-3.4.5-cdh5.12.1]$ bin/zkServer.sh start
[hadoop@hadoop103 zookeeper-3.4.5-cdh5.12.1]$ bin/zkServer.sh start
[hadoop@hadoop104 zookeeper-3.4.5-cdh5.12.1]$ bin/zkServer.sh start
查看状态
[hadoop@hadoop102 zookeeper-3.4.5-cdh5.12.1]$ bin/zkServer.sh status
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.5-cdh5.12.1/bin/../conf/zoo.cfg
Mode: follower
[hadoop@hadoop103 zookeeper-3.4.5-cdh5.12.1]$ bin/zkServer.sh status
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.5-cdh5.12.1/bin/../conf/zoo.cfg
Mode: leader
[hadoop@hadoop104 zookeeper-3.4.5-cdh5.12.1]$ bin/zkServer.sh status
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.5-cdh5.12.1/bin/../conf/zoo.cfg
Mode: follower
集群启动脚本
#!/bin/bash
if [ $# -lt 1 ]
then
echo "No Args Input..."
exit;
fi
case $1 in
"start")
echo " =================== 启动 Zookeeper 集群 ==================="
ssh hadoop102 "/opt/module/zookeeper-3.4.5-cdh5.12.1/bin/zkServer.sh start"
ssh hadoop103 "/opt/module/zookeeper-3.4.5-cdh5.12.1/bin/zkServer.sh start"
ssh hadoop104 "/opt/module/zookeeper-3.4.5-cdh5.12.1/bin/zkServer.sh start"
;;
"stop")
echo " =================== 关闭 Zookeeper 集群 ==================="
ssh hadoop102 "/opt/module/zookeeper-3.4.5-cdh5.12.1/bin/zkServer.sh stop"
ssh hadoop103 "/opt/module/zookeeper-3.4.5-cdh5.12.1/bin/zkServer.sh stop"
ssh hadoop104 "/opt/module/zookeeper-3.4.5-cdh5.12.1/bin/zkServer.sh stop"
;;
"status")
echo " =================== Zookeeper 集群状态 ==================="
ssh hadoop102 "/opt/module/zookeeper-3.4.5-cdh5.12.1/bin/zkServer.sh status"
ssh hadoop103 "/opt/module/zookeeper-3.4.5-cdh5.12.1/bin/zkServer.sh status"
ssh hadoop104 "/opt/module/zookeeper-3.4.5-cdh5.12.1/bin/zkServer.sh status"
;;
*)
echo "Input Args Error..."
;;
esac
操作如下:
[hadoop@hadoop102 bin]$ vi myzk.sh
##这里将上面的脚本复制进去
[hadoop@hadoop102 bin]$ chmod +x myzk.sh
[hadoop@hadoop102 bin]$ myzk.sh status
=================== Zookeeper 集群状态 ===================
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.5-cdh5.12.1/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.5-cdh5.12.1/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.5-cdh5.12.1/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
[hadoop@hadoop102 bin]$ myzk.sh start
=================== 启动 Zookeeper 集群 ===================
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.5-cdh5.12.1/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.5-cdh5.12.1/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.5-cdh5.12.1/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[hadoop@hadoop102 bin]$ myzk.sh status
=================== Zookeeper 集群状态 ===================
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.5-cdh5.12.1/bin/../conf/zoo.cfg
Mode: follower
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.5-cdh5.12.1/bin/../conf/zoo.cfg
Mode: leader
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.5-cdh5.12.1/bin/../conf/zoo.cfg
Mode: follower