Zookeeper(一)在WSL单机搭建Zookeeper伪集群

目录

  • Zookeeper
    • [1 启动单个Zookeeper实例](#1 启动单个Zookeeper实例)
      • [1.1 下载Zookeeper安装包并解压](#1.1 下载Zookeeper安装包并解压)
      • [1.2 添加环境变量](#1.2 添加环境变量)
      • [1.3 修改默认配置](#1.3 修改默认配置)
      • [1.4 新建数据存储目录和日志目录](#1.4 新建数据存储目录和日志目录)
      • [1.5 启动Zookeeper](#1.5 启动Zookeeper)
      • [1.6 停止Zookeeper](#1.6 停止Zookeeper)
    • [2 搭建Zookeeper集群](#2 搭建Zookeeper集群)
      • [2.1 新建集群目录](#2.1 新建集群目录)
      • [2.2 配置环境变量](#2.2 配置环境变量)
      • [2.3 创建节点目录](#2.3 创建节点目录)
      • [2.4 修改配置](#2.4 修改配置)
      • [2.5 创建节点ID文件](#2.5 创建节点ID文件)
      • [2.6 启动集群](#2.6 启动集群)
      • [2.7 停止集群](#2.7 停止集群)

Zookeeper

本文尝试在单机搭建包含有3个节点的Zookeeper集群。

环境:Windows 11 WSL2

Linux发行版本:Ubuntu 22.04.2 LTS

1 启动单个Zookeeper实例

1.1 下载Zookeeper安装包并解压

bash 复制代码
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 添加环境变量

配置环境变量:

bash 复制代码
vim ~/.bashrc

添加如下内容:

#set Zookeeper env vars

export ZOOKEEPER_HOME=/usr/local/bin/apache-zookeeper-3.8.3-bin

export PATH=PATH:ZOOKEEPER_HOME/bin

加载新的环境变量:

bash 复制代码
source ~/.bashrc

1.3 修改默认配置

bash 复制代码
cd $ZOOKEEPER_HOME/conf
cp zoo_sample.cfg zoo.cfg
vim zoo.cfg

在配置文件中修改数据存储目录:

dataDir=$ZOOKEEPER_HOME/data

1.4 新建数据存储目录和日志目录

bash 复制代码
sudo mkdir $ZOOKEEPER_HOME/data
sudo chmod -R 777 data
sudo mkdir $ZOOKEEPER_HOME/logs
sudo chmod -R 777 logs

1.5 启动Zookeeper

bash 复制代码
zkServer.sh start

查看状态:

bash 复制代码
zkServer.sh status

启动命令行:

bash 复制代码
zkCli.sh

1.6 停止Zookeeper

停止Zookeeper

bash 复制代码
zkServer.sh stop

2 搭建Zookeeper集群

2.1 新建集群目录

bash 复制代码
cd /usr/local/bin
sudo zookeeper-cluster
chmod -R 777 zookeeper-cluster

2.2 配置环境变量

bash 复制代码
vim ~/.bashrc
export ZK_CLUSTER_HOME /usr/local/bin/zookeeper-cluster
source ~/.bashrc

2.3 创建节点目录

将Zookeeper安装目录复制3份:

bash 复制代码
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

清理之前单实例产生的数据:

bash 复制代码
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

如果当前账号没有写权限,需要赋权限:

bash 复制代码
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

bash 复制代码
ip addr

分别修改3个节点的配置

bash 复制代码
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

bash 复制代码
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

bash 复制代码
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文件

bash 复制代码
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 启动集群

分别启动三个Zookeeper实例:

bash 复制代码
$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

查看节点状态:

bash 复制代码
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

bash 复制代码
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

bash 复制代码
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 停止集群

bash 复制代码
$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
相关推荐
金刚猿5 分钟前
简单理解下基于 Redisson 库的分布式锁机制
分布式·分布式锁·redisson
我一直在流浪27 分钟前
Kafka - 消费者程序仅消费一半分区消息的问题
分布式·kafka
张彦峰ZYF2 小时前
投资策略规划最优决策分析
分布式·算法·金融
processflow流程图4 小时前
分布式kettle调度平台v6.4.0新功能介绍
分布式
全栈开发圈4 小时前
干货分享|分布式数据科学工具 Xorbits 的使用
分布式
运维&陈同学6 小时前
【zookeeper01】消息队列与微服务之zookeeper工作原理
运维·分布式·微服务·zookeeper·云原生·架构·消息队列
时差9536 小时前
Flink Standalone集群模式安装部署
大数据·分布式·flink·部署
菠萝咕噜肉i7 小时前
超详细:Redis分布式锁
数据库·redis·分布式·缓存·分布式锁
只因在人海中多看了你一眼10 小时前
分布式缓存 + 数据存储 + 消息队列知识体系
分布式·缓存
zhixingheyi_tian13 小时前
Spark 之 Aggregate
大数据·分布式·spark