gpt4 book ai didi

docker - Docker 中的 Kafka 无法正常工作

转载 作者:IT老高 更新时间:2023-10-28 12:35:22 29 4
gpt4 key购买 nike

我正在尝试将 wurstmeister\kafka-docker 图像与 docker-compose 一起使用,但我在连接所有东西时遇到了真正的问题。

我检查的所有帖子或问题似乎都没有任何问题,但坦率地说我迷路了。 (并且在 SO 中至少有两个问题试图解决这个问题)

我认为问题在于我对docker的网络理解不够。那么问题来了:

我可以从同一个 kafka 容器消费和生产,但是,当我尝试创建另一个容器(或将我的笔记本电脑与 python 客户端一起使用)时,我遇到了几个与 advertised.host.name 参数(图中这个参数是KAFKA_ADVERTISED_HOST_NAME)

我已经尝试了很多方法来设置这个变量,但它根本不起作用。

所以我正在寻找一个权威的答案(即如何自动设置这些参数及其含义)如何设置 docker-compose.yml

这是我的:

zookeeper:
image: wurstmeister/zookeeper
ports:
- "2181:2181"

kafka:
image: wurstmeister/kafka
# hostname: kafka
ports:
- "9092"
links:
- zookeeper:zk
environment:
KAFKA_ADVERTISED_HOST_NAME: "kafka"
KAFKA_ADVERTISED_PORT: "9092"
KAFKA_ZOOKEEPER_CONNECT: "zk:2181"

更新

根据@dnephin 的建议,我在以下几行中修改了 start-kafka.sh:

...
if [[ -z "$KAFKA_ADVERTISED_PORT" ]]; then
export KAFKA_ADVERTISED_PORT=$(hostname -i)
fi
...

并从 docker-compose.yml 中删除 KAFKA_ADVERTISED_HOST_NAME: "kafka"

我以规范的方式启动容器:

docker-compose up -d

两个容器都在运行:

$ docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------------------------------
infraestructura_kafka_1 start-kafka.sh Up 0.0.0.0:32768->9092/tcp
infraestructura_zookeeper_1 /opt/zookeeper/bin/zkServe ... Up 0.0.0.0:2181->2181/tcp, 2888/tcp, 3888/tcp

后来我做了:

docker-compose logs

一切顺利。

用于检查 IP 地址:

$ KAFKA_IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' infraestructura_kafka_1)                                                                                                            
$ echo $KAFKA_IP
172.17.0.4

and

$ ZK_IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' infraestructura_zookeeper_1)
$ echo $ZK_IP
172.17.0.3

然后我在两个不同的控制台中执行:

制作人:

$ docker run --rm --interactive wurstmeister/kafka /opt/kafka_2.11-0.9.0.1/bin/kafka-console-producer.sh --topic grillo --broker-list 171.17.0.4:9092  

消费者:

$ docker run --rm --interactive  wurstmeister/kafka /opt/kafka_2.11-0.9.0.1/bin/kafka-console-consumer.sh --topic grillo --from-beginning --zookeeper 172.17.0.3:2181 

几乎立即,警告开始在屏幕上飞来飞去:

[2016-03-11 00:39:17,010] WARN Fetching topic metadata with correlation id 0 for topics [Set(grillo)] from broker [BrokerEndPoint(1001,ba53d4fd7595,9092)] failed (kafka.client.ClientUtils$)
java.nio.channels.ClosedChannelException
at kafka.network.BlockingChannel.send(BlockingChannel.scala:110)
at kafka.producer.SyncProducer.liftedTree1$1(SyncProducer.scala:75)
at kafka.producer.SyncProducer.kafka$producer$SyncProducer$$doSend(SyncProducer.scala:74)
at kafka.producer.SyncProducer.send(SyncProducer.scala:119)
at kafka.client.ClientUtils$.fetchTopicMetadata(ClientUtils.scala:59)
at kafka.client.ClientUtils$.fetchTopicMetadata(ClientUtils.scala:94)
at kafka.consumer.ConsumerFetcherManager$LeaderFinderThread.doWork(ConsumerFetcherManager.scala:66)
at kafka.utils.ShutdownableThread.run(ShutdownableThread.scala:63)
[2016-03-11 00:39:17,013] WARN [console-consumer-79688_9dd5f575d557-1457656747003-f1ed369d-leader-finder-thread], Failed to find leader for Set([grillo,0]) (kafka.consumer.ConsumerFetcherManager$LeaderFin
derThread)
kafka.common.KafkaException: fetching topic metadata for topics [Set(grillo)] from broker [ArrayBuffer(BrokerEndPoint(1001,ba53d4fd7595,9092))] failed
at kafka.client.ClientUtils$.fetchTopicMetadata(ClientUtils.scala:73)
at kafka.client.ClientUtils$.fetchTopicMetadata(ClientUtils.scala:94)
at kafka.consumer.ConsumerFetcherManager$LeaderFinderThread.doWork(ConsumerFetcherManager.scala:66)
at kafka.utils.ShutdownableThread.run(ShutdownableThread.scala:63)
Caused by: java.nio.channels.ClosedChannelException
at kafka.network.BlockingChannel.send(BlockingChannel.scala:110)
at kafka.producer.SyncProducer.liftedTree1$1(SyncProducer.scala:75)
at kafka.producer.SyncProducer.kafka$producer$SyncProducer$$doSend(SyncProducer.scala:74)
at kafka.producer.SyncProducer.send(SyncProducer.scala:119)
at kafka.client.ClientUtils$.fetchTopicMetadata(ClientUtils.scala:59)
... 3 more

等等

在制作人的控制台,我写了几句:

$ docker run --rm --interactive klustera/kafka /opt/kafka_2.11-0.9.0.1/bin/kafka-console-producer.sh --topic grillo --broker-list 171.17.0.4:9092                                                           
Hola
¿Cómo estáń?
¿Todo bien?

过了一会儿,我收到了这样的回复:

[2016-03-11 00:39:28,955] ERROR Error when sending message to topic grillo with key: null, value: 4 bytes with error: Failed to update metadata after 60000 ms. (org.apache.kafka.clients.producer.internals.ErrorLoggingCallback)
[2016-03-11 00:40:28,956] ERROR Error when sending message to topic grillo with key: null, value: 16 bytes with error: Failed to update metadata after 60000 ms. (org.apache.kafka.clients.producer.internals.ErrorLoggingCallback)
[2016-03-11 00:41:28,956] ERROR Error when sending message to topic grillo with key: null, value: 12 bytes with error: Failed to update metadata after 60000 ms. (org.apache.kafka.clients.producer.internals.ErrorLoggingCallback)

并且在 docker-compose 日志

...
zookeeper_1 | 2016-03-11 00:39:07,072 [myid:] - INFO [ProcessThread(sid:0 cport:2181)::PrepRequestProcessor@651] - Got user-level KeeperException when processing sessionid:0x153631368b1000b type:create c
xid:0x2 zxid:0x47 txntype:-1 reqpath:n/a Error Path:/consumers Error:KeeperErrorCode = NodeExists for /consumers
zookeeper_1 | 2016-03-11 00:39:07,243 [myid:] - INFO [ProcessThread(sid:0 cport:2181)::PrepRequestProcessor@651] - Got user-level KeeperException when processing sessionid:0x153631368b1000b type:create c
xid:0x19 zxid:0x4b txntype:-1 reqpath:n/a Error Path:/consumers/console-consumer-79688/owners/grillo Error:KeeperErrorCode = NoNode for /consumers/console-consumer-79688/owners/grillo
zookeeper_1 | 2016-03-11 00:39:07,247 [myid:] - INFO [ProcessThread(sid:0 cport:2181)::PrepRequestProcessor@651] - Got user-level KeeperException when processing sessionid:0x153631368b1000b type:create $xid:0x1a zxid:0x4c txntype:-1 reqpath:n/a Error Path:/consumers/console-consumer-79688/owners Error:KeeperErrorCode = NoNode for /consumers/console-consumer-79688/owners
...

更新 2

至少在 docker-machine 中我让它工作了:

首先,我定义了一个名为docker-machine的变量:

DOCKER_VM=kafka_test

然后,我将docker-compose.yml修改如下:

KAFKA_ADVERTISED_HOST_NAME: "${DOCKER_MACHINE_IP}"

最后,在docker-machine的环境中,我执行:

DOCKER_MACHINE_IP=$(docker-machine ip $DOCKER_VM) docker-compose up -d

但在笔记本电脑上(我的意思是,不使用虚拟机,它不起作用)

最佳答案

我对这个问题的解决方案略有不同。我将 Kafka 配置为在 kafka 上做广告主机,因为它暴露在主机上 localhost:9092 ,我在 /etc/hosts 中添加一个条目对于 kafka解析为 localhost .通过这样做,可以从其他 Docker 容器和 localhost 访问 Kafka。

docker-compose.yml:

  my-web-service:
build: ./my-web-service
ports:
- "8000:8000"
links:
- kafka
kafka:
image: "wurstmeister/kafka:0.10.2.0"
ports:
- "9092:9092"
hostname: kafka
links:
- zookeeper
environment:
- KAFKA_ADVERTISED_HOST_NAME=kafka
- KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
- KAFKA_ADVERTISED_PORT=9092
zookeeper:
image: wurstmeister/zookeeper
ports:
- "2181:2181"

更新主机文件:

more /etc/hosts
127.0.0.1 localhost kafka

关于docker - Docker 中的 Kafka 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35861501/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com