ZooKeeper集群

系统版本:CentOS Linux release 8.3.2011

JDK版本:1.8

Zookpeer版本:3.7.0

此次搭建的集群为伪集群,即在一台机器上开3个实例。

  1. 下载安装包

    [root@i-h0xe1oiq tools]# wget https://dlcdn.apache.org/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz
    
  2. 解压安装包

    [root@i-h0xe1oiq tools]# tar xf apache-zookeeper-3.7.0-bin.tar.gz -C /application/
    
  3. 查看解压后的目录

    [root@i-h0xe1oiq apache-zookeeper-3.7.0-bin]# tree -L 1
    .
    ├── bin
    ├── conf
    ├── docs
    ├── lib
    ├── LICENSE.txt
    ├── NOTICE.txt
    ├── README.md
    └── README_packaging.md
    
    4 directories, 4 files
    
  4. 准备配置文件,进入conf目录,创建zoo.cfg文件,内容可复制conf/zoo_sample.cfg。

    [root@i-h0xe1oiq conf]# cp zoo_sample.cfg zoo.cfg
    
  5. 配置文件说明

    # The number of milliseconds of each tick
    tickTime=2000
    # CS通信心跳数,Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。tickTime以毫秒为单位。
    # The number of ticks that the initial 
    # synchronization phase can take
    initLimit=10
    # LF初始通信时限,集群中的follower服务器(F)与leader服务器(L)之间初始连接时能容忍的最多心跳数(tickTime的数量)。
    # The number of ticks that can pass between 
    # sending a request and getting an acknowledgement
    syncLimit=5
    # LF同步通信时限,集群中的follower服务器与leader服务器之间请求和应答之间能容忍的最多心跳数(tickTime的数量)。
    # the directory where the snapshot is stored.
    # do not use /tmp for storage, /tmp here is just 
    # example sakes.
    dataDir=/tmp/zookeeper
    # 数据文件目录,Zookeeper保存数据的目录,默认情况下,Zookeeper将写数据的日志文件也保存在这个目录里。
    # the port at which the clients will connect
    clientPort=2181
    # 客户端连接端口,客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
    # 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
    
    ## Metrics Providers
    #
    # https://prometheus.io Metrics Exporter
    #metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
    #metricsProvider.httpPort=7000
    #metricsProvider.exportJvmInfo=true
    
    # 服务器名称与地址:集群信息(服务器编号,服务器地址,LF通信端口,选举端口)
    # 这个配置项的书写格式比较特殊,规则如下:
    
    # server.N=YYY:A:B  
    
    # 其中N表示服务器编号,YYY表示服务器的IP地址,A为LF通信端口,表示该服务器与集群中的leader交换的信息的端口。B为选举端口,表示选举新leader时服务器间相互通信的端口(当leader挂掉时,其余服务器会相互通信,选择出新的leader)。一般来说,集群中每个服务器的A端口都是一样,每个服务器的B端口也是一样。但是当所采用的为伪集群时,IP地址都一样,只能时A端口和B端口不一样。
    
  6. 伪集群准备三份zookeeper实例

    [root@i-h0xe1oiq application]# ll -h
    total 12K
    drwxr-xr-x 17 cloud-user cloud-user 4.0K Mar 17  2021 apache-zookeeper-3.7.0-2181
    drwxr-xr-x 17 root       root       4.0K Oct 25 14:54 apache-zookeeper-3.7.0-2182
    drwxr-xr-x 17 root       root       4.0K Oct 25 14:54 apache-zookeeper-3.7.0-2183
    
  7. 各实例配置文件

    # 实例1
    tickTime=2000
    initLimit=10
    syncLimit=5
    dataDir=/application/apache-zookeeper-3.7.0-2181/data
    clientPort=2181
    server.1=127.0.0.1:12888:13888
    server.2=127.0.0.1:14888:15888
    server.3=127.0.0.1:16888:17888
    
    # 实例2
    tickTime=2000
    initLimit=10
    syncLimit=5
    dataDir=/application/apache-zookeeper-3.7.0-2182/data
    clientPort=2182
    server.1=127.0.0.1:12888:13888
    server.2=127.0.0.1:14888:15888
    server.3=127.0.0.1:16888:17888
    
    # 实例3
    tickTime=2000
    initLimit=10
    syncLimit=5
    dataDir=/application/apache-zookeeper-3.7.0-2183/data
    clientPort=2183
    server.1=127.0.0.1:12888:13888
    server.2=127.0.0.1:14888:15888
    server.3=127.0.0.1:16888:17888
    
  8. 配置实例ID以区分

    # 实例1
    [root@i-h0xe1oiq application]# mkdir -p apache-zookeeper-3.7.0-2181/data/ && echo 1 > apache-zookeeper-3.7.0-2181/data/myid
    
    # 实例2
    [root@i-h0xe1oiq application]# mkdir -p apache-zookeeper-3.7.0-2182/data/ && echo 2 > apache-zookeeper-3.7.0-2182/data/myid
    
    # 实例3
    [root@i-h0xe1oiq application]# mkdir -p apache-zookeeper-3.7.0-2183/data/ && echo 3 > apache-zookeeper-3.7.0-2183/data/myid
    
  9. 启动节点

    [root@i-h0xe1oiq application]# ./apache-zookeeper-3.7.0-2181/bin/zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /application/apache-zookeeper-3.7.0-2181/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    [root@i-h0xe1oiq application]# ./apache-zookeeper-3.7.0-2182/bin/zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /application/apache-zookeeper-3.7.0-2182/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    [root@i-h0xe1oiq application]# ./apache-zookeeper-3.7.0-2183/bin/zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /application/apache-zookeeper-3.7.0-2183/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    
  10. 查看节点状态

    [root@i-h0xe1oiq application]# ./apache-zookeeper-3.7.0-2181/bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /application/apache-zookeeper-3.7.0-2181/bin/../conf/zoo.cfg
    Client port found: 2181. Client address: localhost. Client SSL: false.
    Mode: follower
    [root@i-h0xe1oiq application]# ./apache-zookeeper-3.7.0-2182/bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /application/apache-zookeeper-3.7.0-2182/bin/../conf/zoo.cfg
    Client port found: 2182. Client address: localhost. Client SSL: false.
    Mode: leader
    [root@i-h0xe1oiq application]# ./apache-zookeeper-3.7.0-2183/bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /application/apache-zookeeper-3.7.0-2183/bin/../conf/zoo.cfg
    Client port found: 2183. Client address: localhost. Client SSL: false.
    Mode: follower
    
原文地址:https://www.cnblogs.com/os-linux/p/15459146.html