我的 kafka docker 容器无法连接到我的 zookeeper docker 容器 [英] My kafka docker container cannot connect to my zookeeper docker container
问题描述
我想同时使用 confluent/kafka
和 confluent/zookeeper
并在单个 Ubuntu 服务器上运行它们.
I want to use both confluent/kafka
and confluent/zookeeper
and run them on a single Ubuntu server.
我正在使用以下配置:
docker run -e ZOOKEEPER_CLIENT_PORT=2181 --name zookeeper confluent/zookeeper
docker run --name kafka -e KAFKA_ADVERTISED_HOST_NAME=kafka -e KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 -e KAFKA_CREATE_TOPICS=testtopic:1:1 confluent/kafka
docker run --name kafka -e KAFKA_ADVERTISED_HOST_NAME=kafka -e KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 -e KAFKA_CREATE_TOPICS=testtopic:1:1 confluent/kafka
然而这会导致:无法连接到zookeeper:2181
However this results in: Unable to connect to zookeeper:2181
我有其他想要连接的容器,如何通过 zookeeper:2181 访问 zookeeper 和通过 kafka:9092 访问 kafka?
I have other containers that I'd like to connect to, how can I access zookeeper via zookeeper:2181 and kafka via kafka:9092 ?
推荐答案
有多种方法可以做到这一点.但在我们研究它之前,您需要了解您的方法中有两个问题
There are multiple ways to do this. But before we look into it there are two problems in your approach that you need to understand
zookeper
使用docker run
时无法访问主机,因为每个容器都在不同的网络隔离中运行kafka
可能会启动并尝试连接到zookeeper
但zookeeper
尚未准备好
zookeper
host is not reachable when you usedocker run
as each of the containers is running in a different network isolationkafka
may start and try to connect tozookeeper
butzookeeper
is not ready yet
你可以做很多事情来解决问题
You can do a lot of things to fix things
使用 --net=host
在主机网络上运行两者
use --net=host
to run both on the host network
使用 docker network create
然后在启动两个容器时使用 --net=
use docker network create <name>
and then use --net=<name>
while launching both the containers
或者您可以在 zookeeper
容器网络上运行您的 kafka 容器.
Or you can run your kafka container on the zookeeper
containers network.
在启动 kafka
容器时使用 --net=container:zookeeper
.这将确保 zookeeper
主机可访问.除非您有充分的理由这样做,否则不建议这样做.因为一旦 zookeeper
容器出现故障,您的 kafka
容器的网络也会出现故障.但为了便于理解,我把这个选项放在这里
use --net=container:zookeeper
when launching kafka
container. This will make sure zookeeper
host is accessible. This is not recommended as such, until unless you have some strong reason to do so. Because as soon as zookeeper
container goes down, so will be the network of your kafka
container. But for the sake of understanding, I have put this option here
或者你可以在启动zookeeper
和kafka
之间留一个间隙,以确保当kafka
启动zookeeper
> 已启动并运行
Either you can keep a gap between starting zookeeper
and kafka
, to make sure that when kafka
starts zookeeper
is up and running
另一种选择是在 docker run 中使用 --restart=on-failure
标志.这将确保容器在失败时重新启动,并尝试重新连接到 zookeeper
并希望 zookeeper
时间到.
Another option is to use --restart=on-failure
flag with docker run. This will make sure the container is restarted on failure and will try to reconnect to zookeeper
and hopefully that time zookeeper
will be up.
我总是希望使用 docker-compose
来运行这样的链接容器,而不是使用 docker run
.您可以通过创建一个简单的 docker-compose.yml
文件然后使用 docker-compsoe up
Instead of using docker run
I would always prefer the docker-compose
to get such linked containers to be run. You can do that by creating a simple docker-compose.yml
file and then running it with docker-compsoe up
version: "3.4"
services:
zookeeper:
image: confluent/zookeeper
environment:
- ZOOKEEPER_CLIENT_PORT=2181
kafka:
image: confluent/kafka
environment:
- KAFKA_ADVERTISED_HOST_NAME=kafka
- KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
- KAFKA_CREATE_TOPICS=testtopic:1:1
depends_on:
- zookeeper
restart: on-failure
这篇关于我的 kafka docker 容器无法连接到我的 zookeeper docker 容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!