无法连接到本地 Docker 容器中的单个 Kafka 代理
我正在使用 Wurstmeister Docker Image 尝试运行一个非常基本的单一代理 Kafka 服务器用于测试我的应用程序。
这是一个简单的docker-compose.yml
:
version: '2'
services:
zookeeper:
image: wurstmeister/zookeeper
ports:
- '2181:2181'
kafka:
image: wurstmeister/kafka
ports:
- '9092:9092'
environment:
KAFKA_ADVERTISED_HOST_NAME: localhost
KAFKA_ADVERTISED_PORT: 9092
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'
volumes:
- /var/run/docker.sock:/var/run/docker.sock
我可以进入容器,并使用标准二进制文件创建主题,例如
> docker exec -it ${container} kafka-topics.sh --create --topic my-topic --bootstrap-server localhost:9092
> docker exec -it ${container} kafka-topics.sh --list --bootstrap-server localhost:9092
my-topic
显示容器信息显示暴露的端口9092
:
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
14b0210787cf wurstmeister/zookeeper "/bin/sh -c '/usr/sb…" 11 seconds ago Up 9 seconds 22/tcp, 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp catalog-ingestor-service_zookeeper_1
9ecb59c1bfdd wurstmeister/kafka "start-kafka.sh" 11 seconds ago Up 9 seconds 0.0.0.0:9092->9092/tcp catalog-ingestor-service_kafka_1
使用kcat
(kafkacat),我使用自制程序安装,我尝试连接并收到错误:
> kcat -b localhost:9092 -L
%3|1645810235.301|FAIL|rdkafka#producer-1| [thrd:localhost:9092/bootstrap]: localhost:9092/bootstrap: Failed to resolve 'localhost:9092': nodename nor servname provided, or not known (after 5002ms in state CONNECT)
% ERROR: Failed to acquire metadata: Local: Broker transport failure (Are the brokers reachable? Also try increasing the metadata timeout with -m <timeout>?)
我很确定我应该尝试在 localhost:9092
上连接,但即使当我 ping 时它似乎没有解决:
> ping localhost:9092
ping: cannot resolve localhost:9092: Unknown host
另外,我假设我不需要在 Kafka Broker 和 Zookeeper 之间显式创建网络/桥,因为 zookeeper:2181
已经解析了该桥?
I'm using the Wurstmeister Docker Image to try to run a very basic single broker Kafka server just for testing my application.
It's a simple docker-compose.yml
:
version: '2'
services:
zookeeper:
image: wurstmeister/zookeeper
ports:
- '2181:2181'
kafka:
image: wurstmeister/kafka
ports:
- '9092:9092'
environment:
KAFKA_ADVERTISED_HOST_NAME: localhost
KAFKA_ADVERTISED_PORT: 9092
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'
volumes:
- /var/run/docker.sock:/var/run/docker.sock
I can enter the container, and create topics using the standard binaries, e.g.
> docker exec -it ${container} kafka-topics.sh --create --topic my-topic --bootstrap-server localhost:9092
> docker exec -it ${container} kafka-topics.sh --list --bootstrap-server localhost:9092
my-topic
Displaying container information shows the port 9092
exposed:
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
14b0210787cf wurstmeister/zookeeper "/bin/sh -c '/usr/sb…" 11 seconds ago Up 9 seconds 22/tcp, 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp catalog-ingestor-service_zookeeper_1
9ecb59c1bfdd wurstmeister/kafka "start-kafka.sh" 11 seconds ago Up 9 seconds 0.0.0.0:9092->9092/tcp catalog-ingestor-service_kafka_1
Using kcat
(kafkacat), which I installed using homebrew, I try to connect and get an error:
> kcat -b localhost:9092 -L
%3|1645810235.301|FAIL|rdkafka#producer-1| [thrd:localhost:9092/bootstrap]: localhost:9092/bootstrap: Failed to resolve 'localhost:9092': nodename nor servname provided, or not known (after 5002ms in state CONNECT)
% ERROR: Failed to acquire metadata: Local: Broker transport failure (Are the brokers reachable? Also try increasing the metadata timeout with -m <timeout>?)
I'm pretty sure I should be trying to connect on localhost:9092
, but even when I ping it, it doesn't seem to resolve:
> ping localhost:9092
ping: cannot resolve localhost:9092: Unknown host
Also, I assume I don't need to explicitly create a network / bridge across the Kafka Broker and Zookeeper, since zookeeper:2181
already resolves that bridge?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么,使用端口 ping 不会帮助检查您是否可以到达端口。
不管怎样,我在 Confluence Kafka 上遇到了类似的问题,我必须明确公开端口。请参阅以下 yaml:
使用之前的配置,我可以从 Java 连接:
干杯
Well the ping with a port will not help do check whether you can reach a port or not.
Anyway, I was having a similar issue with Confluent Kafka and I had to explicitly expose the port. See the following yaml:
With previous config I am able to connect from Java:
Cheers
您可以检查 9092 端口是否被任何其他进程使用吗?如果是,那么尝试改变它。
另外,尝试使 docker-compose 版本为 3
can you check if the 9092 port is getting used by any other process? if yes then try to change it.
Also, try to make the docker-compose version 3