I’m running Kafka through Zookeeper on my external local Linux machine and my NodeJs application is running in a Pod of Minikube on the same machine.
I set the code up like below:
server.ts:
const fanoutKafka = new Kafka({
clientId: "timeline",
brokers: ["host.minikube.internal:9092"],
});
export const fanoutProducer = fanoutKafka.producer();
fanoutProducer.connect();
timeline.ts
fanoutProducer.send({
topic: kafka_fanout_topic,
messages: [
{
key: msg.postId,
value: JSON.stringify(msg)
}
]
})
It connects to Kafka Producer in server.ts
, but it fails when I try to publish from ‘timeline.ts’ inside the pod. But if I try with 127.0.0.1:9092
and npm start
it works without any issue.
As well as if I try to like this from minikube ssh
it works too:
docker@minikube:~$ nc -vz host.minikube.internal 9092
Connection to host.minikube.internal (192.168.49.1) 9092 port [tcp/*] succeeded!
Here is the full stacktrace from the pod:
KafkaJSNonRetriableError
Caused by: KafkaJSConnectionError: Connection error: connect ECONNREFUSED 127.0.1.1:9092
at Socket.onError (/usr/src/app/node_modules/kafkajs/src/network/connection.js:210:23)
... 3 lines matching cause stack trace ...
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
name: 'KafkaJSNumberOfRetriesExceeded',
retriable: false,
helpUrl: undefined,
retryCount: 5,
retryTime: 14476,
[cause]: KafkaJSConnectionError: Connection error: connect ECONNREFUSED 127.0.1.1:9092
at Socket.onError (/usr/src/app/node_modules/kafkajs/src/network/connection.js:210:23)
at Socket.emit (node:events:519:28)
at emitErrorNT (node:internal/streams/destroy:169:8)
at emitErrorCloseNT (node:internal/streams/destroy:128:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
retriable: true,
helpUrl: undefined,
broker: 'some-random-name',
code: 'ECONNREFUSED',
[cause]: undefined
}
Wondering how it resolves to 127.0.1.1 while Producing an event?
NB: Like I said Procuder.connect()
is fine.