这里以三台虚拟机为例搭建zookeeper集群
1、新建三台虚拟机
分别命名hadoop01、hadoop02、hadoop03
hostnamectl set-hostname <your new hostname>
在一切开始之前,需要
- 安装java环境:http://blog.kdyzm.cn/post/53
- 配置ssh免登录:http://blog.kdyzm.cn/post/19
2、配置hosts文件
三台虚拟机分别配置hosts文件
[root@hadoop01 local]# vim /etc/hosts
192.168.3.21 hadoop01
192.168.3.22 hadoop02
192.168.3.23 hadoop03
3、官网下载zookeeper3.4.6
官网地址:https://zookeeper.apache.org/releases.html
官网下载地址:https://archive.apache.org/dist/zookeeper/
直接下载地址:https://archive.apache.org/dist/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz
下载之后使用工具上传到 hadoop01机器的/usr/local文件夹,使用命令
cd /usr/local
tar -zxvf zookeeper-3.4.6.tar.gz
解压到当前目录,并且建立软链接
ln -s zookeeper-3.4.6 zookeeper
4、配置环境变量
[root@hadoop01 local]# vim /etc/profile
export ZOOKEEPER_HOME=/usr/local/zookeeper-3.4.6
export PATH=$PATH:$ZOOKEEPER_HOME/bin
将配置文件复制到其它两台机器
5、创建数据目录
[root@hadoop01 zookeeper]# mkdir /usr/local/zookeeper/zkdata
6、修改配置文件
[root@hadoop01 conf]# cd /usr/local/zookeeper/conf
[root@hadoop01 conf]# cp zoo_sample.cfg zoo.cfg
修改data_dir目录
dataDir=/usr/local/zookeeper/zkdata
新增集群配置
server.1=hadoop01:2888:3888
server.2=hadoop02:2888:3888
server.3=hadoop03:2888:3888
7、新增myid文件
[root@hadoop01 conf]# vim /usr/local/zookeeper/zkdata/myid
1
8、复制程序到其它两个节点
[root@hadoop01 conf]# scp -r /usr/local/zookeeper-3.4.6 hadoop02:/usr/local
[root@hadoop01 conf]# scp -r /usr/local/zookeeper-3.4.6 hadoop03:/usr/local
9、修改剩余两个节点的myid
[root@hadoop02 conf]# vim /usr/local/zookeeper/zkdata/myid
2
[root@hadoop03 conf]# vim /usr/local/zookeeper/zkdata/myid
3
10、启动服务
从第一台机器开始分别启动zookeeper
[root@hadoop01 conf]# /usr/local/zookeeper/bin/zkServer.sh start
[root@hadoop02 conf]# /usr/local/zookeeper/bin/zkServer.sh start
[root@hadoop03 conf]# /usr/local/zookeeper/bin/zkServer.sh start
11、验证服务
在每个机器上运行jps
命令
[root@hadoop01 conf]# jps
2356 Jps
1901 QuorumPeerMain
QuorumPeerMain能看到的话就表明程序启动没问题了;接下来验证下每个服务的运行模式
在每个机器上运行命令: /usr/local/zookeeper/bin/zkServer.sh status
[root@hadoop01 conf]# /usr/local/zookeeper/bin/zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: follower
[root@hadoop02 conf]# /usr/local/zookeeper/bin/zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: leader
[root@hadoop03 .ssh]# /usr/local/zookeeper/bin/zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: follower
可以看到hadoop01、hadoop03运行在follower模式,hadoop02运行在leader模式;最后简单链接zk运行简单的命令
[root@hadoop01 bin]# /usr/local/zookeeper/bin/zkCli.sh -server localhost:2181
Connecting to localhost:2181
....此处省略若干日志
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper, test]
[zk: localhost:2181(CONNECTED) 1] create /kdyzm hello
Created /kdyzm
[zk: localhost:2181(CONNECTED) 2] get /kdyzm
hello
cZxid = 0x100000005
ctime = Wed Sep 28 00:58:40 CST 2022
mZxid = 0x100000005
mtime = Wed Sep 28 00:58:40 CST 2022
pZxid = 0x100000005
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0
[zk: localhost:2181(CONNECTED) 3] delete /kdyzm
[zk: localhost:2181(CONNECTED) 4]
这样就表示已经完全成功了
service脚本
vim /etc/init.d/zookeeper
#!/bin/bash
#chkconfig:2345 20 90
#description:zookeeper
#processname:zookeeper
export JAVA_HOME=//usr/local/jdk
case $1 in
start) su root /usr/local/zookeeper/bin/zkServer.sh start;;
stop) su root /usr/local/zookeeper/bin/zkServer.sh stop;;
status) su root /usr/local/zookeeper/bin/zkServer.sh status;;
restart) su /usr/local/zookeeper/bin/zkServer.sh restart;;
*) echo "require start|stop|status|restart" ;;
esac
修改完了之后使用命令
chmod a+x /etc/init.d/zookeeper
为启动脚本加上执行权限
注意:本文归作者所有,未经作者允许,不得转载