Zookeeper 基础使用

Zookeeper 基础使用

Lan Yu Lv2

Zookeeper 基础使用

安装

zookeeper集群的每个节点zoo.cfg文件都是相同的,每个节点的环境变量也是相同的,所以下列操作在三台主机上都要进行。kafka及zookeeper都使用普通用户部署,本次使用的用户为kafka

​ 资源划分如下:

序号 主机名 机器IP
1 open0004 10.0.0.208
2 open0005 10.0.0.222
3 open0006 10.0.0.232

下载并上传

  1. 下载最新压缩包zookeeper-3.4.13.tar.gz,其代码仓库地址:https://github.com/apache/zookeeper

  2. 使用非root用户即可安装,上传至用户预安装的目录下进行解压,三个节点进行相同的操作:

    1
    tar -xzvf zookeeper-3.4.13.tar.gz

启动前配置

jdk 环境准备

  启动前需要修改环境变量,步骤如下:

  1. 此前应先准备好jdk,此处简单说明,使用jdk-8u144-linux-x64.tar.gz,传至对应目录解压:

    1
    tar -xzvf jdk-8u144-linux-x64.tar.gz
  2. 配置属主用户的.bash_profile文件,该文件属于隐藏文件,业务用户在家目录ls -a 可看到该文件,修改结果如下:

    1
    export PATH=/home/kafka/jdk1.8.0_144/bin:$PATH:$HOME/.local/bin:$HOME/bin
    • /home/kafka/jdk1.8.0_144/bin::新增内容,首先寻找此目录下的命令,即有限使用此jdk环境

  至此,jdk配置完毕,重新登录当前用户使用java -version可正常使用命令并查看对应版本。

zookeeper 准备

  1. 修改.bash_profile文件,配置文件里增加这几条,修改环境变量结果如下:

    1
    2
    ZOOKEEPER_HOME=/home/kafka/zookeeper/zookeeper-3.4.13 
    PATH=/home/kafka/jdk1.8.0_144/bin:$PATH:$ZOOKEEPER_HOME/bin:$HOME/.local/bin:$HOME/bin
  2. 进入/home/kafka/zookeeper/zookeeper-3.4.13/conf目录,复制出对应的配置文件:

    1
    2
    3
    cd /home/kafka/zookeeper/conf
    cp zoo_sample.cfg zoo.cfg
    [techgroup@localhost bin]$ echo "0"> /data/soft/kafka/zookeeper_data/myid
  3. 修改配置文件的数据目录位置,并添加节点配置,最终文件修改项如下:(/home/kafka/zookeeper/conf/zoo.cfg)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    # 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 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/kafka/zookeeper/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.0=192.168.235.105:2888:3888
    server.1=192.168.235.113:2888:3888
    server.2=192.168.235.114:2888:3888
    • dataDir:存储内存中数据库快照的位置,顾名思义就是Zookeeper保存数据的目录,默认情况下,Zookeeper将写数据的日志文件也保存在这个目录里。
    • tickTime:基本事件单元,以毫秒为单位。这个时间是作为Zookeeper服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每隔tickTime时间就会发送一个心跳。
    • clientPort:这个端口就是客户端连接Zookeeper服务器的端口,Zookeeper会监听这个端口,接受客户端的访问请求。
    • initLimit:这个配置项是用来配置Zookeeper接受客户端初始化连接时最长能忍受多少个心跳时间间隔数,当已经超过10个心跳的时间(也就是tickTime)长度后Zookeeper服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是10*2000=20秒。
    • syncLimit:这个配置项标识Leader与Follower之间发送消息,请求和应答时间长度,最长不能超过多少个tickTime的时间长度,总的时间长度就是5*2000=10秒
    • server.A = B:C:D:A表示这个是第几号服务器;B是这个服务器的ip地址;C表示的是这个服务器与集群中的Leader服务器交换信息的端口;D表示的是万一集群中的Leader服务器挂了,需要一个端口来重新进行选举,选出一个新的Leader。
  4. 上述配置,在三个节点均需要做处理

  5. 三个节点均在数据目录/home/kafka/zookeeper/data下,创建文件myid,内容为对应ID,即0、1、2。

应用启动

  由于环境变量已经配置完毕,可以直接在任意目录执行zkServer.sh start启动,并可以使用zkServer.sh status查看状态,效果如下:

1
2
3
4
5
6
7
8
[kafka@open0005 ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/kafka/zookeeper/zookeeper-3.4.13/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[kafka@open0005 ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/kafka/zookeeper/bin/../conf/zoo.cfg
Mode: follower

客户端使用

  1. 任一节点下使用zkCli.sh命令即可进入控制台。//退出quit
  • 标题: Zookeeper 基础使用
  • 作者: Lan Yu
  • 创建于 : 2024-07-10 21:39:22
  • 更新于 : 2026-10-28 12:12:48
  • 链接: https://blog.lanyu.dev/2024/07/10/Zookeeper 基础使用/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论