参考:https://www.jianshu.com/p/8ccd712e2599
搭建过程
1. 下载zookeeper镜像
docker pull wurstmeister/zookeeper
2. 下载kafka镜像
docker pull wurstmeister/kafka
3. 创建一个docker-compose.yml文件
version: '2'
services:
zookeeper:
image: wurstmeister/zookeeper
ports:
- "2181:2181"
kafka:
image: wurstmeister/kafka
ports:
- "9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: "192.168.99.100"
KAFKA_ZOOKEEPER_CONNECT: 192.168.99.100:2181
volumes:
- /var/run/docker.sock:/var/run/docker.sock
4. 启动docker-compose
docker-compose up -d
5. 运行kafka集群模式
docker-compose scale kafka=3
6. 查看container
docker ps
docker ps -a
7. 进入指定容器
docker exec -it dockertoolbox_kafka_1 /bin/bash
8. 创建Topic
$KAFKA_HOME/bin/kafka-topics.sh --create --topic test \
--zookeeper dockertoolbox_zookeeper_1:2181 --replication-factor 1 --partitions 1
9. 查看创建的topic
$KAFKA_HOME/bin/kafka-topics.sh --zookeeper
dockertoolbox_zookeeper_1:2181 \ --describe --topic test
10. 发布消息
$KAFKA_HOME/bin/kafka-console-producer.sh --topic=test \
--broker-list dockertoolbox_kafka_1:9092
11. 接受消息
$KAFKA_HOME/bin/kafka-console-consumer.sh--bootstrap-server dockertoolbox_kafka_1:9092--from-beginning --topic test
来源:oschina
链接:https://my.oschina.net/u/4312618/blog/4067279