Docker Compose部署Zookeeper集群与Kafka集群

作者:快去debug2024.02.04 13:59浏览量:4

简介:本文将指导您使用Docker Compose部署Zookeeper集群和Kafka集群,并确保它们在Docker容器中稳定运行。我们将首先创建一个新的Docker网络,然后拉取所需的镜像,并配置它们以形成集群。最后,我们将验证集群是否正常工作。

首先,我们需要创建一个新的Docker网络。这将允许我们的容器之间进行通信。运行以下命令创建一个名为’zookeeper_network’的新网络:

  1. docker network create --driver bridge --subnet 172.23.0.0/16 --gateway 172.23.0.1 zookeeper_network

接下来,我们将拉取Zookeeper和Kafka的Docker镜像。运行以下命令:

  1. docker pull zookeeper
  2. docker pull wurstmeister/kafka

此外,如果你还需要Kafka的管理工具,可以再拉取一个Kafka-Manager的镜像:

  1. docker pull hlebalbau/kafka-manager

现在,我们有了所需的镜像,下一步是创建并启动我们的容器。首先,创建一个新的目录用于存储我们的Compose文件,然后编辑docker-compose.yml文件,内容如下:
```yaml
version: ‘3’
services:
zookeeper1:
image: zookeeper
networks:

  • zookeeper_network
    ports:
  • 2181:2181
    environment:
    ZOO_MY_ID: 1
    ZOO_SERVERS: server.1=zookeeper1:2888:3888
    deploy:
    mode: replicated
    replicas: 3
    zookeeper2:
    image: zookeeper
    networks:
  • zookeeper_network
    ports:
  • 2182:2181
    environment:
    ZOO_MY_ID: 2
    ZOO_SERVERS: server.1=zookeeper1:2888:3888
    deploy:
    mode: replicated
    replicas: 3
    zookeeper3:
    image: zookeeper
    networks:
  • zookeeper_network
    ports:
  • 2183:2181
    environment:
    ZOO_MY_ID: 3
    ZOO_SERVERS: server.1=zookeeper1:2888:3888
    deploy:
    mode: replicated
    replicas: 3
    kafka1:
    image: wurstmeister/kafka
    networks:
  • zookeeper_network
    ports:
  • 9092:9092
    environment:
    KAFKA_ADVERTISED_HOST_NAME: kafka1
    KAFKA_ZOOKEEPER_CONNECT: zookeeper1:2181,zookeeper2:2181,zookeeper3:2181,localhost:2181 # Replace localhost with the host IP if your kafka and zookeeper are not in the same docker-compose service.
    depends_on:
  • zookeeper1
    kafka2:
    image: wurstmeister/kafka
    networks:
  • zookeeper_network
    ports:
  • 9093:9092
    environment:
    KAFKA_ADVERTISED_HOST_NAME: kafka2
    KAFKA_ZOOKEEPER_CONNECT: zookeeper1:2181,zookeeper2:2181,zookeeper3:2181,localhost:2181 # Replace localhost with the host IP if your kafka and zookeeper are not in the same docker-compose service.
    depends_on:
  • zookeeper1
    kafka3:
    image: wurstmeister/kafka
    networks:
  • zookeeper_network
    ports:
  • 9094:9092
    environment:
    KAFKA_ADVERTISED_HOST_NAME: kafka3
    KAFKA_ZOOKEEPER_CONNECT: zookeeper1:2181,zookeeper2:2181,zookeeper3:2181,localhost:2181 # Replace localhost with the host IP if your kafka and zookeeper are not