目录
前提条件
集群规划
安装步骤
下载zookeeper-3.7.1安装包
解压
同步至其他机器
配置环境变量(所有机器都执行)
配置服务器编号
配置zoo.cfg
分别启动zk服务
查看zk状态
进入zk客户端命令行测试
ZK集群启动停止脚本
前提条件
- 具备三台CentOS7
- 三台都安装好jdk8
集群规划
node2 | node3 | node4 |
---|---|---|
zk | zk | zk |
安装步骤
下载zookeeper-3.7.1
安装包
[hadoop@node2 installfile]$ wget https://archive.apache.org/dist/zookeeper/zookeeper-3.7.1/apache-zookeeper-3.7.1-bin.tar.gz
解压
[hadoop@node2 installfile]$ tar -zxvf apache-zookeeper-3.7.1-bin.tar.gz -C ~/soft/
同步至其他机器
[hadoop@node2 installfile]$ cd ~/soft/ [hadoop@node2 soft]$ xsync apache-zookeeper-3.7.1-bin
配置环境变量(所有机器都执行)
$ sudo nano /etc/profile.d/my_env.sh
添加如下内容
#ZOOKEEPER_HOME export ZOOKEEPER_HOME=/home/hadoop/soft/apache-zookeeper-3.7.1-bin export PATH=$PATH:$ZOOKEEPER_HOME/bin
让环境变量生效
$ source /etc/profile
注意:node2、node3、node4都需要设置环境变量并让环境变量生效。
配置服务器编号
新建myid
node2机器 [hadoop@node2 apache-zookeeper-3.7.1-bin]$ cd $ZOOKEEPER_HOME/ [hadoop@node2 apache-zookeeper-3.7.1-bin]$ mkdir zkData [hadoop@node2 apache-zookeeper-3.7.1-bin]$ nano zkData/myid 内容为 2 node3机器 [hadoop@node3 apache-zookeeper-3.7.1-bin]$ cd $ZOOKEEPER_HOME/ [hadoop@node3 apache-zookeeper-3.7.1-bin]$ mkdir zkData [hadoop@node3 apache-zookeeper-3.7.1-bin]$ nano zkData/myid 内容为 3 node4机器 [hadoop@node4 apache-zookeeper-3.7.1-bin]$ cd $ZOOKEEPER_HOME/ [hadoop@node4 apache-zookeeper-3.7.1-bin]$ mkdir zkData [hadoop@node4 apache-zookeeper-3.7.1-bin]$ nano zkData/myid 内容为 4
配置zoo.cfg
[hadoop@node2 conf]$ cd $ZOOKEEPER_HOME/conf [hadoop@node2 conf]$ cp zoo_sample.cfg zoo.cfg [hadoop@node2 conf]$ nano zoo.cfg 修改 dataDir=/home/hadoop/soft/apache-zookeeper-3.7.1-bin/zkData 末尾添加 server.2=node2:2888:3888 server.3=node3:2888:3888 server.4=node4:2888:3888
server.A=B:C:D。
A:机器编号
B:主机名
C: Leader与Follower的通信端口
D: 选举Leader接口
同步zoo.cfg
[hadoop@node2 conf]$ xsync zoo.cfg
分别启动zk服务
[hadoop@node2 ~]$ zkServer.sh start ZooKeeper JMX enabled by default Using config: /home/hadoop/soft/apache-zookeeper-3.7.1-bin/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [hadoop@node3 ~]$ zkServer.sh start ZooKeeper JMX enabled by default Using config: /home/hadoop/soft/apache-zookeeper-3.7.1-bin/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [hadoop@node4 ~]$ zkServer.sh start ZooKeeper JMX enabled by default Using config: /home/hadoop/soft/apache-zookeeper-3.7.1-bin/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
查看zk状态
[hadoop@node2 ~]$ zkServer.sh status ZooKeeper JMX enabled by default Using config: /home/hadoop/soft/apache-zookeeper-3.7.1-bin/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Client SSL: false. Mode: follower [hadoop@node3 ~]$ zkServer.sh status ZooKeeper JMX enabled by default Using config: /home/hadoop/soft/apache-zookeeper-3.7.1-bin/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Client SSL: false. Mode: leader [hadoop@node4 ~]$ zkServer.sh status ZooKeeper JMX enabled by default Using config: /home/hadoop/soft/apache-zookeeper-3.7.1-bin/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Client SSL: false. Mode: follower
看到其中一台机器是leader,其他机器是follower。
进入zk客户端命令行测试
[hadoop@node2 ~]$ zkCli.sh ... 省略若干日志输出 ... [zk: localhost:2181(CONNECTED) 0] ls / [zookeeper] [zk: localhost:2181(CONNECTED) 1] create -e /a 1 Created /a [zk: localhost:2181(CONNECTED) 2] get /a 1 [zk: localhost:2181(CONNECTED) 3] quit ... 省略若干日志输出 ... [hadoop@node2 ~]$
ZK集群启动停止脚本
进入~/bin
目录,创建zk.sh
[hadoop@node2 ~]$ cd ~/bin/ [hadoop@node2 bin]$ vim zk.sh
内容如下:
#!/bin/bashcase $1 in
"start"){for i in node2 node3 node4doecho ---------- zookeeper $i 启动 ------------ssh $i "/home/hadoop/soft/apache-zookeeper-3.7.1-bin/bin/zkServer.sh start"done
};;
"stop"){for i in node2 node3 node4doecho ---------- zookeeper $i 停止 ------------ ssh $i "/home/hadoop/soft/apache-zookeeper-3.7.1-bin/bin/zkServer.sh stop"done
};;
"status"){for i in node2 node3 node4doecho ---------- zookeeper $i 状态 ------------ ssh $i "/home/hadoop/soft/apache-zookeeper-3.7.1-bin/bin/zkServer.sh status"done
};;
esac
添加执行权限
[hadoop@node2 bin]$ chmod u+x zk.sh [hadoop@node2 bin]$ ll total 16 -rwxrwxr-x. 1 hadoop hadoop 125 Apr 2 09:40 jpsall -rwxrwxr-x. 1 hadoop hadoop 1142 Apr 2 09:48 myhadoop.sh -rwxrwxr-x. 1 hadoop hadoop 731 Apr 1 11:36 xsync -rwxrw-r--. 1 hadoop hadoop 565 Apr 7 09:53 zk.sh
测试
[hadoop@node2 bin]$ zk.sh start ---------- zookeeper node2 启动 ------------ ZooKeeper JMX enabled by default Using config: /home/hadoop/soft/zookeeper-3.5.7/bin/../conf/zoo.cfg Starting zookeeper ... STARTED ---------- zookeeper node3 启动 ------------ ZooKeeper JMX enabled by default Using config: /home/hadoop/soft/zookeeper-3.5.7/bin/../conf/zoo.cfg Starting zookeeper ... STARTED ---------- zookeeper node4 启动 ------------ ZooKeeper JMX enabled by default Using config: /home/hadoop/soft/zookeeper-3.5.7/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [hadoop@node2 bin]$ zk.sh status ---------- zookeeper node2 状态 ------------ ZooKeeper JMX enabled by default Using config: /home/hadoop/soft/zookeeper-3.5.7/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Mode: follower ---------- zookeeper node3 状态 ------------ ZooKeeper JMX enabled by default Using config: /home/hadoop/soft/zookeeper-3.5.7/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Mode: leader ---------- zookeeper node4 状态 ------------ ZooKeeper JMX enabled by default Using config: /home/hadoop/soft/zookeeper-3.5.7/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Mode: follower [hadoop@node2 bin]$ zk.sh stop ---------- zookeeper node2 停止 ------------ ZooKeeper JMX enabled by default Using config: /home/hadoop/soft/zookeeper-3.5.7/bin/../conf/zoo.cfg Stopping zookeeper ... STOPPED ---------- zookeeper node3 停止 ------------ ZooKeeper JMX enabled by default Using config: /home/hadoop/soft/zookeeper-3.5.7/bin/../conf/zoo.cfg Stopping zookeeper ... STOPPED ---------- zookeeper node4 停止 ------------ ZooKeeper JMX enabled by default Using config: /home/hadoop/soft/zookeeper-3.5.7/bin/../conf/zoo.cfg Stopping zookeeper ... STOPPED
为了能在其他机器执行zk.sh
脚本命令,分发zk.sh
到其他机器
[hadoop@node2 bin]$ xsync zk.sh
完成!enjoy it!