欢迎访问 SJsir 圣杰博客

A Linux operation and maintenance engineer's Blogs

Zookeeper集群安装

  ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,它是集群的管理者,监视着集群中各个节点的状态根据节点提交的反馈进行下一步合理操作。最终,将简单易用的接口和性能高效、功能稳定的系统提供给用户。
  集群模式有两种形式:
    1)使用多台机器,在每台机器上运行一个ZooKeeper Server进程;
    2)使用一台机器,在该台机器上运行多个ZooKeeper Server进程。
  在生产环境中,一般使用第一种形式,在练习环境中,一般使用第二种形式。

  1.依赖环境:

    Linux:Centos 7.5.1804
    Java:JDK 1.8.171
    Zookeeper:3.4.12

  2.安装JDK环境:

    (1)通过Oracle官网下载JDK的rpm包:
    http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
    (2)安装JDK环境:
    [root@localhost ~]# yum install ./jdk-8u171-linux-x64.rpm 
    Loaded plugins: fastestmirror, langpacks
    Examining ./jdk-8u171-linux-x64.rpm: 2000:jdk1.8-1.8.0_171-fcs.x86_64
    Marking ./jdk-8u171-linux-x64.rpm to be installed
    Resolving Dependencies
    --> Running transaction check
    ---> Package jdk1.8.x86_64 2000:1.8.0_171-fcs will be installed
    --> Finished Dependency Resolution
    epel/7/x86_64                                                                                            | 3.2 kB  00:00:00     
    epel/7/x86_64/updateinfo                                                                                 | 934 kB  00:00:00     
    epel/7/x86_64/primary                                                                                    | 3.5 MB  00:00:00     
    extras/7/x86_64                                                                                          | 3.4 kB  00:00:00     
    os/7/x86_64                                                                                              | 3.6 kB  00:00:00     
    updates/7/x86_64                                                                                         | 3.4 kB  00:00:00     

    Dependencies Resolved

    ================================================================================================================================
     Package                Arch                   Version                               Repository                            Size
    ================================================================================================================================
    Installing:
     jdk1.8                 x86_64                 2000:1.8.0_171-fcs                    /jdk-8u171-linux-x64                 279 M

    Transaction Summary
    ================================================================================================================================
    Install  1 Package

    Total size: 279 M
    Installed size: 279 M
    Is this ok [y/d/N]: y
    Downloading packages:
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Installing : 2000:jdk1.8-1.8.0_171-fcs.x86_64                                                                             1/1 
    Unpacking JAR files...
        tools.jar...
        plugin.jar...
        javaws.jar...
        deploy.jar...
        rt.jar...
        jsse.jar...
        charsets.jar...
        localedata.jar...
      Verifying  : 2000:jdk1.8-1.8.0_171-fcs.x86_64                                                                             1/1 

    Installed:
      jdk1.8.x86_64 2000:1.8.0_171-fcs

    Complete!
    (3)查看是否安装成功:
    [root@localhost ~]# java -version
    java version "1.8.0_171"
    Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
    Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)

  3.本机安装多个zookeeper实例:

    [root@localhost ~]# cd /software/
    [root@localhost software]# tar zxf /root/zookeeper-3.4.12.tar.gz
    [root@localhost software]# mv zookeeper-3.4.12/ zookeeper1
    [root@localhost software]# tar zxf /root/zookeeper-3.4.12.tar.gz
    [root@localhost software]# mv zookeeper-3.4.12/ zookeeper2
    [root@localhost software]# tar zxf /root/zookeeper-3.4.12.tar.gz
    [root@localhost software]# mv zookeeper-3.4.12/ zookeeper3
    [root@localhost software]# ll
    total 12
    drwxr-xr-x 10 centos centos 4096 Mar 27 12:36 zookeeper1
    drwxr-xr-x 10 centos centos 4096 Mar 27 12:36 zookeeper2
    drwxr-xr-x 10 centos centos 4096 Mar 27 12:36 zookeeper3

  4.修改三个zookeeper的配置文件:

    Server1:
    [root@localhost software]# cp zookeeper1/conf/zoo_sample.cfg zookeeper1/conf/zoo.cfg
    [root@localhost software]# vim zookeeper1/conf/zoo.cfg
    tickTime=2000
    initLimit=10
    syncLimit=5
    dataDir=/software/data/zookeeper1
    dataLogDir=/software/logs/zookeeper1
    clientPort=2181
    server.1=127.0.0.1:2200:2201
    server.2=127.0.0.1:2210:2211
    server.3=127.0.0.1:2220:2221

    Server2:
    [root@localhost software]# cp zookeeper2/conf/zoo_sample.cfg zookeeper1/conf/zoo.cfg
    [root@localhost software]# vim zookeeper2/conf/zoo.cfg
    tickTime=2000
    initLimit=10
    syncLimit=5
    dataDir=/software/data/zookeeper2
    dataLogDir=/software/logs/zookeeper2
    clientPort=2182
    server.1=127.0.0.1:2200:2201
    server.2=127.0.0.1:2210:2211
    server.3=127.0.0.1:2220:2221

    Server3:
    [root@localhost software]# cp zookeeper3/conf/zoo_sample.cfg zookeeper1/conf/zoo.cfg
    [root@localhost software]# vim zookeeper3/conf/zoo.cfg
    tickTime=2000
    initLimit=10
    syncLimit=5
    dataDir=/software/data/zookeeper3
    dataLogDir=/software/logs/zookeeper3
    clientPort=2183
    server.1=127.0.0.1:2200:2201
    server.2=127.0.0.1:2210:2211
    server.3=127.0.0.1:2220:2221

  4.添加myid文件:

    data目录下添加myid文件,用于存储一个数值,用来作为该ZooKeeper Server进程的标识。即上面配置中的NUM。
    [root@localhost software]# cat data/zookeeper1/myid 
    1
    [root@localhost software]# cat data/zookeeper2/myid 
    2
    [root@localhost software]# cat data/zookeeper3/myid 
    3

  5.启动zookeeper集群:

    [root@localhost software]# zookeeper1/bin/zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /software/zookeeper1/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    [root@localhost software]# zookeeper2/bin/zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /software/zookeeper2/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    [root@localhost software]# zookeeper3/bin/zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /software/zookeeper3/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    [root@localhost software]# ss -tnl
    State      Recv-Q Send-Q  Local Address:Port                 Peer Address:Port
    LISTEN     0      50          127.0.0.1:2210                            *:*
    LISTEN     0      50                  *:33730                           *:*
    LISTEN     0      50          127.0.0.1:2211                            *:*
    LISTEN     0      50                  *:2181                            *:*
    LISTEN     0      50                  *:2182                            *:*
    LISTEN     0      50                  *:2183                            *:*
    LISTEN     0      50          127.0.0.1:2221                            *:*
    LISTEN     0      50                  *:45907                           *:*
    LISTEN     0      50                  *:45108                           *:*
    LISTEN     0      128                 *:22                              *:*
    LISTEN     0      50          127.0.0.1:2201                            *:*

  6.查看zookeeper集群的状态:

    [root@localhost software]# zookeeper1/bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /software/zookeeper1/bin/../conf/zoo.cfg
    Mode: follower
    [root@localhost software]# zookeeper2/bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /software/zookeeper2/bin/../conf/zoo.cfg
    Mode: leader
    [root@localhost software]# zookeeper3/bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /software/zookeeper3/bin/../conf/zoo.cfg
    Mode: follower

  7.故障转移

    假设zookeeper2节点(leader)故障,查看Leader是否会转移:
    (1)停止zookeeper2节点:
        [root@localhost software]# zookeeper2/bin/zkServer.sh stop
        ZooKeeper JMX enabled by default
        Using config: /software/zookeeper2/bin/../conf/zoo.cfg
        Stopping zookeeper ... STOPPED
        [root@localhost software]# zookeeper2/bin/zkServer.sh status
        ZooKeeper JMX enabled by default
        Using config: /software/zookeeper2/bin/../conf/zoo.cfg
        Error contacting service. It is probably not running.
    (2)查看其它zookeeper节点的状态,发现Leader已经转移到zookeeper3节点
        [root@localhost software]# zookeeper1/bin/zkServer.sh status
        ZooKeeper JMX enabled by default
        Using config: /software/zookeeper1/bin/../conf/zoo.cfg
        Mode: follower
        [root@localhost software]# zookeeper3/bin/zkServer.sh status
        ZooKeeper JMX enabled by default
        Using config: /software/zookeeper3/bin/../conf/zoo.cfg
        Mode: leader
    (3)使zookeeper2节点手动上线:
        [root@localhost software]# zookeeper2/bin/zkServer.sh start
        ZooKeeper JMX enabled by default
        Using config: /software/zookeeper2/bin/../conf/zoo.cfg
        Starting zookeeper ... STARTED
        [root@localhost software]# zookeeper2/bin/zkServer.sh status
        ZooKeeper JMX enabled by default
        Using config: /software/zookeeper2/bin/../conf/zoo.cfg
        Mode: follower
点赞