[英]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.