一、清理卷:
bash
docker volume rm docker-kafka_kafka_0_data docker-kafka_kafka_1_data docker-kafka_kafka_2_data docker-kafka_zookeeper_data
二、编辑yml:
bash
vi docker-compose.yml
yaml
version: "2"
services:
zookeeper:
container_name: zookeeper
hostname: zookeeper
image: bitnami/zookeeper:3.8
ports:
- "2181"
environment:
- ALLOW_ANONYMOUS_LOGIN=yes
volumes:
- zookeeper_data:/bitnami/zookeeper
kafka-0:
container_name: kafka-0
hostname: kafka-0
image: bitnami/kafka:3.2
ports:
- "9092"
environment:
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
- KAFKA_CFG_BROKER_ID=0
- ALLOW_PLAINTEXT_LISTENER=yes
- KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT
- KAFKA_CFG_LISTENERS=INTERNAL://:9092,EXTERNAL://0.0.0.0:9093
- KAFKA_CFG_ADVERTISED_LISTENERS=INTERNAL://kafka-0:9092,EXTERNAL://192.168.71.33:9093
- KAFKA_CFG_INTER_BROKER_LISTENER_NAME=INTERNAL
volumes:
- kafka_0_data:/bitnami/kafka
depends_on:
- zookeeper
kafka-1:
container_name: kafka-1
hostname: kafka-1
image: bitnami/kafka:3.2
ports:
- "9092"
environment:
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
- KAFKA_CFG_BROKER_ID=1
- ALLOW_PLAINTEXT_LISTENER=yes
- KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT
- KAFKA_CFG_LISTENERS=INTERNAL://:9092,EXTERNAL://0.0.0.0:9094
- KAFKA_CFG_ADVERTISED_LISTENERS=INTERNAL://kafka-1:9092,EXTERNAL://192.168.71.33:9094
- KAFKA_CFG_INTER_BROKER_LISTENER_NAME=INTERNAL
volumes:
- kafka_1_data:/bitnami/kafka
depends_on:
- zookeeper
kafka-2:
container_name: kafka-2
hostname: kafka-2
image: bitnami/kafka:3.2
ports:
- "9092"
environment:
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
- KAFKA_CFG_BROKER_ID=2
- ALLOW_PLAINTEXT_LISTENER=yes
- KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT
- KAFKA_CFG_LISTENERS=INTERNAL://:9092,EXTERNAL://0.0.0.0:9095
- KAFKA_CFG_ADVERTISED_LISTENERS=INTERNAL://kafka-2:9092,EXTERNAL://192.168.71.33:9095
- KAFKA_CFG_INTER_BROKER_LISTENER_NAME=INTERNAL
volumes:
- kafka_2_data:/bitnami/kafka
depends_on:
- zookeeper
nginx:
container_name: nginx
hostname: nginx
image: nginx:1.22.0-alpine
volumes:
- /opt/kafka/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
ports:
- "9093-9095:9093-9095"
depends_on:
- kafka-0
- kafka-1
- kafka-2
volumes:
zookeeper_data:
driver: local
kafka_0_data:
driver: local
kafka_1_data:
driver: local
kafka_2_data:
driver: local
三、运行容器:
bash
docker compose up -d
四、测试:
bash
docker exec -it kafka-0 /opt/bitnami/kafka/bin/kafka-console-consumer.sh --bootstrap-server kafka-0:9092 --topic mytopic
EOF