- apache官网下载合适的Zookeeper版本 地址,Zookeeper依赖jdk,提前安好jdk版本,建议使用oracle的jdk,建议不要使用openjdk
- 至少准备好三台Linux虚拟机,将下载的Zookeeper包上传到随意一个目录下
- 使用解压命令
tar -xf zookeeper-3.4.6.tar.gz
,解压好后如下 - 为了方便执行Zookeeper命令,配置下环境变量
vi /etc/profile
,添加如下
export ZOOKEEPER_HOME=/data/zookeeper-3.4.6
export PATH=$PATH:$ZOOKEEPER_HOME/bin
- 执行
source /etc/profile
让环境变量生效 - 在任何位置输入
zk
,再按tab就会有对应指令的提示,就说明我们环境变量配置成功了 - 其他三台也做同样的处理
- 修改Zookeeper的配置文件,在Zookeeper的conf目录下有一个zoo_sample.cfg的配置文件,这个是官方给的模板配置文件,拷贝一份同时命名为zoo.cfg,
cp zoo_sample.cfg zoo.cfg
,在此文件基础上进行修改
# The number of milliseconds of each tick
# 心跳间隔
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
# 初始化期间follower与leader最大容忍心跳次数
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.
# zookeeper 数据存放位置
dataDir=/usr/local/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
# 配置集群的格式 server.[myid]=[服务器IP/域名]:[Follower和Leader的通信端口]:[选举端口]
server.1=axs2:2888:3888
server.2=axs6:2888:3888
server.3=axs8:2888:3888
- 将 zookeeper-3.4.6 文件夹拷贝到其他两台虚拟机上
scp -r ./zookeeper-3.4.6/ axs6:/data/
scp -r ./zookeeper-3.4.6/ axs8:/data/
- 三台虚拟机分别创建目录
mkdir /usr/local/zookeeper
- 三台虚拟机分别添加myid文件 myid 要和配置文件中的myid一致
echo 1 > /usr/local/zookeeper/myid
echo 2 > /usr/local/zookeeper/myid
echo 3 > /usr/local/zookeeper/myid
- 按照 1 2 3的顺序启动Zookeeper,如下就启动成功