一、集群规划

在3台虚拟机节点上安装ZooKeeper:

  1. cloud-01
  2. cloud-02
  3. cloud-03

    二、安装解压

    1.下载ZooKeeper

    apache镜像下载太慢,故选择清华镜像
    1. nzx@cloud-01:/opt/nzx/module/zookeeper-3.5.8$ wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.5.8/apache-zookeeper-3.5.8-bin.tar.gz

    2.解压安装

    nzx@cloud-01:/opt/nzx/module/zookeeper-3.5.8$ tar -zxvf apache-zookeeper-3.5.8-bin.tar.gz
    nzx@cloud-01:/opt/nzx/module/zookeeper-3.5.8$ mv apache-zookeeper-3.5.8-bin /opt/nzx/module/zookeeper-3.5.8
    nzx@cloud-01:/opt/nzx/module/zookeeper-3.5.8$ xsync /opt/nzx/module/zookeeper-3.5.8
    

    三、ZooKeeper配置

    1.配置服务器编号

    nzx@cloud-01:/opt/nzx/module/zookeeper-3.5.8$ pwd
    /opt/nzx/module/zookeeper-3.5.8
    nzx@cloud-01:/opt/nzx/module/zookeeper-3.5.8$ mkdir -p zkData
    nzx@cloud-01:/opt/nzx/module/zookeeper-3.5.8$ touch zkData/myid
    
    在myid文件中,cloud-01写入数字1,cloud-02写入数字2,cloud-03写入数字3

    2.配置zoo.cfg

    nzx@cloud-01:/opt/nzx/module/zookeeper-3.5.8/conf$ mv zoo_sample.cfg zoo.cfg
    
    修改zoo.cfg:
  • 修改存储路径设置:

dataDir=/opt/nzx/module/zookeeper-3.5.8/zkData

  • 添加集群配置信息:
######################cluster############################
server.1=cloud-01:2888:3888
server.2=cloud-02:2888:3888
server.3=cloud-03:2888:3888

server.A=B:C:D:

  • A:第几号服务器
  • B:服务器的IP地址
  • C:这个服务器与leader交换信息的端口
  • D:选举新的leader的通信端口

    四、启动ZooKeeper

    nzx@cloud-01:/opt/nzx/module/zookeeper-3.5.8$ bin/zkServer.sh start
    nzx@cloud-02:/opt/nzx/module/zookeeper-3.5.8$ bin/zkServer.sh start
    nzx@cloud-03:/opt/nzx/module/zookeeper-3.5.8$ bin/zkServer.sh start
    

    五、查看ZooKeeper状态

    nzx@cloud-01:/opt/nzx/module/zookeeper-3.5.8$ bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /opt/nzx/module/zookeeper-3.5.8/bin/../conf/zoo.cfg
    Client port found: 2181. Client address: localhost.
    Mode: follower
    
    nzx@cloud-02:/opt/nzx/module/zookeeper-3.5.8$ bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /opt/nzx/module/zookeeper-3.5.8/bin/../conf/zoo.cfg
    Client port found: 2181. Client address: localhost.
    Mode: leader
    
    nzx@cloud-03:/opt/nzx/module/zookeeper-3.5.8$ bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /opt/nzx/module/zookeeper-3.5.8/bin/../conf/zoo.cfg
    Client port found: 2181. Client address: localhost.
    Mode: follower