准备镜像
本示例起3个kafka实例 一个zookeeper实例
- kafka1:端口 9092 broker_id 0
- kafka2:端口 9093 broker_id 1
- kafka3:端口 9094 broker_id 2
- zookeeper: 端口 2181
docker pull wurstmeister/kafka
docker pull wurstmeister/zookeepe
环境搭建
启动zookeeper
docker run -d --name zookeeper -p 2181:2181 -t wurstmeister/zookeeper
启动一个kafka先做下测试
docker run -d --name kafka1 \
说明:192.168.1.138为我本机IP
-p 9092:9092 \
-e KAFKA_BROKER_ID=0 \
-e KAFKA_ZOOKEEPER_CONNECT=192.168.1.138:2181 \
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.1.138:9092 \
-e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 -t wurstmeister/kafka
进入kafka1容器内,测试单节点收发消息
docker exec -ti kafka1 /bin/bash
进入容器内部,到/opt/kafka下 bin目录下为所有的脚本,通过kafka-topics.sh来创建一个名为 receive_mysql_data1 的 topic:
$KAFKA_HOME/bin/kafka-topics.sh --create --zookeeper 192.168.1.138:2181 --replication-factor 1 --partitions 1 --topic receive_mysql_data1
说明:replication-factor=1 ,partitions=1
查看Topic信息:
$KAFKA_HOME/bin/kafka-topics.sh --zookeeper zookeeper:2181 --describe --topic receive_mysql_data1
发几个测试消息:
$KAFKA_HOME/bin/kafka-console-producer.sh --broker-list 192.168.1.138:9092 --topic receive_mysql_data1
helloWorld
Who am I?
bash-4.4#
说明:输入数据回车,ctrl+c结束
消费者接收消息:
$KAFKA_HOME/bin/kafka-console-consumer.sh --bootstrap-server 192.168.1.138:9092 --topic receive_mysql_data1 --from-beginning
创建另外2个kafka实例
启动kafka2:
docker run -d --name kafka2 \
-p 9093:9093 \
-e KAFKA_BROKER_ID=1 \
-e KAFKA_ZOOKEEPER_CONNECT=192.168.1.138:2181 \
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.1.138:9093 \
-e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9093 -t wurstmeister/kafka登录到容器,并创建topic receive_mysql_data2 :
docker exec -ti kafka2 /bin/bash
$KAFKA_HOME//bin/kafka-topics.sh --create --zookeeper 192.168.1.138:2181 --replication-factor 2 --partitions 2 --topic receive_mysql_data2
启动kafka3:
docker run -d --name kafka3 \
-p 9094:9094 \
-e KAFKA_BROKER_ID=2 \
-e KAFKA_ZOOKEEPER_CONNECT=192.168.1.138:2181 \
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.1.138:9094 \
-e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9094 -t wurstmeister/kafka登录到容器,并创建topic receive_mysql_data3:
docker exec -ti kafka3 /bin/bash
$KAFKA_HOME//bin/kafka-topics.sh --create --zookeeper 192.168.1.138:2181 --replication-factor 2 --partitions 2 --topic receive_mysql_data3
搭建集群管理控制台
docker pull sheepkiller/kafka-manager
docker run -itd --name=kafka-manager -p 9000:9000 -e ZK_HOSTS="192.168.1.138:2181" sheepkiller/kafka-manager
说明:本地 http://localhost:9000 进入管理界面,选择Add Cluster新增集群,填入zookeeper:2181
转载请注明:左手代码右手诗 » 使用Docker快速搭建kafka集群