我需要创建一个已创建主题的 kafka 图像
Posted
技术标签:
【中文标题】我需要创建一个已创建主题的 kafka 图像【英文标题】:I need to create a kafka image with topics already created 【发布时间】:2020-01-20 02:19:36 【问题描述】:我有一个要求,我需要使用容器中已有的主题在本地设置 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 文件,该文件将创建主题,我只需要运行它。
或
我需要运行的 docker compose 文件
伙计们,我需要帮助,因为我对 docker 和 kafka 完全陌生
【问题讨论】:
将命令添加到docker run
的末尾
你能帮我处理一下 docker 文件吗
@Lin 会覆盖 CMD,因此无法保持 kafka 运行
【参考方案1】:
我也必须这样做!如果我不想使用 wurstmeister 图片怎么办?我决定制作一个自定义脚本来完成这项工作,并在单独的容器中运行此脚本。
存储库
https://github.com/yan-khonski-it/kafka-compose
注意,它适用于使用 zookeeper 的 kafka 版本。 Is Zookeeper a must for Kafka?
用你的所有主题和 zookeeper 开始 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-topics-creator。
在这里,我有三个文件
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"]
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://***.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".
【讨论】:
好的,谢谢。它很有用,并解释了如何提供分区【参考方案2】:你可以像这样创建一个 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
【讨论】:
【参考方案3】:您应该尝试使用支持环境变量的 wurstmeister/kafka 映像在容器启动期间创建主题。
当然,Landoop 容器还有很多其他有用的东西,但听起来你只想要 Kafka 而不想搞乱编辑任何 Dockerfiles
另一种解决方案是在运行创建脚本的 Kafka 之后启动第二个容器,然后自行停止
【讨论】:
我们有什么办法可以使用 confluentinc kafka 图像来做到这一点? 该变量在该图像中不存在,因此您不能 @abhisheksingh,是的,请检查下面的答案。以上是关于我需要创建一个已创建主题的 kafka 图像的主要内容,如果未能解决你的问题,请参考以下文章