Linux Zookeeper集群安装

本文介绍在单台电脑上搭建Zookeeper伪集群的方法。因只有一台电脑,需模拟搭建,要注意节点间通信不受防火墙或安全组规则阻碍。集群数量推荐奇数,遵循选举规则。还给出下载解压zookeeper包、配置文件及启动查看状态等步骤。

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

由于只有一台电脑,所以搭建一个伪集群(伪集群就是在一台电脑上模拟搭建集群,走不同端口启动,真实的情况在每台机器上搭建一个zookeeper或者每台机器两个zookeeper等),道理是一样的,只不过要注意别被防火墙或者安全组规则挡住了zookeeper节点间的通信,每个节点直接的网络要是通的。
集群数量推荐奇数,这是由于zookeeper的选举规则决定的,不懂先看zookeeper入门的文章,这里只是安装教程。

  1. 下载并解压zookeeper tar包

    https://apache.claz.org/zookeeper/zookeeper-3.6.2/apache-zookeeper-3.6.2-bin.tar.gz

    此处将包下载解压到了/usr/local/share/下

    #解压后的目录里的结构
    root@ubuntu:/usr/local/share/apache-zookeeper-3.6.2-bin# ls
    bin  conf  docs  lib  LICENSE.txt  logs  NOTICE.txt  README.md  README_packaging.md
    #bin目录
    root@ubuntu:/usr/local/share/apache-zookeeper-3.6.2-bin# cd bin
    root@ubuntu:/usr/local/share/apache-zookeeper-3.6.2-bin/bin# ls
    README.txt    zkCli.cmd  zkEnv.cmd  zkServer.cmd            zkServer.sh            zkSnapShotToolkit.sh  zkTxnLogToolkit.sh
    zkCleanup.sh  zkCli.sh   zkEnv.sh   zkServer-initialize.sh  zkSnapShotToolkit.cmd  zkTxnLogToolkit.cmd
    #conf目录
    root@ubuntu:/usr/local/share/apache-zookeeper-3.6.2-bin/conf# ls
    configuration.xsl  log4j.properties   zoo_sample.cfg
    #将bin目录添加到path去
    echo 'export PATH=$PATH:/usr/local/share/apache-zookeeper-3.6.2-bin/bin' >> /etc/profile
    #加载
    source /etc/profile
    
  2. 复制三份配置后面用三份不同配置启动三个zookeeper

    #进conf目录
    cd conf
    #编辑第一个配置文件(或者复制zoo_sample.cfg修改)
    vi zk1.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/local/zk1
    # 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
    
    ## Metrics Providers
    #
    # https://prometheus.io Metrics Exporter
    #metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
    #metricsProvider.httpPort=7000
    #metricsProvider.exportJvmInfo=true
    server.1=127.0.0.1:2981:2982
    server.2=127.0.0.1:2983:2984
    server.3=127.0.0.1:2985:2986
    
    • 然后保存退出
    • 然后复制两份,命名为zk2.cfg和zk3.cfg
    cp zk1.cfg zk2.cfg
    cp zk1.cfg zk3.cfg
    #然后编辑zk2.cfg修改dataDir为/usr/local/zk2  修改clientPort为2182
    #然后编辑zk3.cfg修改dataDir为/usr/local/zk3  修改clientPort为2183
    #均保存退出
    #创建这三个data目录
    mkdir /usr/local/zk1 /usr/local/zk2 /usr/local/zk3
    
  3. 启动三个zookeeper并查看状态

    #在conf目录下执行 root@ubuntu:/usr/local/share/apache-zookeeper-3.6.2-bin/conf
    zkServer.sh start zk1.cfg
    zkServer.sh start zk2.cfg
    zkServer.sh start zk3.cfg
    
    #查看三个zookeeper的状态,发现第二个是leader
    root@ubuntu:/usr/local/share/apache-zookeeper-3.6.2-bin/conf# zkServer.sh status zk1.cfg 
    /usr/bin/java
    ZooKeeper JMX enabled by default
    Using config: /usr/local/share/apache-zookeeper-3.6.2-bin/bin/../conf/zk1.cfg
    Client port found: 2181. Client address: localhost. Client SSL: false.
    Mode: follower
    root@ubuntu:/usr/local/share/apache-zookeeper-3.6.2-bin/conf# zkServer.sh status zk2.cfg 
    /usr/bin/java
    ZooKeeper JMX enabled by default
    Using config: /usr/local/share/apache-zookeeper-3.6.2-bin/bin/../conf/zk2.cfg
    Client port found: 2182. Client address: localhost. Client SSL: false.
    Mode: leader
    root@ubuntu:/usr/local/share/apache-zookeeper-3.6.2-bin/conf# zkServer.sh status zk3.cfg 
    /usr/bin/java
    ZooKeeper JMX enabled by default
    Using config: /usr/local/share/apache-zookeeper-3.6.2-bin/bin/../conf/zk3.cfg
    Client port found: 2183. Client address: localhost. Client SSL: false.
    Mode: follower
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HumorChen99

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值