• docker-compose搭建zookeeper集群环境 CodingCode


    docker-compose搭建zookeeper集群环境

    1. 编辑docker-compose.yaml文件
    version: '2'
    networks:
      byfn:
    
    services:
      zookeeper1:
        image: zookeeper
        container_name: zookeeper1
        environment:
            - ZOO_MY_ID=1
            - ZOO_SERVERS=server.1=zookeeper1:2888:3888 server.2=zookeeper2:2888:3888 server.3=zookeeper3:2888:3888
        ports:
          - 2181
        networks:
          - byfn
    
      zookeeper2:
        image: zookeeper
        container_name: zookeeper2
        environment:
            - ZOO_MY_ID=2
            - ZOO_SERVERS=server.1=zookeeper1:2888:3888 server.2=zookeeper2:2888:3888 server.3=zookeeper3:2888:3888
        ports:
          - 2181
        networks:
          - byfn
    
      zookeeper3:
        image: zookeeper
        container_name: zookeeper3
        environment:
            - ZOO_MY_ID=3
            - ZOO_SERVERS=server.1=zookeeper1:2888:3888 server.2=zookeeper2:2888:3888 server.3=zookeeper3:2888:3888
        ports:
          - 2181
        networks:
          - byfn
    

    详细的配置请参考文档:https://hub.docker.com/_/zookeeper/
    这里非常清楚的描述了如何修改配置,存储数据,以及调整日志级别以及输出等设置。

    1. 启动服务
    $ docker-compose up
    
    1. 验证集群的状态
    $ docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                         NAMES
    c1f7646bf0e6        zookeeper           "/docker-entrypoin..."   23 minutes ago      Up 23 minutes       2888/tcp, 3888/tcp, 0.0.0.0:32794->2181/tcp   zookeeper2
    7e05f06c258d        zookeeper           "/docker-entrypoin..."   23 minutes ago      Up 23 minutes       2888/tcp, 3888/tcp, 0.0.0.0:32793->2181/tcp   zookeeper3
    d293438c1813        zookeeper           "/docker-entrypoin..."   23 minutes ago      Up 23 minutes       2888/tcp, 3888/tcp, 0.0.0.0:32792->2181/tcp   zookeeper1
    
    $ docker exec -t zookeeper1 zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /conf/zoo.cfg
    Mode: follower
    $ docker exec -t zookeeper2 zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /conf/zoo.cfg
    Mode: follower
    $ docker exec -t zookeeper3 zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /conf/zoo.cfg
    Mode: leader
    

    这里我们看到三个zookeeper节点,zookeeper3是leader,zookeeper1和zookeeper2是follower。

    其他状态检查命令:

    $ nc localhost 32794 <<< ruok
    imok
    
    $ echo srvr | nc localhost 32794
    Zookeeper version: 3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03, built on 06/29/2018 04:05 GMT
    Latency min/avg/max: 0/7/116
    Received: 40
    Sent: 39
    Connections: 1
    Outstanding: 0
    Zxid: 0x10000000e
    Mode: follower
    Node count: 5
    
    1. 集群的使用

    4.1 基本操作

    [zk: localhost:2181(CONNECTED) 4] create /foo "Hello foo"
    Created /foo
    
    [zk: localhost:2181(CONNECTED) 5] get /foo
    Hello foo
    cZxid = 0x100000013
    ctime = Fri Jan 04 02:29:47 GMT 2019
    mZxid = 0x100000013
    mtime = Fri Jan 04 02:29:47 GMT 2019
    pZxid = 0x100000013
    cversion = 0
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 9
    numChildren = 0
    
    [zk: localhost:2181(CONNECTED) 6] ls /foo 
    []
    

    4.2 创建路径create

    [zk: localhost:2181(CONNECTED) 7] create /foo1/foo1 "Hello foo1"
    Node does not exist: /foo1/foo1
    

    创建失败,因为zookeeper不支持递归创建,必须先一级一级的创建父目录节点。
    创建父目录时,让值为空就可以。(其实目录也可以带值,两者并不冲突。)

    [zk: localhost:2181(CONNECTED) 14] create /foo1 ""     # 或者:create /foo1 ""
    Created /foo1
    
    [zk: localhost:2181(CONNECTED) 15] create /foo1/foo1 "Hello foo1"
    Created /foo1/foo1
    

    4.3 查看节点的元素ls

    [zk: localhost:2181(CONNECTED) 21] ls /foo1     
    [foo1]
    

    因为/foo1是一个目录,他下面有一个子节点/foo1/foo1

    [zk: localhost:2181(CONNECTED) 21] ls /foo1/foo1     
    []
    

    因为/foo1/foo1是一个元节点,他下面没有子节点了。

    注意,ls命令只能列出一级目录,不能递归列出所有的子目录。

    4.4 获取值get

    get /foo1/foo1
    

    4.5 修改值set

    set /foo1/foo1 'newvalue'
    

    4.6 删除节点delete

    delete /foo1/foo1
    

    注意只能删除子节点为空的节点;所以如果要删除一个节点,必须先把所有的子节点都删除。
    如果要删除非空目录,还可以使用rmr命令。



    作者:CodingCode
    链接:https://www.jianshu.com/p/8d31ede99206
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    《生命3.0—在亿年的尺度下审视生命的演进》阅读笔记3
    软件杯赛题周总结(2)
    《生命3.0—在亿年的尺度下审视生命的演进》阅读笔记2
    记一次阅读源码的小经历
    11
    解决在 CSS 中,如何实现动态吸顶的样式/效果 ?
    Angular 初始化项目后,如何把默认的 .css 文件修改为 .scss 文件?
    解决 Angular 项目中,添加 <router-outlet> 标签后,报错: ‘router-outlet’ is not a known element 的问题。
    在 Angular 项目中,如何为项目单独创建路由文件?
    [NOIP2013 提高组] 《火柴排队》
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/11797908.html
Copyright © 2020-2023  润新知