我想同时使用 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合流/kafka
但是,这导致:无法连接到zookeeper:2181
However this results in: Unable to connect to zookeeper:2181
我还有其他容器要连接,如何通过zookeeper:2181和kafka通过kafka:9092访问zookeeper?
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
- 当您使用 docker run 时,
- zookeper 主机无法访问,因为每个容器都在不同的网络隔离中运行
- kafka 可能会启动并尝试连接到 zookeeper ,但 zookeeper 尚未准备就绪
- zookeper host is not reachable when you use docker run as each of the containers is running in a different network isolation
- kafka may start and try to connect to zookeeper but zookeeper is not ready yet
您可以做很多事情来解决问题
You can do a lot of things to fix things
使用-net = host 在主机网络上运行
使用 docker网络创建< name> ,然后在启动两个容器
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
另一种选择是将-restart = on-failure 标志用于docker run.这将确保容器在发生故障时重新启动,并尝试重新连接到 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容器
发布评论