当前位置: 首页 > 工具软件 > docker-spark > 使用案例 >

docker-compose安装zookeeper集群与kafka

钱弘壮
2023-12-01


kafka的安装依赖于zookeeper,所以这里将通过docker-compose先安装zookeeper集群,并通过zkui作为可视化面板进行查看,然后安装单机kafka,并注册到zk集群上。同时,通过开源软件kafka-ui作为可视化面板进行监控和操作。

一、zookeeper与zk-ui的安装

version: '3'

services:
  zk1:
    image: zookeeper:3.4.14
    restart: always
    hostname: zk1
    container_name: zk1
    ports:
      - 2181:2181
    volumes:
      - /mydata/zookeeper/zk1/data:/data
      - /mydata/zookeeper/zk1/datalog:/datalog
    environment:
      ZOO_MY_ID: 1
      ZOO_SERVERS: server.1=zk1:2888:3888 server.2=zk2:2888:3888 server.3=zk3:2888:3888
    networks:
      - mynet

  zk2:
    image: zookeeper:3.4.14
    restart: always
    hostname: zk2
    container_name: zk2
    ports:
      - 2182:2181
    volumes:
      - /mydata/zookeeper/zk2/data:/data
      - /mydata/zookeeper/zk2/datalog:/datalog
    environment:
      ZOO_MY_ID: 2
      ZOO_SERVERS: server.1=zk1:2888:3888 server.2=zk2:2888:3888 server.3=zk3:2888:3888
    networks:
      - mynet

  zk3:
    image: zookeeper:3.4.14
    restart: always
    hostname: zk3
    container_name: zk3
    ports:
      - 2183:2181
    volumes:
      - /mydata/zookeeper/zk3/data:/data
      - /mydata/zookeeper/zk3/datalog:/datalog
    environment:
      ZOO_MY_ID: 3
      ZOO_SERVERS: server.1=zk1:2888:3888 server.2=zk2:2888:3888 server.3=zk3:2888:3888
    networks:
      - mynet

  zkui:
    image: maauso/zkui
    restart: always
    hostname: zkui
    container_name: zkui
    ports:
      - 9090:9090
    environment:
      ZKLIST: zk1:2181,zk2:2182,zk3:2183
    networks:
      - mynet

networks:
  mynet:
    driver: bridge

运行下面的命令来启动上述脚本,将安装在2181,2182,2183端口的三个zk,并组成一个集群

docker-compose up -d

二、kafka与kafka-ui的安装

version: '3'

services:
  kafka:
    image: bitnami/kafka:2.3.1
    restart: always
    container_name: kafka
    ports:
      - "9092:9092"
    environment:
      KAFKA_BROKER_ID: 0
      # 服务器内网ip:2181/kafka
      # 配置zookeeper管理kafka的路径
      KAFKA_ZOOKEEPER_CONNECT: "zk1:2181,zk1:2182,zk1:2183"
      # PLAINTEXT://服务器内网ip:port
      # 把kafka的地址端口注册给zookeeper
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
      # 配置kafka的监听端口
      KAFKA_LISTENERS: PLAINTEXT://:9092
      KAFKA_LOG_RETENTION_HOURS: 24
      ALLOW_PLAINTEXT_LISTENER: "yes"
      KAFKA_MESSAGE_MAX_BYTES: 10000000
      KAFKA_REPLICA_FETCH_MAX_BYTES: 10000000
      KAFKA_GROUP_MAX_SESSION_TIMEOUT_MS: 60000
      KAFKA_NUM_PARTITIONS: 3
      KAFKA_DELETE_RETENTION_MS: 1000
    volumes:
      - /mydata/kafka/data:/kafka
      - /etc/localtime:/etc/localtime
    networks:
      - mynet


  kafka-ui:
    image: provectuslabs/kafka-ui
    restart: always
    container_name: kafka-ui
    ports:
      - "9099:8080"
    environment:
      KAFKA_CLUSTERS_0_NAME: kaka
      KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:9092
      KAFKA_CLUSTERS_0_ZOOKEEPER: "zk1:2181,zk1:2182,zk1:2183"
    networks:
      - mynet

volumes:
  mydata:
    external: true
networks:
  mynet:
    driver: bridge

运行下面的命令来启动上述脚本,将安装端口在2181,2182,2183端口的三个zk,并组成一个集群

docker-compose up -d

注意:将上述安装kafka脚本中的zk1,zk2,zk3 分别替换为实际安装的zk服务的服务器ip地址。

当然,除了写死ip地址也还有其他方法,比如使用.env文件,在里面写入COMPOSE_PROJECT_NAME=lk,这样就能统一docker的网络前缀。具体方式可以自行搜索相关资料。这样可以最终使得所有容器的网络可以互通,使用.env文件的方式就不用修改上面的脚本了

 类似资料: