Single machine or distributed installation
Decompress zookeeper to the corresponding paths on different machines
For stand-alone installation, change the for loop to the IP of your own host
#!/bin/bash
zootmp=/root/liutao/zookeeper/tmp # zk的目录下的数据目录,需要自己创建,此路径用于存储zookeeper中数据的内存快照、及事物日志文件
zoocfg=/root/liutao/zookeeper/conf/zoo.cfg # zk目录下的配置文件的路径
myid=1
for h7_node in h71 h72 h73;do
ssh -Tq root@$h7_node << EOF
echo $myid
if test -e $zootmp
then
rm -rf $zootmp
mkdir $zootmp
touch $zootmp/myid
echo $myid >> ~/liutao/zookeeper/tmp/myid // zk主机号
else
mkdir $zootmp
touch $zootmp/myid
echo $myid >> ~/liutao/zookeeper/tmp/myid // // zk主机号
fi
if test -e $zoocfg
then
echo "tickTime=2000" > $zoocfg
echo "initLimit=10" >> $zoocfg
echo "syncLimit=5" >> $zoocfg
echo "dataDir=/root/liutao/zookeeper/tmp" >> $zoocfg
echo "clientPort=2181" >> $zoocfg // 2181:对cline端提供服务
echo "server.1=h71:2888:3888" >> $zoocfg // 3888:选举leader使用 , 2888:集群内机器通讯使用(Leader监听此端口)
echo "server.2=h72:2888:3888" >> $zoocfg
echo "server.3=h73:2888:3888" >> $zoocfg
else
touch $zoocfg
echo "tickTime=2000" > $zoocfg
echo "initLimit=10" >> $zoocfg
echo "syncLimit=5" >> $zoocfg
echo "dataDir=/root/liutao/zookeeper/tmp" >> $zoocfg
echo "clientPort=2181" >> $zoocfg
echo "server.1=h71:2888:3888" >> $zoocfg
echo "server.2=h72:2888:3888" >> $zoocfg
echo "server.3=h73:2888:3888" >> $zoocfg
fi
exit
EOF
myid=`expr $myid + 1`
done
Environment variable
export ZOOKEEPER_HOME=/root/liutao/zookeeper
export PATH=$PATH:$ZOOKEEPER_HOME/bin
Common commands
start stop
// 启动zookeeper zkServer.sh start
// 停止zookeeper zkServer.sh stop
// 查看状态:zkServer.sh status
Create node
zkCli.sh 链接本地zk服务器,退出使用quit
create [-s] [-e] path data #其中-s 为有序节点,-e 临时节点,-s会记录每个子节点创建的先后顺序
Create a persistent node and write data, get command to get data
Persistent ordered nodes and write data, the node name will be changed to a combination of name and number, the role is to create a unique ID
After the temporary node is created, quit exits the session, the node is automatically deleted, and the temporary node cannot have child nodes.
Update node
The version number will be updated after the node is updated
zkCli.sh 链接本地zk服务器,退出使用quit
set 路径 更新的数据 【当前dataversion,可选】 #
Delete node
delete path 【当前dataversion,可选】 # 结点有子结点,是不允许删除的
rmr path # 删除该结点以及所有的子结点
View node
get path # 返回数据和当前结点的属性
stat path # 返回当前结点属性
Property description:
Return to node list
ls path # 返回当前路径下的所有子结点
ls2 path # 返回当前路径下的所有子结点和当前子结点的属性