在Docker中部署Kafka集群通常需要使用Docker Compose来管理多个容器。以下是一个简单的示例,通过Docker Compose来部署一个包含3个Kafka节点和1个Zookeeper节点的Kafka集群。
1、创建一个Docker Compose配置文件docker-compose.yaml:
```yaml
version: '3'
services:
zookeeper:
image: wurstmeister/zookeeper
ports:
- 2181:2181
kafka1:
image: wurstmeister/kafka
ports:
- 9092:9092
environment:
KAFKA_ADVERTISED_LISTENERS: INSIDE://kafka1:9093,OUTSIDE://localhost:9092
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
KAFKA_LISTENERS: INSIDE://0.0.0.0:9093,OUTSIDE://0.0.0.0:9092
KAFKA_INTER_BROKER_LISTENER_NAME: INSIDE
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_CREATE_TOPICS: "test:1:1"
links:
- zookeeper
kafka2:
image: wurstmeister/kafka
environment:
KAFKA_BROKER_ID: 1
KAFKA_ADVERTISED_LISTENERS: INSIDE://kafka2:9093,OUTSIDE://localhost:9094
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
KAFKA_LISTENERS: INSIDE://0.0.0.0:9093,OUTSIDE://0.0.0.0:9094
KAFKA_INTER_BROKER_LISTENER_NAME: INSIDE
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
links:
- zookeeper
kafka3:
image: wurstmeister/kafka
environment:
KAFKA_BROKER_ID: 2
KAFKA_ADVERTISED_LISTENERS: INSIDE://kafka3:9093,OUTSIDE://localhost:9095
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
KAFKA_LISTENERS: INSIDE://0.0.0.0:9093,OUTSIDE://0.0.0.0:9095
KAFKA_INTER_BROKER_LISTENER_NAME: INSIDE
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
links:
- zookeeper
```
2、在命令行中使用Docker Compose启动Kafka集群:
```bash
docker-compose up
```
这将启动Zookeeper节点和三个Kafka节点。您可以通过访问`localhost:9092`,`localhost:9094`和`localhost:9095`来访问这三个Kafka节点。
请注意,这只是一个简单的示例,实际部署可能需要更多的配置和调整。您可能还需要做一些额外的配置,如数据卷、网络配置等。建议查看Kafka和Docker的官方文档以获取更多详细的信息。