Docker按键kafka

Docker按键kafka,第1张

Docker按键kafka 搭建方法一

分别搭建参考Docker搭建Kafka

下载镜像
docker pull wurstmeister/kafka 
docker pull zookeeper
单机启动zookeeper
docker run -d --name zookeeper -p 2181:2181 -t zookeeper:latest
单机启动kafka
docker run -d --name kafka 
-p 9092:9092 
-e KAFKA_BROKER_ID=0 
-e KAFKA_ZOOKEEPER_ConNECT=192.168.8.208:2181 
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.8.208:9092 
-e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 wurstmeister/kafka
创建一个topic
#进入容器
docker exec -it ${ConTAINER ID} /bin/bash
cd opt/bin
#单机方式:创建一个主题
# opt/kafka/bin/
# 172.17.0.2 通过docker inspect zookeeper获取zookeeper的ip地址
./kafka-topics.sh --create --zookeeper 172.17.0.2:2181 --replication-factor 1 --partitions 1 --topic mykafka
#运行一个生产者
./kafka-console-producer.sh --broker-list localhost:9092 --topic mykafka
#运行一个消费者
./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic mykafka --from-beginning
搭建方法二

同时搭建参考docker部署kafka

拉取镜像
docker pull wurstmeister/kafka 
docker pull zookeeper
定义docker-compose.yml
version: '2.2'
services:
  zookeeper:
    image: zookeeper
    ports:
      - "2181:2181"
  kafka:
    image: wurstmeister/kafka
    depends_on: [ zookeeper ]
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: 192.168.8.208
      KAFKA_CREATE_TOPICS: "test:1:1"
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    volumes:
      # 需要在本地找到目录
      - ${HOME}/docker/kafka/docker.sock:/var/run/docker.sock
docker-compose.yml所在文件夹打包
zzw@zhaozhenwei-Mac-mini kafka % docker-compose build
zookeeper uses an image, skipping
kafka uses an image, skipping
启动
zzw@zhaozhenwei-Mac-mini kafka % docker-compose up -d       
Starting kafka_zookeeper_1 ... done
Starting kafka_kafka_1     ... done
关闭所有镜像
docker stop $(docker ps -q)
查看镜像
ConTAINER ID   IMAGE                COMMAND                  CREATED          STATUS              PORTS                                                  NAMES
7fe021070118   wurstmeister/kafka   "start-kafka.sh"         17 minutes ago   Up 23 seconds       0.0.0.0:9092->9092/tcp                                 kafka_kafka_1
6c1c80b36b95   zookeeper            "/docker-entrypoint.…"   17 minutes ago   Up about a minute   2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, 8080/tcp   kafka_zookeeper_1
使用
# 新建topic
$KAFKA_HOME/bin/kafka-topics.sh --create --topic mykafka --partitions 4 --zookeeper kafka_zookeeper_1:2181 --replication-factor 1

# 查看topic
$KAFKA_HOME/bin/kafka-topics.sh --zookeeper kafka_zookeeper_1:2181 --describe --topic mykafka
Topic: mykafka	PartitionCount: 4	ReplicationFactor: 1	Configs: 
	Topic: mykafka	Partition: 0	Leader: 1001	Replicas: 1001	Isr: 1001
	Topic: mykafka	Partition: 1	Leader: 1001	Replicas: 1001	Isr: 1001
	Topic: mykafka	Partition: 2	Leader: 1001	Replicas: 1001	Isr: 1001
	Topic: mykafka	Partition: 3	Leader: 1001	Replicas: 1001	Isr: 1001

# 创建生产者
$KAFKA_HOME/bin/kafka-console-producer.sh --topic=mykafka --broker-list kafka_kafka_1:9092

# 创建消费者
$KAFKA_HOME/bin/kafka-console-consumer.sh --bootstrap-server kafka_kafka_1:9092 --from-beginning --topic mykafka

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5572920.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存