简体   繁体   English

从docker主机外部与kafka docker容器交互

[英]Interact with kafka docker container from outside of docker host

I have built a kafka docker container and orchestrate it using docker-compose. 我已经构建了一个kafka docker容器并使用docker-compose编排它。

Calling docker ps I get the following putput: 调用docker docker ps我得到以下putput:

CONTAINER ID        IMAGE                          COMMAND                CREATED             STATUS              PORTS                                         NAMES
    5bde6f76246e        hieutrtr/docker-kafka:0.0.1    "/start.sh"            About an hour ago   Up About an hour    7203/tcp, 0.0.0.0:32884->9092/tcp             dockerkafka_kafka_3
    be354f1b8cc0        hieutrtr/docker-ubuntu:devel   "/usr/bin/supervisor   About an hour ago   Up About an hour    22/tcp                                        producer1
    50d3203af90e        hieutrtr/docker-kafka:0.0.1    "/start.sh"            About an hour ago   Up About an hour    7203/tcp, 0.0.0.0:32883->9092/tcp             dockerkafka_kafka_2
    61b285f39615        hieutrtr/docker-kafka:0.0.1    "/start.sh"            2 hours ago         Up 2 hours          7203/tcp, 0.0.0.0:32882->9092/tcp             dockerkafka_kafka_1
    20c9c5ccec05        jplock/zookeeper:3.4.6         "/opt/zookeeper/bin/   2 hours ago         Up 2 hours          2888/tcp, 3888/tcp, 0.0.0.0:32881->2181/tcp   dockerkafka_zookeeper_1

I can run a producer and a consumer from inside the docker container, but it is not working from outside the docker network. 我可以从docker容器中运行生产者和消费者,但它不能从docker网络外部运行。

For example : 例如

I run a kafka producer on my localhost and the following error appears: 我在localhost上运行kafka生成器,出现以下错误:

$ kafka_2.9.1-0.8.2.1: bin/kafka-console-producer.sh --topic test --broker-list $DOCKER_HOST:32884
[2015-08-31 06:55:15,450] WARN Property topic is not valid (kafka.utils.VerifiableProperties)
to
[2015-08-31 06:55:20,214] WARN Failed to send producer request with correlation id 2 to broker 1 with data for partitions [test,0] (kafka.producer.async.DefaultEventHandler)
java.nio.channels.ClosedChannelException
    at kafka.network.BlockingChannel.send(BlockingChannel.scala:100)
    at kafka.producer.SyncProducer.liftedTree1$1(SyncProducer.scala:73)
    at kafka.producer.SyncProducer.kafka$producer$SyncProducer$$doSend(SyncProducer.scala:72)
    at kafka.producer.SyncProducer$$anonfun$send$1$$anonfun$apply$mcV$sp$1.apply$mcV$sp(SyncProducer.scala:103)
    at kafka.producer.SyncProducer$$anonfun$send$1$$anonfun$apply$mcV$sp$1.apply(SyncProducer.scala:103)
    at kafka.producer.SyncProducer$$anonfun$send$1$$anonfun$apply$mcV$sp$1.apply(SyncProducer.scala:103)

This is my kafka docker example on github that includes the mentioned problem. 这是我在github上的kafka docker示例,其中包含上述问题。

So, is anyone experiencing the same problems and can help me in any way? 那么,是否有人遇到同样的问题并且可以以任何方式帮助我?

Additional info : 附加信息

(Just fork from ches/kafka and modify something for docker-compose) : (只需从ches / kafka派生并修改docker-compose的内容):

在Kafka服务器属性中,您需要将advertised.host.nameadvertised.port设置为正在运行的容器的ip / port,然后它应该可以工作。

You need to put the name of the host machine where the docker instance was deployed. 您需要将主机的名称放在部署docker实例的位置。 Also you need to map ports from docker host machine(public) to the docker container instance(private). 您还需要将端口从docker主机(公共)映射到docker容器实例(私有)。

TL;DR Expose port 9092 on host and map it to 9092 container port to access kafka brokers outside container. TL; DR在主机上公开端口9092并将其映射到9092容器端口以访问容器外的kafka代理。 See docker-compose documentation for details. 有关详细信息,请参阅docker-compose文档。

I think the problem is that you don't expose port 9092 outside container. 我认为问题是你没有在容器外面暴露端口9092。 According to your docker ps listing your 9092 container port is mapped dynamically to host's port range 32882-32884. 根据您的docker ps列表,您的9092容器端口动态映射到主机的端口范围32882-32884。 When you connect to broker configured this way you receive metadata that contains port 9092 for advertising. 当您连接到以这种方式配置的代理时,您会收到包含用于广告的端口9092的元数据。 With this metadata producer tries to do other requests through port 9092 and fails. 此元数据生成器尝试通过端口9092执行其他请求并失败。

For the record, another way to get my local kafka consumer communicating with remote broker inside a Docker container was to add an entry in my /etc/hosts : docker-host-ip-address docker-kafka-container-hostname 为了记录,让我的本地kafka消费者与Docker容器内的远程代理通信的另一种方法是在我的/ etc / hosts中添加一个条目: docker-host-ip-address docker-kafka-container-hostname

Anyway Lundahl's solution worked fine for me and seems cleaner. 无论如何,Lundahl的解决方案对我来说很好,看起来更干净。 Even cleaner would be to set advertised.listeners = host-ip : port since advertised.host.name and advertised.port are deprecated. 甚至更干净的是设置advertised.listeners = host-ipport,因为不推荐使用advertised.host.nameadvertised.port

Here are my two cents, since I had a hard time figuring this one out. 这是我的两分钱,因为我很难搞清楚这一点。

My $KAFKA_HOME/config/server.properties contains the following: 我的$ KAFKA_HOME / config / server.properties包含以下内容:

listener.security.protocol.map=INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT

advertised.listeners=INSIDE://${container_ip}:9092,OUTSIDE://${outside_host_ip}:29092

listeners=INSIDE://:9092,OUTSIDE://:29092

inter.broker.listener.name=INSIDE

This is creating two connections, one to be used inside docker and another to be used outside. 这创建了两个连接,一个用于docker内部,另一个用于外部。 You have to choose a new port for the latter, in my case 29092, make sure this port is exposed and mapped by docker. 您必须为后者选择一个新端口,在我的情况下为29092,确保此端口由docker公开并映射。

I was not yet able to figure out a solution without the ${outside_host_ip} in the environment, hence I'm providing the host machine's ip as an env var. 在环境中没有$ {outside_host_ip}的情况下我还没有找到解决方案,因此我将主机的ip作为env var提供。

Test: 测试:

  1. Enter the Kafka container and create a topic: ./kafka-topics.sh -zookeeper zookeeper:2181 --create --topic dummytopic --partitions 1 --replication-factor 1 输入Kafka容器并创建一个主题: ./kafka-topics.sh -zookeeper zookeeper:2181 --create --topic dummytopic --partitions 1 --replication-factor 1
  2. From outside the Kafka container do: ./kafka-console-producer.sh --broker-list 0.0.0.0:29092 --topic dummytopic and input a message 从Kafka容器外部执行: ./kafka-console-producer.sh --broker-list 0.0.0.0:29092 --topic dummytopic并输入消息

I hope this helps others 我希望这有助于其他人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从 docker 容器外部访问 Kafka - Kafka is not accessbile from outside of the docker container Kafka Docker - 不能从码头集装箱外生产或消费 - Kafka Docker - Can't produce or consume from outside of docker container docker kafka 代理无法从外部或其他 docker 容器访问 - docker kafka broker is not accessible from outside or other docker container 无法连接到 docker 主机外部的 docker 容器 - Unable connect to docker container outside docker host Docker pypspark 集群容器未从主机接收 kafka 流? - Docker pypspark cluster container not receiving kafka streaming from the host? 无法从外部客户端连接到 kafka docker 容器(wurstmeister 图像) - Cannot connect to kafka docker container from outside client (wurstmeister images) 无法通过端口映射从外部主机访问 docker 容器 - cant reach docker container from outside host with port mapping 从 docker 容器在主机上运行 docker 容器 - Running a docker container on the host from a docker container 如何从主机外部(同一网络)连接到 docker 容器 [Windows] - How to connect to a docker container from outside the host (same network) [Windows] 如何从主机外部连接到 docker postgreSQL 容器 - How do I connect to a docker postgreSQL container from outside the host
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM