zookeeper学习(二) 集群模式安装

前置环境

  • 三台centos7服务器
powershell 复制代码
192.168.2.201
192.168.2.202
192.168.2.150
  • 三台服务器都需要安装jdk1.8以上
  • zookeeper安装包

安装jdk

在单机模式已经描述过,这里略过,有需要可以去看单机模式中的这部分,注意的是三台服务器都需要安装

安装zookeeper

  • 当前在192.168.2.201上操作
  • 解压zookeeper安装包
  • 在安装目录下新建zkData目录
powershell 复制代码
mkdir zkData
  • 修改修改conf/zoo.cfg文件
powershell 复制代码
 cp zoo_sample.cfg ./zoo.cfg
  • 修改zoo.cfg中的DataDir值 改为zkData所在的目录
powershell 复制代码
dataDir=/opt/software/zookeeper-3.4.6/zkData
  • 在zoo.cfg目录中添加集群信息,server.x 中的x是代表这台主机的序号,如server.1代表这台zookeeper的序号是1,server.2代表这台zookeeper的序号是2,server.3代表这台zookeeper的序号是3,
powershell 复制代码
server.1=192.168.2.201:2888:3888
server.2=192.168.2.202:2888:3888   
server.3=192.168.2.150:2888:3888
# 自动触动清楚任务时间间隔,小时为单位,默认为0,表示不自动清除
autopurge.purgeInterval=2
# 保存的快照数量,之外的会被清除
autopurge.snapRetainCount=5
  • 最终的zoo.cfg文件
powershell 复制代码
[root@aiops-release conf]# cat zoo.cfg 
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
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.
#dataDir=/tmp/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

dataDir=/opt/software/zookeeper-3.4.6/zkData
clientPort=2181 
server.1=192.168.2.201:2888:3888
server.2=192.168.2.202:2888:3888   
server.3=192.168.2.150:2888:3888
autopurge.purgeInterval=2
autopurge.snapRetainCount=5
  • 在zkData目录下创建myid文件,并写入1,对应之前zoo.cfg中的server.1中的序号1
powershell 复制代码
echo "1" >> myid
  • 配置环境变量
powershell 复制代码
vim /etc/profile

在文件底部添加zookeeper的安装目录

powershell 复制代码
export ZOOKEEPER_HOME=/opt/software/zookeeper-3.4.6
export PATH=$PATH:$ZOOKEEPER_HOME/bin
  • 刷新配置文件
powershell 复制代码
source /etc/profile
  • 复制整个zookeeper安装目录到其余两台服务器,最好在相同目录
powershell 复制代码
scp -r zookeeper-3.4.6/ root@192.168.2.202:/目录/z/zookeeper-3.4.6/
scp -r zookeeper-3.4.6/ root@192.168.2.150:/目录/z/zookeeper-3.4.6/
  • 修改192.168.2.202 和192.168.2.150中myid对应的值,分别为2和3
powershell 复制代码
192.168.2.202
echo "2" >> myid
192.168.2.150
echo "3" >> myid
  • 配置192.168.2.202 和192.168.2.150的环境变量,并刷新,与上面配置192.168.2.201时的步骤一致
  • 最终三台服务器都应该装有jdk1.8,以及三台服务器中的zoo.cfg文件是一致的,zkData中的myid分别为1,2,3,与zoo.cfg中的配置的服务器编号对应

启动

  • 依次启动三台服务器的zookeeper
  • 查看三台服务器的状态
    192.168.2.150
powershell 复制代码
[root@aiops-test bin]# ./zkServer.sh status
JMX enabled by default
Using config: /opt/software/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: leader

192.168.2.202

powershell 复制代码
[root@aiops-release bin]# ./zkServer.sh status
JMX enabled by default
Using config: /opt/software/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: follower

192.168.2.201

powershell 复制代码
[root@aiops bin]# ./zkServer.sh status
JMX enabled by default
Using config: /opt/software/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: follower
相关推荐
HERR_QQ2 小时前
强化学习的数学原理 学习笔记
人工智能·笔记·学习·自动驾驶
MartinYeung53 小时前
[论文学习]DP-Fusion:面向大语言模型的令牌级差分隐私推理-深度解析
人工智能·学习·语言模型
zwenqiyu3 小时前
非线性字符串数据结构串讲
数据结构·c++·学习·算法
MartinYeung54 小时前
[论文学习]SecureGate:通过令牌级门控学习何时安全地揭示PII-深度解析
学习·安全
大鱼>5 小时前
超参数调优进阶:Optuna/Bayesian/Early Stopping
人工智能·学习·机器学习·聚类
liuyicenysabel6 小时前
多服务上线日记二:
服务器·笔记·学习
小弥儿6 小时前
GitHub今日热榜 | 2026-07-05:阿里巴巴浏览器Agent上榜
学习·开源·github
‿hhh6 小时前
Dify核心模块详解:从文本生成到智能体
人工智能·学习·microsoft·agent·上下文·记忆
辰海Coding6 小时前
MiniSpring框架学习笔记-动态代理:如何在运行时插入逻辑?
java·笔记·学习·spring·动态代理
Sunsets_Red6 小时前
浅谈博弈论
c++·学习·编程·博弈论·信息学竞赛·巴巴博弈