zookeeper伪分布式配置安装

本文详细介绍了一种在单机上实现Zookeeper伪分布式部署的方法,包括安装步骤、配置文件详解及常见问题解决。通过该方法,可以更好地理解Zookeeper的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自己在学习实验zookeeper的时候通常都会是用一台机器进行伪分布式的实验,本篇文章主要是介绍安装步骤,其实安装部署特别简单但是还是存在一点坑的。

  1. 创建文件夹我的目录结果是:
    [root@VM_71_178_centos zookeeper]# pwd
    /usr/zookeeper
  • 首先创建需要用到的文件夹(s1,s2,s3),
    [root@VM_71_178_centos zookeeper]# ls
    s1  s2  s3  zk  zookeeper-3.4.10.tar.gz

    (这里注意一点,zookeeper一般情况下部署都是采用奇数,为什么要奇数下面会介绍到)
  • 下载zookeeper(wget  http://apache.org/dist/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz)

  • 解压
    tar -cvzf zookeeper-3.4.9.tar.gz -C zk 

  • 进入zk/zookeeper-3.4.9/conf/文件夹中重命名zook.simple.cfg 为zook.cfg
  • 将文件夹zookeeper-3.4.9复制三份分别到目录s1,s2,s3中
  • 在s1,s2,s3中分别创建文件夹
    [root@VM_71_178_centos s1]# ls
    zkdata  zklog  zookeeper-3.4.10

  • 分别修改s1,s2,s3中的zook.cfg配置文件。修改后内容如下:
  • s1/zookeeper-3.4.10/conf/zook.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=/usr/zookeeper/s1/zkdata
    dataLogDir=/usr/zookeeper/s1/zklog
    # 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=localhost:2287:3387
    server.2=localhost:2288:3388
    server.3=localhost:2289:3389


  • s2/zookeeper-3.4.10/conf/zook.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=/usr/zookeeper/s2/zkdata
    dataLogDir=/usr/zookeeper/s2/zklog
    # the port at which the clients will connect
    clientPort=2182
    # 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=localhost:2287:3387
    server.2=localhost:2288:3388
    server.3=localhost:2289:3389
  • s3/zookeeper-3.4.10/conf/zook.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=/usr/zookeeper/s3/zkdata
    dataLogDir=/usr/zookeeper/s3/zklog
    # the port at which the clients will connect
    clientPort=2183
    # 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=localhost:2287:3387
    server.2=localhost:2288:3388
    server.3=localhost:2289:3389

    
    
    
    
    
    
      配置文件解释
      • tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
      • dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
      • clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
      • initLimit:这个配置项是用来配置 Zookeeper 接受客户端(这里所说的客户端不是用户连接 Zookeeper 服务器的客户端,而是 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数。当已经超过 10 个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 5*2000=10 秒
      • syncLimit:这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 2*2000=4 秒
      • server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号。

      除了修改 zoo.cfg 配置文件,集群模式下还要配置一个文件 myid,这个文件在 dataDir 目录下,这个文件里面就有一个数据就是 A 的值,Zookeeper 启动时会读取这个文件,拿到里面的数据与 zoo.cfg 里面的配置信息比较从而判断到底是那个 server。

      1. 分别在文件夹zkdata中创建文件myid文件内容为server.?=localhost:2287:3387中的?如下:s1文件夹下vi /usr/zookeeper/s1/zkdata/myid
      2. 1
        
      3. 分别进入相应zookeeper中bin目录分别启动
      4. [root@VM_71_178_centos bin]# ./zkServer.sh  start
      5. 查询状态是否成功
      6. [root@VM_71_178_centos bin]# ./zkServer.sh status
        ZooKeeper JMX enabled by default
        Using config: /usr/zookeeper/s1/zookeeper-3.4.10/bin/../conf/zoo.cfg
        Mode: follower
        


          几点问题:1。zook.cfg中dataDir路径一定要正确否则启动不成功,2.myid中一定要跟server.几对应否则也不成功
          为啥需要奇数机器:
          官网强烈建议部署zookeeper机器的数量是奇数的,原因在于ZooKeeper的选举机制,选票超过半数的机器才有可能成为Leader,因此当整个集群中只有两台服务器或者整个集群超过半数的机器都挂掉时,是无法选举出Leader的.其实这是一个集群资源利用最大化的问题,例如集群中有三个节点,允许挂掉一个节点,这时还剩余两个节点,超过半数;而集群中有四个节点时,最多也只能挂掉一个节点,因为如果再挂掉一个,整个集群就剩下两个节点,无法满足超过集群数的半数这个条件.综合来看两者的容灾能力是一样的


          
          
          <think>嗯,用户想了解ZooKeeper伪分布式安装教程和配置指南。首先,我需要回忆一下ZooKeeper的伪集群模式是怎么回事。根据用户提供的引用[1],伪集群模式是在单台机器上运行多个ZooKeeper实例,每个实例有不同的配置和端口。那安装步骤大致包括准备环境、下载安装包、配置每个实例、创建数据和日志目录、设置myid文件、启动服务以及验证集群状态。 首先,用户可能需要确认已经安装了Java环境,因为ZooKeeper依赖Java。然后,下载并解压ZooKeeper安装包。接下来是配置文件的修改,每个实例需要不同的端口号,比如客户端端口、服务器间通信端口等。引用[1]提到每个实例要有自己的配置文件,指定不同的端口和数据存储位置。例如,第一个实例的配置文件中clientPort是2181,而第二个可能是2182,依此类推。 然后,需要为每个实例创建数据目录和日志目录,并在数据目录下创建myid文件,写入对应的服务器ID。这样每个实例才能正确识别自己在集群中的角色。启动服务时,可能需要分别指定不同的配置文件,所以启动命令要指向各自的conf文件。 验证部分,用户可以通过客户端连接不同的端口,使用命令如stat来查看集群状态,确认各个节点是否正确加入。另外,引用[2]提到了监控配置,用户可能还需要配置监控工具,比如Prometheus或Telegraf,但用户的问题主要是安装配置,这部分可以作为扩展建议。 用户提供的引用[3]提到了MapReduce和Yarn的配置,可能和Hadoop相关,但这里的问题是关于ZooKeeper的,暂时不需要涉及。不过需要注意,如果在Hadoop环境中使用ZooKeeper,可能需要额外的集成步骤,但用户没有提到这点,所以暂时忽略。 在编写步骤时,需要详细说明每个配置项的作用,比如tickTime、initLimit、syncLimit,以及server.X的配置。这些参数影响集群的同步和通信,正确设置对集群的稳定性很重要。同时,要提醒用户确保端口没有被占用,防火墙设置正确,避免通信问题。 常见问题部分需要考虑端口冲突、myid文件未创建或错误、防火墙阻止通信等。这些都是安装过程中容易出错的地方,提前提示可以帮助用户避免问题。另外,监控配置可以作为可选步骤,提升用户对集群运行状态的掌握。 总结一下,回答的结构应该是先介绍伪分布式模式的概念,然后分步骤详细说明安装配置过程,包括环境准备、下载安装配置实例、创建目录、启动服务、验证状态,最后给出常见问题解决和扩展建议。同时,引用相关的站内资料,比如引用[1]来解释伪集群模式的定义,引用[2]来建议监控配置,但用户当前的问题可能不需要深入监控部分,所以作为可选内容提及即可。</think>### ZooKeeper伪分布式模式安装指南 #### 一、伪分布式模式概念 伪分布式模式指在**单台机器**上通过不同端口启动多个ZooKeeper服务实例,模拟真实集群环境。每个实例拥有独立配置文件、数据目录和通信端口[^1]。 --- #### 二、安装步骤 1. **环境准备** - 操作系统:Linux/Unix(推荐)或Windows(需调整路径) - 依赖环境:安装JDK 8+,配置`JAVA_HOME` - 下载ZooKeeper安装包(建议版本3.4+) 2. **解压安装包** ```bash tar -zxvf apache-zookeeper-3.8.1-bin.tar.gz mv apache-zookeeper-3.8.1-bin /opt/zookeeper ``` 3. **配置多实例** - 创建3个实例目录: ```bash mkdir -p /opt/zookeeper/{node1,node2,node3} ``` - 每个实例配置`zoo.cfg`(以node1为例): ```properties # node1/conf/zoo.cfg tickTime=2000 initLimit=10 syncLimit=5 dataDir=/opt/zookeeper/node1/data dataLogDir=/opt/zookeeper/node1/logs clientPort=2181 server.1=127.0.0.1:2888:3888 server.2=127.0.0.1:2889:3889 server.3=127.0.0.1:2890:3890 ``` - `clientPort`:客户端连接端口(不同实例需唯一) - `server.X`:集群节点定义,格式为`IP:数据同步端口:选举端口` 4. **创建数据目录与myid** ```bash # node1 mkdir -p /opt/zookeeper/node1/{data,logs} echo 1 > /opt/zookeeper/node1/data/myid # node2/node3同理,分别写入2和3 ``` 5. **启动服务** ```bash # 分别启动三个实例(指定不同配置文件) bin/zkServer.sh start /opt/zookeeper/node1/conf/zoo.cfg bin/zkServer.sh start /opt/zookeeper/node2/conf/zoo.cfg bin/zkServer.sh start /opt/zookeeper/node3/conf/zoo.cfg ``` 6. **验证集群状态** ```bash echo stat | nc 127.0.0.1 2181 # 查看node1状态 echo stat | nc 127.0.0.1 2182 # 查看node2状态 ``` 输出应包含`Mode: follower`或`Mode: leader`。 --- #### 三、关键配置说明 | 参数 | 作用 | 示例值 | |------|------|--------| | `tickTime` | 基础时间单元(毫秒) | 2000 | | `initLimit` | 初始化连接超时(tick倍数) | 10 | | `syncLimit` | 心跳检测超时(tick倍数) | 5 | | `dataDir` | 数据快照存储路径 | /opt/zookeeper/node1/data | | `clientPort` | 客户端连接端口 | 2181/2182/2183 | | `server.X` | 集群节点定义 | 127.0.0.1:2888:3888 | --- #### 四、常见问题 1. **端口冲突** 检查`clientPort`、`server.X`中端口是否被占用 ```bash netstat -tuln | grep 2181 ``` 2. **myid文件错误** 确保`dataDir`目录下`myid`文件内容与`server.X`的ID一致 3. **防火墙限制** 伪分布式模式下仍需允许本地端口通信 --- #### 五、扩展配置(可选) 参考引用[2]的监控配置: ```properties # 添加至zoo.cfg metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider metricsProvider.httpPort=7000 metricsProvider.exportJvmInfo=true ``` ---
          评论
          添加红包

          请填写红包祝福语或标题

          红包个数最小为10个

          红包金额最低5元

          当前余额3.43前往充值 >
          需支付:10.00
          成就一亿技术人!
          领取后你会自动成为博主和红包主的粉丝 规则
          hope_wisdom
          发出的红包
          实付
          使用余额支付
          点击重新获取
          扫码支付
          钱包余额 0

          抵扣说明:

          1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
          2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

          余额充值