Ubuntu18.04 搭建zookeeper单机版集群

时间:2021-05-24 15:26:48

一台电脑启动三个虚拟机比较折腾,这里就用一台虚拟机模拟一下zk集群。

1.后台下载安装包到 /opt目录

sudo wget -b http://archive.apache.org/dist/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz \
-o /opt/archive/zookeeper.download.log -P /opt

解压、链接、配置环境变量

tar -zxf /opt/zookeeper-3.4.9.tar.gz -C /opt;
ln -s /opt/zookeeper-3.4.9 /opt/zk;
echo -e '\nZK_HOME=/opt/zk\nPATH=\$PATH:\$ZK_HOME\bin\n' >> /etc/profile;

2.创建数据及日志目录并修改权限

# 同时为创建3*2=6个目录
sudo mkdir /data/{zk1,zk2,zk3}/{data,logs}
# 修改权限为当前用户所有
sudo chown -R $USER:$GROUPS /data/{zk1,zk2,zk3}

3.复制并修改配置文件

cp /opt/zk/conf/zoo_sample.cfg /data/zk1/zoo.cfg

4.配置文件

# The number of milliseconds of each tick
# 心跳间隔
tickTime=2000 # The number of ticks that the initial
# synchronization phase can take
# 初始同步阶段的心跳间隔
initLimit=10 # The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5 # the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
# 存储快照的目录,不要使用/tmp目录
dataDir=/data/zk1/data
dataLogDir=/data/zk1/logs # the port at which the clients will connect
clientPort=2181 # the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1 # 心跳端口,数据端口;选举完成后,每个节点打开数据端口;只有主节点打开心跳端口,接受心跳数据
# server.N 和 myid 是对应的
server.1=bogon:2888:3888
server.2=bogon:2889:3889
server.3=bogon:2890:3890

  

5.批量操作脚本

for zk in {1,2,3}
do
myid=/data/zk${zk}/data/myid
if [ ! -e "$myid" ];then
echo $zk > $myid
fi
zkServer.sh $1 /data/zk${zk}/zoo.cfg
done