Kafka 是一个分布式流处理平台,使用 Docker 可以方便地部署 Kafka 环境。以下是几种常见的 Docker 安装 Kafka 的方法:
拉取 Kafka 和 Zookeeper 镜像:
docker pull wurstmeister/zookeeper
docker pull wurstmeister/kafka
启动 Zookeeper:
docker run -d --name zookeeper -p 2181:2181 wurstmeister/zookeeper
启动 Kafka:
docker run -d --name kafka \
-p 9092:9092 \
-e KAFKA_ADVERTISED_HOST_NAME=localhost \
-e KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 \
--link zookeeper \
wurstmeister/kafka
创建 docker-compose.yml
文件:
version: '3'
services:
zookeeper:
image: wurstmeister/zookeeper
ports:
- "2181:2181"
kafka:
image: wurstmeister/kafka
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: localhost
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
depends_on:
- zookeeper
启动服务:
docker-compose up -d
创建 docker-compose.yml
文件:
version: '2'
services:
zookeeper:
image: confluentinc/cp-zookeeper:latest
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
ports:
- "2181:2181"
kafka:
image: confluentinc/cp-kafka:latest
depends_on:
- zookeeper
ports:
- "9092:9092"
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
启动服务:
docker-compose up -d
进入 Kafka 容器:
docker exec -it kafka /bin/bash
创建测试主题:
kafka-topics.sh --create --topic test --partitions 1 --replication-factor 1 --bootstrap-server localhost:9092
列出所有主题:
kafka-topics.sh --list --bootstrap-server localhost:9092
连接问题:
KAFKA_ADVERTISED_HOST_NAME
或 KAFKA_ADVERTISED_LISTENERS
设置正确性能优化:
-e KAFKA_HEAP_OPTS="-Xmx1G -Xms1G"
数据持久化:
-v /your/path:/var/lib/kafka/data
希望这个指南能帮助你顺利在 Docker 中安装 Kafka。根据你的具体需求,可以选择适合的安装方式。