本篇文章基于docker方式部署zookeeper集群,请先安装docker
目录
1. docker初期准备
zookeeper-toc" name="tableOfContents" style="margin-left:0px">2.启动zookeeper
2.1 单点部署
2.2 集群部署
3. Linux脚本实现快速切换启动关闭
1. docker初期准备
拉取zookeeper镜像
docker pull zookeeper:3.5.6
如果拉取时间过长,可以配置docker镜像源重启docker后重试
sudo bash -c 'echo '\''{"registry-mirrors": ["https://05f073ad3c0010ea0f4bc00b7105ec20.mirror.swr.myhuaweicloud.com","https://mirror.ccs.tencentyun.com","https://0dj0t5fb.mirror.aliyuncs.com","https://docker.mirrors.ustc.edu.cn","https://6kx4zyno.mirror.aliyuncs.com","https://akchsmlh.mirror.aliyuncs.com","https://hub-mirror.c.163.com","https://mirror.baidubce.com"]
}'\'' > /etc/docker/daemon.json'
sudo systemctl daemon-reload
sudo systemctl restart docker
创建docker网络(方便集群内部通信)
docker network create zookeeper-net
zookeeper" name="2.%E5%90%AF%E5%8A%A8zookeeper">2.启动zookeeper
2.1 单点部署
启动docker容器
第一台
# 指定容器名称 zookeeper
# 指定容器的主机名 zookeeper(在容器网络中使用的名称)
# 将容器加入名为 zookeeper-net 的 Docker 网络(方便集群扩展)
# 将容器的 2181 端口映射到宿主机的 2181 端口(Zookeeper 客户端默认端口)
# 设置容器的时区为 Asia/Shanghai
# 卷挂载:将宿主机的 /root/zookeeper/node-1/data 目录挂载到容器的 /data 目录(用于存储 Zookeeper 数据)
# 卷挂载:将宿主机的 /root/zookeeper/node-1/conf 目录挂载到容器的 /conf 目录(用于存放 Zookeeper 配置文件)
# 使用 zookeeper:3.5.6 镜像启动容器
docker run -d \--name zookeeper \--hostname zookeeper \--network zookeeper-net \-p 2181:2181 \-e TZ="Asia/Shanghai" \-v /root/zookeeper/node-1/data:/data \-v /root/zookeeper/node-1/conf:/conf \zookeeper:3.5.6
默认生成的配置文件都是单点部署的配置文件,那么单点部署就已经完成了
2.2 集群部署
由于zookeeper的选举机制是超过半数可用才能正常提供服务,因此一般集群个数为奇数台,这里以三台作案例
启动第二台
docker run -d \--name zookeeper-2 \--hostname zookeeper-2 \--network zookeeper-net \-p 2182:2181 \-e TZ="Asia/Shanghai" \-v /root/zookeeper/node-2/data:/data \-v /root/zookeeper/node-2/conf:/conf \zookeeper:3.5.6
启动第三台
docker run -d \--name zookeeper-3 \--hostname zookeeper-3 \--network zookeeper-net \-p 2183:2181 \-e TZ="Asia/Shanghai" \-v /root/zookeeper/node-3/data:/data \-v /root/zookeeper/node-3/conf:/conf \zookeeper:3.5.6
现在我们有了三台zookeeper单点的容器,让我们把它们配置成一个集群
首先执行以下命令将默认生成的单点配置文件备份
cp /root/zookeeper/node-1/conf/zoo.cfg /root/zookeeper/node-1/conf/zooAlone.cfg
接着生成集群配置文件
echo 'dataDir=/data
dataLogDir=/datalog
tickTime=2000
initLimit=5
syncLimit=2
autopurge.snapRetainCount=3
autopurge.purgeInterval=0
maxClientCnxns=60
clientPort=2181
admin.enableServer=true
server.1=zookeeper:2888:3888
server.2=zookeeper-2:2888:3888
server.3=zookeeper-3:2888:3888' > /root/zookeeper/node-1/conf/zooCluster.cfg
使用集群配置文件覆写
cp /root/zookeeper/node-1/conf/zooCluster.cfg /root/zookeeper/node-1/conf/zoo.cfg
这样我们就有了三个文件zoo.cfg,zooCluster.cfg和zooAlone.cfg,zookeeper启动时会读取的是zoo.cfg文件,我们可以根据启动场景使用另外两个配置文件灵活覆写zoo.cfg
因为已经有一个备份配置文件了,并且单点启动的话只需要启动结点1即可,结点2和3的配置文件可以不进行备份直接覆写
echo 'dataDir=/data
dataLogDir=/datalog
tickTime=2000
initLimit=5
syncLimit=2
autopurge.snapRetainCount=3
autopurge.purgeInterval=0
maxClientCnxns=60
clientPort=2181
admin.enableServer=true
server.1=zookeeper:2888:3888
server.2=zookeeper-2:2888:3888
server.3=zookeeper-3:2888:3888' > /root/zookeeper/node-2/conf/zoo.cfg
echo 'dataDir=/data
dataLogDir=/datalog
tickTime=2000
initLimit=5
syncLimit=2
autopurge.snapRetainCount=3
autopurge.purgeInterval=0
maxClientCnxns=60
clientPort=2181
admin.enableServer=true
server.1=zookeeper:2888:3888
server.2=zookeeper-2:2888:3888
server.3=zookeeper-3:2888:3888' > /root/zookeeper/node-2/conf/zoo.cfg
【重要】
我们需要编辑/root/zookeeper/node-1/data/myid文件
vim /root/zookeeper/node-1/data/myid
结点1一般默认内容都是1,不用动即可
1
这个myid是zookeeper集群内部的身份标识,必须唯一才能启动成功
同理需要保证结点2,结点三的myid在集群内部唯一
vim /root/zookeeper/node-2/data/myid
2
vim /root/zookeeper/node-3/data/myid
3
重启容器加载配置文件
docker restart zookeeper
docker restart zookeeper-2
docker restart zookeeper-3
3. Linux脚本实现快速切换启动关闭
因为作者的云服务器资源经常不够,虽然学习过程中常常需要集群环境进行选举机制等研究,但是实际使用环境单点部署完全够用了,所以作者需要常常面临单点部署和集群部署模式的切换。对此,作者使用了编写Linux脚本的方式进行快速管理
单点管理脚本
vim /root/zookeeper/zookeeperAlone.sh
NODES=("zookeeper")# 检查是否提供了正确的参数
if [ $# -ne 1 ]; thenecho "Usage: $0 {start|stop|restart}"exit 1
fi# 获取操作类型
OPERATION=$1# 检查操作类型是否有效
if [[ "$OPERATION" != "start" && "$OPERATION" != "stop" && "$OPERATION" != "restart" ]]; thenecho "Invalid argument. Usage: $0 {start|stop|restart}"exit 1
fi# 覆写 zoo.cfg 文件
ZOO_CLUSTER_CFG="/root/zookeeper/node-1/conf/zooAlone.cfg"
ZOO_CFG="/root/zookeeper/node-1/conf/zoo.cfg"if [ -f "$ZOO_CLUSTER_CFG" ]; thenecho "Copying $ZOO_CLUSTER_CFG to $ZOO_CFG..."cp "$ZOO_CLUSTER_CFG" "$ZOO_CFG"if [ $? -eq 0 ]; thenecho "Configuration file updated successfully."elseecho "Failed to update configuration file."exit 1fi
elseecho "zooCluster.cfg not found in /root/zookeeper/node-1/conf."exit 1
fi# 执行操作
for node in "${NODES[@]}"; doecho "$OPERATION $node..."docker "$OPERATION" "$node"if [ $? -eq 0 ]; thenecho "$node $OPERATION successfully."elseecho "Failed to $OPERATION $node."fi
done
集群管理脚本
vim /root/zookeeper/zookeeperCluster.sh
#!/bin/bash# 定义节点列表
NODES=("zookeeper" "zookeeper-2" "zookeeper-3")# 检查是否提供了正确的参数
if [ $# -ne 1 ]; thenecho "Usage: $0 {start|stop|restart}"exit 1
fi# 获取操作类型
OPERATION=$1# 检查操作类型是否有效
if [[ "$OPERATION" != "start" && "$OPERATION" != "stop" && "$OPERATION" != "restart" ]]; thenecho "Invalid argument. Usage: $0 {start|stop|restart}"exit 1
fi# 覆写 zoo.cfg 文件
ZOO_CLUSTER_CFG="/root/zookeeper/node-1/conf/zooCluster.cfg"
ZOO_CFG="/root/zookeeper/node-1/conf/zoo.cfg"if [ -f "$ZOO_CLUSTER_CFG" ]; thenecho "Copying $ZOO_CLUSTER_CFG to $ZOO_CFG..."cp "$ZOO_CLUSTER_CFG" "$ZOO_CFG"if [ $? -eq 0 ]; thenecho "Configuration file updated successfully."elseecho "Failed to update configuration file."exit 1fi
elseecho "zooCluster.cfg not found in /root/zookeeper/node-1/conf."exit 1
fi# 执行操作
for node in "${NODES[@]}"; doecho "$OPERATION $node..."docker "$OPERATION" "$node"if [ $? -eq 0 ]; thenecho "$node $OPERATION successfully."elseecho "Failed to $OPERATION $node."fi
done
添加可执行权限
chmod +x /root/zookeeper/zookeeperAlone.sh
chmod +x /root/zookeeper/zookeeperCluster.sh
使用方式:
单节点
#开启
#关闭
#重启
./root/zookeeper/zookeeperAlone.sh start
./root/zookeeper/zookeeperAlone.sh stop
./root/zookeeper/zookeeperAlone.sh restart
集群
#开启
#关闭
#重启
./root/zookeeper/zookeeperCluster.sh start
./root/zookeeper/zookeeperCluster.sh stop
./root/zookeeper/zookeeperCluster.sh restart
读者也可以自己修改脚本内容尝试添加更多参数命令