zookeeper原理以及手动创建过程,见:https://blog.csdn.net/qiushisoftware/article/details/79043379
下面贴上我自己写的一键创建脚本:
在开始之前,请先下载zookeeper:https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/
备注下:开始之前请关了防火墙
- 准备三台机器:
192.168.182.204
192.168.182.205
192.168.182.206 - 机器系统:centos7
- 使用xshell5连接每台机器,必须用xshell5
- 分别在三台机器上新建一个shell脚本,命名为:zk.sh
- zk.sh内容如下,本教程用的是3.4.13版本,如果你用的版本不一样,批量替换脚本中的3.4.13为你自己的版本号:
#!/bin/bash
mkdir -p /opt/zookeeper
cd /opt/zookeeper/
rz
tar -xvzf zookeeper-3.4.13.tar.gz
echo -e "\033[1;41;33m zookeeper-3.4.13 upload success,if you want to reupload,please delete '/opt/zookeeper/zookeeper-3.4.13.tar.gz' first \033[0m"
cd /opt/zookeeper/zookeeper-3.4.13/conf && cp zoo_sample.cfg zoo.cfg
mkdir -p /opt/zookeeper/zookeeper-3.4.13/data
echo -e "\033[1;41;33m zookeeper-3.4.13 dataDir created \033[0m"
touch /opt/zookeeper/zookeeper-3.4.13/data/myid
val=($@)
for i in `seq 0 $((${#val[*]}-1))`
do
result=$(ifconfig | grep ${val[${i}]})
if [[ "$result" != "" ]]
then
echo "Param ${i} --- ${val[${i}]} is match;"
echo "${i}" > /opt/zookeeper/zookeeper-3.4.13/data/myid
echo -e "\033[1;41;33m zookeeper-3.4.13 myid created with "${i}" \033[0m"
fi
done
cat >/opt/zookeeper/zookeeper-3.4.13/conf/zoo.cfg <<EOF
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/opt/zookeeper/zookeeper-3.4.13/data
clientPort=2181
server.0 = $1:2888:3888
server.1 = $2:2888:3888
server.2 = $3:2888:3888
EOF
/opt/zookeeper/zookeeper-3.4.13/bin/zkServer.sh start
- 分别在三台机器上为脚本授权:chmod +x zk.sh
- 分别在三台机器上执行以下命令,执行过程中会弹框让你选择zookeeper安装包:
./zk.sh 192.168.182.204 192.168.182.205 192.168.182.206
-
启动成功截图如下:
-
验证集群是否安装成功,在每台机器上执行如下命令,会得到两个followerh和一个leader:
/opt/zookeeper/zookeeper-3.4.13/bin/zkServer.sh status
- 在zookeeper中任意一个节点,执行指令./zkCli.sh:
- 执行完zkCli.sh之后,执行以下命令,如图:
ls / 查找根目录
create /test test 创建节点并赋值
get /test 获取指定节点的值
- 在另一台机器上执行 ./zkCli.sh ,执行以下命令,获取到值说明集群成功:
get /test