1.先pull zookeeper的镜像
docker pull wurstmeister/zookeeper:latest
docker pull wurstmeister/kafka:latest
2.查看镜像
docker images
3.执行命令启动kafka,zookeeper
docker run -d --name zookeeper -p 2181:2181 -t wurstmerister/zookeeper
docker run -d --name kafka1 -p 9092:9092 -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT=ip:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://ip:9092 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 -t wurstmeister/kafka
4.进入kafka
docker exec -ti kafka1 /bin/bash
cd /opt/kafka_2.13-2.7.1/
查看主题
./bin/kafka-topics.sh --zookeeper ip:2181 --describe
指定查看的主题
./bin/kafka-topics.sh --zookeeper ip:2181 --describe --topic data
创建新的topic
./bin/kafka-topics.sh --create --zookeeper ip:2181 --replication-factor 1 --partitions 1 --topic topic_zhangmr
./bin/kafka-topics.sh --zookeeper 139.224.43.3:2181 --describe --topic topic_zhangmr
分别启动生产者,消费者
./bin/kafka-console-producer.sh --broker-list ip:9092 --topic topic_zhangmr
./bin/kafka-console-consumer.sh --bootstrap-server ip:9092 --topic topic_zhangmr --from-beginning
生产者发送消息,消费者消费消息
5.docker compose 安装kafka集群模式
version: '2'
services:
zook:
image: wurstmeister/zookeeper
restart: unless-stopped
hostname: zook
ports:
- "2181:2181"
container_name: zook
kafka_1:
image: wurstmeister/kafka
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: ip
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://ip:9092
KAFKA_ZOOKEEPER_CONNECT: "zook:2181"
KAFKA_ADVERTISED_PORT: 9092
KAFKA_BROKER_ID: 1
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
depends_on:
- zook
container_name: kafka_1
kafka_2:
image: wurstmeister/kafka
ports:
- "9093:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: ip
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://ip:9093
KAFKA_ZOOKEEPER_CONNECT: "zook:2181"
KAFKA_ADVERTISED_PORT: 9093
KAFKA_BROKER_ID: 2
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
depends_on:
- zook
container_name: kafka_2
kafka-manager:
container_name: kafka-manager
image: sheepkiller/kafka-manager
environment:
ZK_HOSTS: ip
ports:
- "9000:9000"
执行docker-composr up -d命令
docker-composr up -d
分别进入kafka_1,kafka_2
docker exec -ti kafka_1 /bin/bash
docker exec -ti kafka_2 /bin/bash
cd /opt/kafka_2.13-2.7.1/
./bin/kafka-topics.sh --create --zookeeper ip:2181 --replication-factor 1 --partitions 2 --topic topic_zhangmr
在kafka_kafka_1_1上创建主题 topic_zhangmr (进入 /opt/kafka_2.13-2.7.1/)
cd /opt/kafka_2.13-2.7.1/
分别在kafka_1,kafka_2上查看topic
./bin/kafka-topics.sh --zookeeper ip:2181 --describe --topic topic_zhangmr
在kafka_1上启动消费者
./bin/kafka-console-producer.sh --broker-list ip:9092 --topic topic_zhangmr
进入kafka_1,kafka_2消费者
./bin/kafka-console-consumer.sh --bootstrap-server ip:9092 --topic topic1 --from-beginning
./bin/kafka-console-consumer.sh --bootstrap-server ip:9093 --topic topic1 --from-beginning
kafka_1发送消息 kafka_1,kafka_2 消费者查看
|