在docker环境中启动分布式kafka连接后创建kafka连接器

yduiuuwa  于 2021-06-04  发布在  Kafka
关注(0)|答案(2)|浏览(464)

我正在尝试创建一个Kafka连接器后,连接分布式命令执行。我编写了entrypoint.sh脚本并将其与cmd一起使用。我有这样的docker文件:

FROM confluentinc/cp-kafka
RUN mkdir /plugins
RUN mkdir /config
COPY kafka-connect-couchbase-*.jar /plugins/
COPY config /config/
RUN chmod +x /config/stage/entrypoint.sh
ENV EXPOSED_PORT 8083
CMD /config/stage/entrypoint.sh

我的入口点脚本文件为:

connect-distributed config/"${DEPLOY_ENV}"/connect-distributed.properties
curl -X POST -H "Content-Type: application/json" -d @config.json http://localhost:8083/connectors

部署环境是无关的,它是来自Jenkins。配置文件和distributed.properties也不相关,而且是正确的,我手动尝试过。
kafka connect启动时没有问题,但是用于创建连接器的curl命令没有效果。
简而言之,我想在connect distributed启动后创建一个连接器,而不在容器外执行任何rest请求。我如何做到这一点?

lmyy7pcs

lmyy7pcs1#

多亏了罗宾·莫法特(robin moffatt)的绝妙解决方案,我把它与自己的需求结合起来,效果很好。
既然我把图像部署到库伯内特家, /etc/confluent/docker/run & background命令使容器传递到completed状态,而不是running。这使得容器无法通过rest接口从外部访问,如下所示:

http://some-ip:31682/connectors

为了解决这个问题,我在最初的问题中使用了dockerfile,但是修改了robin的脚本,删除了汇合docker run命令并添加了额外的if来检查连接器是否存在。

bash -c ' \
echo -e "\n\n=============\nWaiting for Kafka Connect to start listening on localhost ⏳\n=============\n"
while [ $(curl -s -o /dev/null -w %{http_code} http://localhost:8083/connectors) -ne 200 ] ; do
  echo -e "\t" $(date) " Kafka Connect listener HTTP state: " $(curl -s -o /dev/null -w %{http_code} http://localhost:8083/connectors) " (waiting for 200)"
  sleep 5
done
echo -e $(date) "\n\n--------------\n\o/ Kafka Connect is ready! Listener HTTP state: " $(curl -s -o /dev/null -w %{http_code} http://localhost:8083/connectors) "\n--------------\n"

if [ $(curl -s -o /dev/null -w %{http_code} http://localhost:8083/connectors/cbconnector2) -ne 200 ]
then
  curl -X POST -H "Content-Type: application/json" -d @config/stage/config.json http://localhost:8083/connectors
fi'

之后,我通过添加post start lifecycle修改了kubernetes部署文件,并将entrypoint.sh脚本作为命令来执行,如下所示:

lifecycle:
  postStart:
    exec:
      command: ["/bin/sh", "/config/stage/entrypoint.sh"]

基本上,它首先启动kafka connect,在进程(pod)启动之后,我只需执行自定义shell脚本来创建kafka连接器。
希望这对有类似使用场景的人有所帮助。我也对其他(更好的)解决方案持开放态度。多谢罗宾·莫法特。

zdwk9cvp

zdwk9cvp2#

您需要确保您正在等待kafka connect worker完全启动。
顺便说一句,你最好从Kafka连接基地的形象开始

FROM confluentinc/cp-kafka-connect-base:5.5.0

通常您会使用confluent hub来安装连接器,但是看起来couchbase一个不在那里,所以您必须像以前一样在jar中复制。
在connect图像中启动kafka connect的实际脚本是 /etc/confluent/docker/run ,所以你的 /config/stage/entrypoint.sh 应该是这样的:


# Launch the worker

/etc/confluent/docker/run &

# Wait for it to start running

# Change the port here if not using the default

bash -c ' \
echo -e "\n\n=============\nWaiting for Kafka Connect to start listening on localhost ⏳\n=============\n"
while [ $(curl -s -o /dev/null -w %{http_code} http://localhost:8083/connectors) -ne 200 ] ; do
  echo -e "\t" $(date) " Kafka Connect listener HTTP state: " $(curl -s -o /dev/null -w %{http_code} http://localhost:8083/connectors) " (waiting for 200)"
  sleep 5
done
echo -e $(date) "\n\n--------------\n\o/ Kafka Connect is ready! Listener HTTP state: " $(curl -s -o /dev/null -w %{http_code} http://localhost:8083/connectors) "\n--------------\n"

# Now create your connector

## Inline config example:

curl -i -X PUT -H  "Content-Type:application/json" http://localhost:8083/connectors/sink-file-jsonschema-as-json/config \
    -d '{
            "connector.class": "org.apache.kafka.connect.file.FileStreamSinkConnector",
            "key.converter": "org.apache.kafka.connect.storage.StringConverter",
            "value.converter": "org.apache.kafka.connect.json.JsonConverter",
            "tasks.max": 1,
            "file": "/jsonschema-as-json.txt",
            "topics": "test-jsonschema"
}'

## External file example:

curl -X POST -H "Content-Type: application/json" -d @config.json http://localhost:8083/connectors

另请参见https://rmoff.net/2018/12/15/docker-tips-and-tricks-with-ksql-and-kafka/

相关问题