简体   繁体   English

在虚拟机中的docker中与kafka连接

[英]Connection with kafka in docker in virtual machine

I am trying to use wurstmeister\\kafka-docker image with docker-compose, but I am having real problems with connecting everything. 我正在尝试将wurstmeister\\kafka-docker镜像与wurstmeister\\kafka-docker -compose一起使用,但是在连接所有内容时遇到了实际问题。

I am working on Oracle Linux 7.6 as guest OS based on XEN host server so I don't have virtualization in this virtual machine. 我正在使用Oracle Linux 7.6作为基于XEN主机服务器的来宾OS,因此该虚拟机中没有虚拟化。

I am trying to connect with one broker so I used docker-compose-single-broker.yml : 我试图与一个经纪人联系,所以我使用了docker-compose-single-broker.yml

version: '2'
services:
  zookeeper:
    image: wurstmeister/zookeeper
    ports:
      - "2181:2181"
  kafka:
    build: .
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: 127.0.0.1
      KAFKA_CREATE_TOPICS: "test:1:1"
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

So when I run docker-compose -f docker-compose-single-broker.yml up everything looks good: 所以当我运行docker-compose -f docker-compose-single-broker.yml up一切看起来都不错:

kafka_1      | creating topics: test:1:1
zookeeper_1  | 2019-04-23 12:32:16,680 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxnFactory@215] - Accepted socket connection from /172.24.0.2:50096
zookeeper_1  | 2019-04-23 12:32:16,685 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:ZooKeeperServer@949] - Client attempting to establish new session at /172.24.0.2:50096
zookeeper_1  | 2019-04-23 12:32:16,690 [myid:] - INFO  [SyncThread:0:ZooKeeperServer@694] - Established session 0x100476bb75e0001 with negotiated timeout 30000 for client /172.24.0.2:50096
zookeeper_1  | 2019-04-23 12:32:17,161 [myid:] - INFO  [ProcessThread(sid:0 cport:2181)::PrepRequestProcessor@487] - Processed session termination for sessionid: 0x100476bb75e0001
zookeeper_1  | 2019-04-23 12:32:17,165 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@1056] - Closed socket connection for client /172.24.0.2:50096 which had sessionid 0x100476bb75e0001

After that I open new terminal and run ssh: docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -e HOST_IP=172.17.0.1 -e ZK=172.17.0.1:2181 -i -t kafka-docker_kafka /bin/bash 之后,我打开新终端并运行ssh: docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -e HOST_IP=172.17.0.1 -e ZK=172.17.0.1:2181 -i -t kafka-docker_kafka /bin/bash

But when I try to create new topic: 但是当我尝试创建新主题时:

kafka-topics.sh --create --topic topicELO --partitions 4 --zookeeper $ZK --replication-factor 1

I've got exception: 我有例外:

Exception in thread "main" kafka.zookeeper.ZooKeeperClientTimeoutException: Timed out waiting for connection while in state: CONNECTING
        at kafka.zookeeper.ZooKeeperClient.$anonfun$waitUntilConnected$3(ZooKeeperClient.scala:242)
        at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
        at kafka.utils.CoreUtils$.inLock(CoreUtils.scala:251)
        at kafka.zookeeper.ZooKeeperClient.waitUntilConnected(ZooKeeperClient.scala:238)
        at kafka.zookeeper.ZooKeeperClient.(ZooKeeperClient.scala:96)
        at kafka.zk.KafkaZkClient$.apply(KafkaZkClient.scala:1825)
        at kafka.admin.TopicCommand$ZookeeperTopicService$.apply(TopicCommand.scala:262)
        at kafka.admin.TopicCommand$.main(TopicCommand.scala:53)
        at kafka.admin.TopicCommand.main(TopicCommand.scala)

I also tried localhost and IP of my machine - same result. 我也尝试了机器的localhost和IP-相同的结果。

When I try to do that in Docker for Windows, using host.docker.internal everything works fine - where I made a mistake on linux? 当我尝试在Windows的Docker中执行此操作时,使用host.docker.internal一切正常-在Linux上我出错了吗?

You need to add the --network option to be the same name as the Docker-Compose network on your docker run command. 您需要在--network docker run命令上添加--network选项,使其与Docker-Compose网络名称相同。

Otherwise your "new container" won't be in the same network as the Zookeeper container, and therefore a connection will timeout. 否则,您的“新容器”将与Zookeeper容器不在同一网络中,因此连接将超时。

Once you do that, you don't need -e ZK , as you would just use kafka-topics --zookeeper zookeeper:2181 (the container service name) 完成此操作后,就不需要-e ZK ,只需使用kafka-topics --zookeeper zookeeper:2181 (容器服务名称)

Use docker network ls and look for the one that contains the directory name you started docker-compose in 使用docker network ls并查找包含您在其中启动docker-compose的目录名称的目录


The quicker way would be to do this, though 不过,更快的方法是这样做

docker-compose exec kafka bash -c "kafka-topics --create --zookeeper zookeeper:2181 ... "


Note: KAFKA_ADVERTISED_HOST_NAME: 127.0.0.1 isn't "correct" as you would be telling any external Kafka clients to connect to themselves (on 127.0.0.1), not actually the Kafka container. 注意: KAFKA_ADVERTISED_HOST_NAME: 127.0.0.1不是“正确的”,因为您将告诉任何外部Kafka客户端(而不是Kafka容器)连接到它们自己(在127.0.0.1上)。 more details here 这里有更多细节

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

相关问题 VMWare中2个虚拟机之间的Point 2 Point连接 - Point 2 Point connection between 2 virtual machine in VMWare 无法在 Linux 上激活虚拟环境(Docker 快速入门终端虚拟机) - Cannot activate virtual environment on Linux (Docker Quickstart Terminal virtual machine) 在 Windows 上托管 docker 虚拟机时,是否可以将 docker-nvidia 配置为与 tensorflow-serving 一起使用? - Is it possible to configure docker-nvidia for use with tensorflow-serving while hosting the docker virtual machine on Windows? 从安装了 Zeppelin 的本地计算机连接到 Docker Spark 集群 - Connection from local machine installed Zeppelin to Docker Spark cluster Docker 说:无法建立连接,因为目标机器主动拒绝了它 - Docker says: No connection could be made because the target machine actively refused it 如何将 nginx docker 的 /etc/nginx/ 目录挂载到主机虚拟机? - How to mount nginx docker's /etc/nginx/ directory to host virtual machine? 无法通过JDBC连接到虚拟机上的Oracle 11g:连接重置 - Unable to connect to Oracle 11g on virtual machine via JDBC: Connection reset 当我使用visual studio远程linux连接到一个linux虚拟机时,连接不稳定 - When I use visual studio remote linux to connect to connect to a linux virtual machine, the connection is instability 虚拟机查询? - virtual machine query? 具有FBCTF的虚拟机 - Virtual Machine with FBCTF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM