首先,我们需要创建一个新的Docker网络。这将允许我们的容器之间进行通信。运行以下命令创建一个名为’zookeeper_network’的新网络:
docker network create --driver bridge --subnet 172.23.0.0/16 --gateway 172.23.0.1 zookeeper_network
接下来,我们将拉取Zookeeper和Kafka的Docker镜像。运行以下命令:
docker pull zookeeper
docker pull wurstmeister/kafka
此外,如果你还需要Kafka的管理工具,可以再拉取一个Kafka-Manager的镜像:
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=zookeeper13888
deploy:
mode: replicated
replicas: 3
zookeeper2:
image: zookeeper
networks: - zookeeper_network
ports: - 2182:2181
environment:
ZOO_MY_ID: 2
ZOO_SERVERS: server.1=zookeeper13888
deploy:
mode: replicated
replicas: 3
zookeeper3:
image: zookeeper
networks: - zookeeper_network
ports: - 2183:2181
environment:
ZOO_MY_ID: 3
ZOO_SERVERS: server.1=zookeeper13888
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