分布式学习十一:搭建zookeeper集群

举报
仙士可 发表于 2023/06/26 17:17:23 2023/06/26
【摘要】 单机部署之前发过一个单机部署教程,集群部署类似,这次我们通过docker-compse进行编排部署http://www.php20.cn/article/sw/z/317 单机安装制作docker镜像由于centos8停止维护了,这次我们选择Ubuntu,新建dockerfile文件:FROM ubuntuARG ZK_VERSION=3.8.0WORKDIR /zk/RUN apt-get...

单机部署

之前发过一个单机部署教程,集群部署类似,这次我们通过docker-compse进行编排部署

http://www.php20.cn/article/sw/z/317 单机安装

制作docker镜像

由于centos8停止维护了,这次我们选择Ubuntu,新建dockerfile文件:

FROM ubuntu
ARG ZK_VERSION=3.8.0

WORKDIR /zk/
RUN apt-get update 
RUN apt-get install curl -y 
RUN apt-get install openjdk-8-jdk  -y
RUN curl -o zookeeper.tar.gz https://downloads.apache.org/zookeeper/zookeeper-${ZK_VERSION}/apache-zookeeper-${ZK_VERSION}-bin.tar.gz
RUN tar -zvxf zookeeper.tar.gz && \cp -rf apache-zookeeper-${ZK_VERSION}-bin/*  /zk/
RUN cp /zk/conf/zoo_sample.cfg /zk/conf/zoo.cfg
RUN rm -rf zookeeper.tar.g apache-zookeeper-${ZK_VERSION}-bin/
CMD ./bin/zkServer.sh start-foreground
复制

build镜像:

docker build -t zk-test:v1 -f ./zkList/dockerfile/Dockerfile ./zkList/dockerfile
复制

启动:

 docker run -it zk-test:v1
复制

可看到单机启动成功

docker-compose容器编排

容器编排目录结构

tioncico@xianshikedeMBP zkStudy % tree
.
└── zkList  
    ├── docker-compose.yml
    ├── dockerfile
    │   └── Dockerfile
    └── service
        ├── zk01
        │   ├── data
        │   │   └── myid
        │   └── zoo.cfg
        ├── zk02
        │   ├── data
        │   │   └── myid
        │   └── zoo.cfg
        ├── zk03
        │   ├── data
        │   │   └── myid
        │   └── zoo.cfg
        ├── zk04
        │   ├── data
        │   │   └── myid
        │   └── zoo.cfg
        └── zk05
            ├── data
            │   └── myid
            └── zoo.cfg
复制

编写docker-compose.yml 

version: "3"
services:
  zk01:
    image: zk-test:v1
    ports:
      - "10001:2181"
    volumes:
      - ${SOURCE_DIR}/service/zk01/data:/zk/data/
      - ${SOURCE_DIR}/service/zk01/data/myid:/zk/data/myid
      - ${SOURCE_DIR}/service/zk01/zoo.cfg:/zk/conf/zoo.cfg
    restart: always
    networks:
      - default
    environment:
      TZ: "Asia/Shanghai"
    command: [ ./bin/zkServer.sh,start-foreground ]

  zk02:
    image: zk-test:v1
    ports:
      - "10002:2181"
    volumes:
      - ${SOURCE_DIR}/service/zk02/data:/zk/data/
      - ${SOURCE_DIR}/service/zk02/data/myid:/zk/data/myid
      - ${SOURCE_DIR}/service/zk02/zoo.cfg:/zk/conf/zoo.cfg
    restart: always
    networks:
      - default
    environment:
      TZ: "Asia/Shanghai"
    command: [ ./bin/zkServer.sh,start-foreground ]

  zk03:
    image: zk-test:v1
    ports:
      - "10003:2181"
    volumes:
      - ${SOURCE_DIR}/service/zk03/data:/zk/data/
      - ${SOURCE_DIR}/service/zk03/data/myid:/zk/data/myid
      - ${SOURCE_DIR}/service/zk03/zoo.cfg:/zk/conf/zoo.cfg
    restart: always
    networks:
      - default
    environment:
      TZ: "Asia/Shanghai"
    command: [ ./bin/zkServer.sh,start-foreground ]

  zk04:
    image: zk-test:v1
    ports:
      - "10004:2181"
    volumes:
      - ${SOURCE_DIR}/service/zk04/data:/zk/data/
      - ${SOURCE_DIR}/service/zk04/data/myid:/zk/data/myid
      - ${SOURCE_DIR}/service/zk04/zoo.cfg:/zk/conf/zoo.cfg
    restart: always
    networks:
      - default
    environment:
      TZ: "Asia/Shanghai"
    command: [ ./bin/zkServer.sh,start-foreground ]

  zk05:
    image: zk-test:v1
    ports:
      - "10005:2181"
    volumes:
      - ${SOURCE_DIR}/service/zk05/data:/zk/data/
      - ${SOURCE_DIR}/service/zk05/data/myid:/zk/data/myid
      - ${SOURCE_DIR}/service/zk05/zoo.cfg:/zk/conf/zoo.cfg
    restart: always
    networks:
      - default
    environment:
      TZ: "Asia/Shanghai"
    command: [ ./bin/zkServer.sh,start-foreground ]
networks:
  default:
复制

注意 里面有个${SOURCE_DIR} 是.env 环境文件配置的

tioncico@xianshikedeMBP zkList % cat .env
SOURCE_DIR=./
tioncico@xianshikedeMBP zkList %
复制

编写zoo.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=/zk/data/
# the port at which the clients will connect
clientPort=2181

# 集群地址配置 server.A=B:C:D A为序号 B为ip地址或者host C为通信端口 D为集群选举端口
server.1=zk01:2888:3888
server.2=zk02:2888:3888
server.3=zk03:2888:3888
server.4=zk04:2888:3888
server.5=zk05:2888:3888
# 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.
#
# https://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.httpHost=0.0.0.0
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
复制

编写myid

每个myid文件写上服务器编号即可,1-5

启动

docker-composer up
复制

验证集群

打开任意n个(n>2) 容器(多个shell窗口打开):

docker exec -it zklist_zk01_1 ./bin/zkCli.sh
docker exec -it zklist_zk02_1 ./bin/zkCli.sh
docker exec -it zklist_zk03_1 ./bin/zkCli.sh
复制

使用其中一个create /test

create /test 123456
复制

使用另外2个查询

get /test
复制

集群搭建成功

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。