我需要创建一个Kafka图像,主题已经创建

htzpubme  于 2021-06-04  发布在  Kafka
关注(0)|答案(3)|浏览(605)

我有一个要求,我需要设置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

在运行这个命令之后,我的容器就开始运行了。
现在我进入这个容器内的bash,就像docker-exec-it landopkafka bash一样
并使用以下命令创建主题

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

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

我需要运行的docker compose文件
伙计们,我需要你们的帮助,因为我对docker和kafka绝对是新手

pkmbmrz7

pkmbmrz71#

我也必须这么做!如果我不想使用wurstmeister图像呢?我决定制作一个自定义脚本来完成这项工作,并在一个单独的容器中运行这个脚本。

存储库

https://github.com/yan-khonski-it/kafka-compose
注意,它将与使用zookeeper的kafka版本一起工作。Zookeeper是Kafka的必备品吗?
从Kafka开始你所有的主题和主题- 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主题创造者。这里,我有三个文件 create-kafka-topics.sh , Dockerfile , README.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"]

创建-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".
u3r8eeie

u3r8eeie2#

你可以像这样创建一个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

yb3bgrhw

yb3bgrhw3#

相反,您应该尝试使用支持环境变量的wurstmeister/kafka映像在容器启动期间创建主题。
当然,landoop容器有很多其他有用的东西,但是听起来你只想要kafka,不想在编辑任何dockerfiles上乱来
另一种解决方案是在kafka之后启动第二个容器,该容器运行create脚本,然后停止自身

相关问题