2

I have set up a Kafka cluster with docker-compose file where i specify the brokers like this:

kafka1:
    image: confluentinc/cp-kafka:latest
    hostname: kafka1
    ports:
      - "19092:19092"
    depends_on:
      - zookeeper-1
      - zookeeper-2
      - zookeeper-3
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper-1:12181,zookeeper-2:12181,zookeeper-3:12181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka1:19092

Now when i try to create topic with js script and i refer to the brokers like follows(for the sake of this post i only attach code about 1 broker):

try
    {
        const kafka = new Kafka({
            clientId: 'myapp',
            brokers: ['kafka1:19092','kafka2:29092','kafka3:39092']
        })

I get this error:

{"level":"ERROR","timestamp":"2020-03-21T19:06:13.653Z","logger":"kafkajs","message":"[Connection] Connection error: getaddrinfo ENOTFOUND kafka1","broker":"kafka1:19092","clientId":"myapp","stack":"Error: getaddrinfo ENOTFOUND kafka1\n    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26)"}

I feel like i have exhausted internet trying to troubleshoot this. Anyone have any suggestions how to connect to the broker in the docker?

Itzblend
  • 107
  • 2
  • 9

1 Answers1

2

See Robin's blog in the comments.

Otherwise, your code will only work within a docker container because you're referring to the Docker service names, which are not resolvable by your DNS server

In other words 1) adjust the advertised listeners (see blog) to include localhost 2) use localhost in your code when it runs outside a container

Note: 3 brokers on one machine isn't improving anything

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245