Docker安装kafka进阶方式

你在使用docker安装kafka的时候,是不是先启动zookeeper,再启动kafka,复杂的命令自己都不知道啥意思,接下来介绍一种结合docker-compose文件的简洁方式,虽然多了docker-compose文件,但是命令比之前简洁多了:

具体命令如下:

  • docker pull wurstmeister/kafka
  • docker pull zookeeper
  • tag wurstmeister/kafka defaultkafa
  • tag zookeeper defaultzookeeper
  • 创建docker-compose.yml ,配置文件内容在后文提供
  • docker-compose.yml所在文件夹执行:docker-compose up -d
  • 查看是否启动成功:docker ps
  • 进入容器:docker exec -it kafka1 /bin/bash
  • 查看topic列表:/opt/kafka/bin/kafka-topics.sh --zookeeper zoo1:2181 --list
  • 创建topic: /opt/kafka/bin/kafka-topics.sh --create --zookeeper zoo1:2181 --replication-factor 1 --partitions 1 --topic testTopic
  • 查看消息详情:/opt/kafka/bin/kafka-topics.sh --topic testTopic --describe --zookeeper zoo1:2181
  • 启动生产者:/opt/kafka/bin/kafka-console-producer.sh --topic=testTopic --broker-list kafka1:9092
  • 启动消费者:/opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server kafka1:9092 --from-beginning --topic testTopic

docker-compose.yml文件:

version: 'V1.0.1'
 
services:
  zoo1:
    image: defaultzookeeper
    restart: unless-stopped
    hostname: zoo1
    ports:
      - "2181:2181"
    container_name: zookeeper
 
  # kafka version: 1.1.0
  # scala version: 2.12
  kafka1:
    image: defaultkafa
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: localhost
      KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181"
      KAFKA_BROKER_ID: 1
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_CREATE_TOPICS: "stream-in:1:1,stream-out:1:1"
    depends_on:
      - zoo1
    container_name: kafka1

 

©️2020 CSDN 皮肤主题: 点我我会动 设计师:上身试试 返回首页