欢迎访问 SJsir 圣杰博客

A Linux operation and maintenance engineer's Blogs

Zookeeper单机安装

  ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,它是集群的管理者,监视着集群中各个节点的状态根据节点提交的反馈进行下一步合理操作。最终,将简单易用的接口和性能高效、功能稳定的系统提供给用户。

  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安装包:

    (1)国内常见镜像源:
        华中科技大学:http://mirrors.hust.edu.cn/apache/zookeeper/
        上海大学:http://mirrors.shu.edu.cn/apache/zookeeper/
        清华大学:http://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/
    (2)zookpeer安装包下载:
    [root@localhost ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.tar.gz
    --2018-06-22 15:14:56--  https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.tar.gz
    Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1
    Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 36667596 (35M) [application/octet-stream]
    Saving to: ‘zookeeper-3.4.12.tar.gz’
    100%[======================================================================================>] 36,667,596  2.37MB/s   in 14s    
    2018-06-22 15:15:10 (2.54 MB/s) - ‘zookeeper-3.4.12.tar.gz’ saved [36667596/36667596]

  4.zookeeper安装:

    [root@localhost ~]# mkdir /software
    [root@localhost ~]# cd /software/
    [root@localhost software]# tar zxf /root/zookeeper-3.4.12.tar.gz 
    [root@localhost software]# ls
    zookeeper-3.4.12
    [root@localhost software]# mv zookeeper-3.4.12/ zookeeper
    [root@localhost software]# ls
    zookeeper

  5.zookeeper配置:

    (1)zookeeper软件配置:
    [root@localhost ~]# cd /software/zookeeper/
    [root@localhost zookeeper]# cd conf/
    [root@localhost conf]# cp zoo_sample.cfg zoo.cfg
    tickTime=2000       #心跳间隔时间,zookeeper中使用的基本时间单位,毫秒值。每隔2秒发送一个心跳
    initLimit=10        #leader与客户端连接超时时间。表示10个心跳间隔
    syncLimit=5         #Leader与Follower之间的超时时间,表示5个心跳间隔
    dataDir=/software/data/zookeeper        #数据目录
    dataLogDir=/software/logs/zookeeper     #日志目录
    clientPort=2181     #客户端连接端口
    (2)配置环境变量
    [root@localhost ~]# cat /etc/profile.d/zoo.sh
    ZOOKEEPER_HOME=/software/zookeeper
    PATH=$ZOOKEEPER_HOME/bin:$PATH
    export ZOOKEEPER_HOME PATH
    [root@localhost ~]# source /etc/profile.d/zoo.sh 
    [root@localhost ~]# echo $PATH
    /software/zookeeper/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
    [root@localhost ~]# zk
    zkCleanup.sh  zkCli.cmd     zkCli.sh      zkEnv.cmd     zkEnv.sh      zkServer.cmd  zkServer.sh

  6.启动与适用zookeeper:

    (1)启动Zookeeper:
        [root@localhost ~]# zkServer.sh start
        ZooKeeper JMX enabled by default
        Using config: /software/zookeeper/bin/../conf/zoo.cfg
        Starting zookeeper ... STARTED
    (2)查看Zookeeper状态:
    [root@localhost ~]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /software/zookeeper/bin/../conf/zoo.cfg
    Mode: standalone
    [root@localhost ~]# ss -tnl
    State      Recv-Q Send-Q        Local Address:Port          Peer Address:Port
    LISTEN     0      50                        *:36924                    *:*
    LISTEN     0      50                        *:2181                     *:*
    LISTEN     0      128                        *:22                      *:*
    (3)连接使用Zookeeper:
    [root@localhost ~]# zkCli.sh -server 127.0.0.1:2181
    Connecting to 127.0.0.1:2181
    2018-06-22 16:26:05,220 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.12-e5259e437540f349646870ea94dc2658c4e44b3b, built on 03/27/2018 03:55 GMT
    2018-06-22 16:26:05,230 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=VM_73_232_centos
    2018-06-22 16:26:05,230 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_171
    2018-06-22 16:26:05,232 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
    2018-06-22 16:26:05,233 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/java/jdk1.8.0_171-amd64/jre
    2018-06-22 16:26:05,233 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/software/zookeeper/bin/../build/classes:/software/zookeeper/bin/../build/lib/*.jar:/software/zookeeper/bin/../lib/slf4j-log4j12-1.7.25.jar:/software/zookeeper/bin/../lib/slf4j-api-1.7.25.jar:/software/zookeeper/bin/../lib/netty-3.10.6.Final.jar:/software/zookeeper/bin/../lib/log4j-1.2.17.jar:/software/zookeeper/bin/../lib/jline-0.9.94.jar:/software/zookeeper/bin/../lib/audience-annotations-0.5.0.jar:/software/zookeeper/bin/../zookeeper-3.4.12.jar:/software/zookeeper/bin/../src/java/lib/*.jar:/software/zookeeper/bin/../conf:
    2018-06-22 16:26:05,233 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
    2018-06-22 16:26:05,233 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
    2018-06-22 16:26:05,233 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
    2018-06-22 16:26:05,233 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
    2018-06-22 16:26:05,233 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
    2018-06-22 16:26:05,233 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=3.10.0-862.3.3.el7.x86_64
    2018-06-22 16:26:05,233 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root
    2018-06-22 16:26:05,233 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root
    2018-06-22 16:26:05,233 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/root
    2018-06-22 16:26:05,234 [myid:] - INFO  [main:ZooKeeper@441] - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@799f7e29
    2018-06-22 16:26:05,258 [myid:] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1028] - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
    Welcome to ZooKeeper!
    JLine support is enabled
    2018-06-22 16:26:05,372 [myid:] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@878] - Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
    2018-06-22 16:26:05,386 [myid:] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1302] - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x100000a5c520002, negotiated timeout = 30000
    WATCHER::
    WatchedEvent state:SyncConnected type:None path:null
    [zk: 127.0.0.1:2181(CONNECTED) 0] ls /
    [zookeeper]
    [zk: 127.0.0.1:2181(CONNECTED) 1] quit
    Quitting...
    2018-06-22 16:26:11,499 [myid:] - INFO  [main:ZooKeeper@687] - Session: 0x100000a5c520002 closed
    2018-06-22 16:26:11,501 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@521] - EventThread shut down for session: 0x100000a5c520002
    (4)停止Zookeeper:
    [root@localhost ~]# zkServer.sh stop
    ZooKeeper JMX enabled by default
    Using config: /software/zookeeper/bin/../conf/zoo.cfg
    Stopping zookeeper ... STOPPED
    [root@localhost ~]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /software/zookeeper/bin/../conf/zoo.cfg
    Error contacting service. It is probably not running.
点赞