Zookeeper
本文尝试在单机搭建包含有3个节点的Zookeeper集群。
环境:Windows 11 WSL2
Linux发行版本:Ubuntu 22.04.2 LTS
1 启动单个Zookeeper实例
1.1 下载Zookeeper安装包并解压
wget https://dlcdn.apache.org/zookeeper/zookeeper-3.8.3/apache-zookeeper-3.8.3-bin.tar.gz
sudo tar xvf apache-zookeeper-3.8.3-bin.tar.gz -C /usr/local/bin
1.2 添加环境变量
vim ~/.bashrc
#set Zookeeper env vars
export ZOOKEEPER_HOME=/usr/local/bin/apache-zookeeper-3.8.3-bin
export PATH=$PATH:$ZOOKEEPER_HOME/bin
source ~/.bashrc
1.3 修改默认配置
cd $ZOOKEEPER_HOME/conf
cp zoo_sample.cfg zoo.cfg
vim zoo.cfg
1.4 新建数据存储目录和日志目录
sudo mkdir $ZOOKEEPER_HOME/data
sudo chmod -R 777 data
sudo mkdir $ZOOKEEPER_HOME/logs
sudo chmod -R 777 logs
1.5 启动Zookeeper
zkServer.sh start
zkServer.sh status
zkCli.sh
1.6 停止Zookeeper
停止Zookeeper
zkServer.sh stop
2 搭建Zookeeper集群
2.1 新建集群目录
cd /usr/local/bin
sudo zookeeper-cluster
chmod -R 777 zookeeper-cluster
2.2 配置环境变量
vim ~/.bashrc
export ZK_CLUSTER_HOME /usr/local/bin/zookeeper-cluster
source ~/.bashrc
2.3 创建节点目录
cp -r apache-zookeeper-3.8.3-bin zookeeper-cluster/zookeeper-1
cp -r apache-zookeeper-3.8.3-bin zookeeper-cluster/zookeeper-2
cp -r apache-zookeeper-3.8.3-bin zookeeper-cluster/zookeeper-3
sudo rm -rf zookeeper-cluster/zookeeper-1/data
sudo rm -rf zookeeper-cluster/zookeeper-2/data
sudo rm -rf zookeeper-cluster/zookeeper-3/data
sudo mkdir zookeeper-cluster/zookeeper-1/data
sudo mkdir zookeeper-cluster/zookeeper-2/data
sudo mkdir zookeeper-cluster/zookeeper-3/data
sudo chmod -R 777 zookeeper-cluster/zookeeper-1
sudo chmod -R 777 zookeeper-cluster/zookeeper-2
sudo chmod -R 777 zookeeper-cluster/zookeeper-3
2.4 修改配置
ip addr
分别修改3个节点的配置
vim zookeeper-cluster/zookeeper-1/conf/zoo.cfg
dataDir=$ZK_CLUSTER_HOME/zookeeper-1/data
clientPort=2181
#Cluster
server.1=172.26.143.96:2881:3881
server.2=172.26.143.96:2882:3882
server.3=172.26.143.96:2883:3883
vim zookeeper-cluster/zookeeper-2/conf/zoo.cfg
dataDir=$ZK_CLUSTER_HOME/zookeeper-2/data
clientPort=2182
#Cluster
server.1=172.26.143.96:2881:3881
server.2=172.26.143.96:2882:3882
server.3=172.26.143.96:2883:3883
vim zookeeper-cluster/zookeeper-3/conf/zoo.cfg
dataDir=$ZK_CLUSTER_HOME/zookeeper-3/data
clientPort=2183
#Cluster
server.1=172.26.143.96:2881:3881
server.2=172.26.143.96:2882:3882
server.3=172.26.143.96:2883:3883
2.5 创建节点ID文件
echo 1 > zookeeper-cluster/zookeeper-1/data/myid
echo 2 > zookeeper-cluster/zookeeper-2/data/myid
echo 3 > zookeeper-cluster/zookeeper-3/data/myid
2.6 启动集群
$ZK_CLUSTER_HOME/zookeeper-1/bin/zkServer.sh start
$ZK_CLUSTER_HOME/zookeeper-2/bin/zkServer.sh start
$ZK_CLUSTER_HOME/zookeeper-3/bin/zkServer.sh start
ZK_CLUSTER_HOME/zookeeper-1/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/bin/zookeeper-cluster/zookeeper-1/bin/…/conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
ZK_CLUSTER_HOME/zookeeper-2/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/bin/zookeeper-cluster/zookeeper-2/bin/…/conf/zoo.cfg
Client port found: 2182. Client address: localhost. Client SSL: false.
Mode: leader
ZK_CLUSTER_HOME/zookeeper-3/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/bin/zookeeper-cluster/zookeeper-3/bin/…/conf/zoo.cfg
Client port found: 2183. Client address: localhost. Client SSL: false.
Mode: follower
2.7 停止集群
$ZK_CLUSTER_HOME/zookeeper-1/bin/zkServer.sh stop
$ZK_CLUSTER_HOME/zookeeper-2/bin/zkServer.sh stop
$ZK_CLUSTER_HOME/zookeeper-3/bin/zkServer.sh stop
原文地址:https://blog.csdn.net/yunyun1886358/article/details/134617927
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_18605.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!