我需要使用已经创建的主题创建一个kafka图像

时间:2019-09-19 05:43:09

标签: docker apache-kafka docker-compose dockerfile

我有一个要求,我需要在本地设置带有容器中已有主题的kafka。我正在使用ladoop/fast-data-dev来实现

我是如何手动进行的-

docker run -d --name landoopkafka -p 2181:2181 -p 3030:3030 -p 8081:8081 -p 8082:8082 -p 8083:8083 -p 9092:9092 -e ADV_HOST=localhost landoop/fast-data-dev

运行此命令后,我的容器已启动并运行。

现在我要像docker -exec -it landopkafka bash一样在这个容器中进行bash

并使用此命令创建主题

kafka-topics --zookeeper localhost:2181 --create --topic hello_topic --partitions 1 --replication-factor 1

我的主题已创建。

但是我的要求是我需要有一个将创建主题的docker文件,我只需要运行它即可。

OR

我需要运行的docker compose文件

我需要帮助,因为我对docker和kafka绝对陌生

3 个答案:

答案 0 :(得分:0)

您应该尝试使用支持环境变量的wurstmeister / kafka映像在容器启动期间创建主题。

当然,Landoop容器还有很多其他有用的东西,但是听起来您只想要Kafka,不想弄乱编辑任何Dockerfiles

另一种解决方案是在Kafka之后启动第二个容器 ,该容器运行创建脚本,然后自行停止

答案 1 :(得分:0)

您可以像这样创建一个docker-compose文件...


version: '2'

services:
  zookeeper:
      image: wurstmeister/zookeeper:latest
      ports:
        - "2181:2181"

  kafka:
      image: wurstmeister/kafka:0.10.2.1
      ports:
        - "9092:9092"
      environment:
        KAFKA_ADVERTISED_HOST_NAME: 127.0.0.1
        KAFKA_CREATE_TOPICS: "MY_TOPIC_ONE:1:1,/
                              MY_TOPIC_TWO:1:1"
        KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      volumes:
        - /var/run/docker.sock:/var/run/docker.sock

将您的主题放在此处并运行docker-compose up

答案 2 :(得分:0)

我也必须这样做!如果我不想使用 wurstmeister 图片怎么办?我决定制作一个可以完成此任务的自定义脚本,然后在单独的容器中运行该脚本。

存储库

Is Zookeeper a must for Kafka?

请注意,它将与使用 zookeeper 的kafka版本一起使用。 https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref

要以所有主题和动物园管理员开始卡夫卡 -docker-compose up -d


实施细节。

docker-compose.yml

# These services are kafka related. This docker-compose allows to start kafka locally quickly.

version: '2.1'

networks:
  demo-network:
    name: demo-network
    driver: bridge

services:
  zookeeper:
    image: "confluentinc/cp-zookeeper:${CONFLUENT_PLATFORM_VERSION}"
    container_name: zookeeper
    environment:
      ZOOKEEPER_CLIENT_PORT: 32181
      ZOOKEEPER_TICK_TIME: 2000
    ports:
      - 32181:32181
    hostname: zookeeper
    networks:
      - demo-network

  kafka:
    image: "confluentinc/cp-kafka:${CONFLUENT_PLATFORM_VERSION}"
    container_name: kafka
    hostname: kafka
    ports:
      - 9092:9092
      - 29092:29092

    environment:
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:32181
      KAFKA_BROKER_ID: 1
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092,PLAINTEXT_HOST://kafka:29092
      LISTENERS: PLAINTEXT://0.0.0.0:9092
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1

    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    depends_on:
      - "zookeeper"
    networks:
      - demo-network

  # Automatically creates required kafka topics if they were not created.
  kafka-topics-creator:
    build:
      context: kafka-topic-creator
      dockerfile: Dockerfile
    container_name: kafka-topics-creator
    depends_on:
      - zookeeper
      - kafka
    environment:
      ZOOKEEPER_HOSTS: "zookeeper:32181"
      KAFKA_TOPICS: "topic_v1 topic_v2"
    networks:
      - demo-network

然后我有一个目录 kafka-topics-creator 。 在这里,我有三个文件 create-kafka-topics.shDockerfileREADME.md

Dockerfile

# It is recommened to use same version as kafka broker is used.
# So no additional images are pulled.
FROM confluentinc/cp-kafka:4.1.2

WORKDIR usr/bin

# Once it is executed, this container is not needed.
COPY create-kafka-topics.sh create-kafka-topics.sh
ENTRYPOINT ["./create-kafka-topics.sh"]

create-kafka-topics.sh

#!/bin/bash

# Simply wait until original kafka container and zookeeper are started.
sleep 15.0s

# Parse string of kafka topics into an array
# https://stackoverflow.com/a/10586169/4587961
kafkatopicsArrayString="$KAFKA_TOPICS"
IFS=' ' read -r -a kafkaTopicsArray <<< "$kafkatopicsArrayString"

# A separate variable for zookeeper hosts.
zookeeperHostsValue=$ZOOKEEPER_HOSTS

# Create kafka topic for each topic item from split array of topics.
for newTopic in "${kafkaTopicsArray[@]}"; do
  # https://kafka.apache.org/quickstart
  kafka-topics --create --topic "$newTopic" --partitions 1 --replication-factor 1 --if-not-exists --zookeeper "$zookeeperHostsValue"
done

README.md -其他人知道如何使用它。始终记录您的内容-很好的建议。

# Creates kafka topics automatically.

## Parameters
`ZOOKEEPER_HOSTS` - zookeeper hosts,  I used value `"zookeeper:32181"` to run it locally.

`KAFKA_TOPICS` - space separated list of kafka topics. Example, `topic_1, topic_2, topic_3`.

Note, this container should run only **after** your original kafka broker and zookeeper are running.
After this container creates topics, it is not needed anymore.

如何检查主题是否已创建。

一种解决方案是检查kafka-topics-creator容器的日志。

docker logs kafka-topics-creator应该打印

$ docker logs kafka-topics-creator
WARNING: Due to limitations in metric names, topics with a period ('.') or underscore ('_') could collide. To avoid issues it is best to use either, but not both.
Created topic "topic_v1".
WARNING: Due to limitations in metric names, topics with a period ('.') or underscore ('_') could collide. To avoid issues it is best to use either, but not both.
Created topic "topic_v2".