zookeeper集群的搭建

阅读声明:以下内容是结合网上材料及工作所写的个人理解,如有不当,欢迎大家指正~~~谢谢啦

一、准备工作

  由于在分布式环境中,单台ZooKeeper存在单点故障问题,所以我们必须搭建ZooKeeper集群,记录一下个人搭建记录。

  

 

①准备3个节点

  我这里选用三个虚拟机:

  192.168.144.130

  192.168.144.132

  192.168.144.133

②获取zookeeper的安装包

  wget  https://archive.apache.org/dist/zookeeper/zookeeper-3.4.7/zookeeper-3.4.7.tar.gz

二、集群搭建

  选择其中一个节点,执行以下步骤:

①解压安装包

    tar -xvf zookeeper-3.4.7.tar.gz

②在zookeeper的根目录

    mkdir data(用于存放数据)

③进入zookeeper的conf目录

    cp zoo_sample.cfg zoo.cfg(拷贝配置模版)

④编辑配置文件

  vim zoo.cfg,如下图所示。为防止图片失效,代码也粘上。

      

# 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.
dataDir=/home/software/zookeeper-3.4.7/data
# 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.1=192.168.144.132:2888:3888
server.2=192.168.144.133:2888:3888
server.3=192.168.144.130:2888:3888

⑤进入data目录

    cd /home/softwares/zookeeper-3.4.7/data:

    vim  myid

    1(数字1,要与zoo.cfg配置文件中数字保持一致,该id指定机器id在启动时用于选举使用。)

⑥通过scp命令拷贝

    将本机的zookeeper的整个目录拷贝至另外两个节点:

    scp -r zookeeper-3.4.7 root@192.168.144.133:/home/software/zookeeper-3.4.7

⑦修改myid

    修改对应myid与zoo.cfg中的id和ip对应即可完成集群搭建。

⑧启动服务  

    依次启动每个zookeeper:cd /home/softwares/zookeeper-3.4.7/sbin,sh zkServer.sh start

⑨查看节点状态

    cd /home/softwares/zookeeper-3.4.7/sbin,sh zkServer.sh status

   如图zookeeper的搭建成功~

   192.168.144.130节点:follower角色

   

   192.168.144.133节点:leader角色

   

   192.168.144.132节点:follower角色

    

三、总结  

①启动失败

    原因:zoo.cfg配置出错,检查dataDir的配置路径是否正确。

②检查server.*

    所对应的数字是否和后边的ip对应,如下图所示。

    

  以上是我搭建zookeeper所遇到的问题,希望能帮助自己和他人,如果有问题,欢迎大家指正~。接下来就是讨论zookeeper的选举机制和ZAB协议,研究一下,多学一点,生活更多彩一些。

原文地址:https://www.cnblogs.com/rmxd/p/11244001.html