在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的官方文檔以獲取更多詳細的信息。