Linux Zookeeper集群安装

由于只有一台电脑,所以搭建一个伪集群(伪集群就是在一台电脑上模拟搭建集群,走不同端口启动,真实的情况在每台机器上搭建一个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
    
    •  

 

相关文章

#一、什么是ZooKeeper**ZooKeeper是一个分布式服务协调框架*...
2.ZooKeeper介绍2.1.ZooKeeper由来正式介绍ZooKeeper之前,我...
Zookeeper概述1.ZooKeeper最为主要的使⽤场景,是作为分布式...
环境:1.VMware®Workstation12Pro 2.CentOS7 3.zookeeper...
###1\.面试官:工作中使用过Zookeeper嘛?你知道它是什么...
##2\.ZooKeeper介绍###2.1\.ZooKeeper由来正式介绍Z...