Bo G.
Bo G.

Reputation: 107

How to fix `kafka: client has run out of available brokers to talk to (Is your cluster reachable?)` error

I am developing an application which reads a message off of an sqs queue, does some stuff with that data, and takes the result and publishes to a kafka topic. In order to test locally, I'd like to set up a kafka image in my docker build. I am currently able to spin up aws-cli, localstack, and my app's containers locally using docker-compose. Separately, I am able to spin up kafka and zookeper without a problem as well. I am unable to get my application to communicate with kafka.

I've tried using two separate compose files, and also fiddled with the networks. Finally, I've referenced: https://rmoff.net/2018/08/02/kafka-listeners-explained/.

Here is my docker-compose file:

version: '3.7'
services:
  localstack:
    image: localstack/localstack:latest
    container_name: localstack
    env_file: .env
    ports:
      # Localstack endpoints for various API. Format is localhost:container
      - '4563-4584:4563-4584'
      - '8080:8080'
    environment:
      - SERVICES=sns:4575,sqs:4576
      - DATA_DIR=/tmp/localstack/data
    volumes:
      # store data locally in 'localstack' folder
      - './localstack:/tmp/localstack'
    networks:
      - my_network

  aws:
    image: mesosphere/aws-cli
    container_name: aws-cli
    # copy local JSON_DATA folder contents into aws-cli container's app folder
    #volumes:
    #  - ./JSON_DATA:/app
    env_file: .env
    # bash entrypoint needed for multiple commands
    entrypoint: /bin/sh -c
    command: >
      " sleep 10;
        aws --endpoint-url=http://localstack:4576 sqs create-queue --queue-name input_queue;
        aws --endpoint-url=http://localstack:4575 sns create-topic --name input_topic;
        aws --endpoint-url=http://localstack:4575 sns subscribe --topic-arn arn:aws:sns:us-east-2:123456789012:example_topic --protocol sqs --notification-endpoint http://localhost:4576/queue/input_queue; "
    networks:
      - my_network
    depends_on:
      - localstack

  my_app:
    build: .
    image: my_app
    container_name: my_app
    env_file: .env
    ports:
      - '9000:9000'
    networks:
      - my_network
    depends_on:
      - localstack
      - aws

  zookeeper:
    image: confluentinc/cp-zookeeper:5.0.0
    container_name: zookeeper
    ports:
      - 2181:2181
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
    networks:
      - my_network

  kafka:
    image: confluentinc/cp-kafka:5.0.0
    ports:
      - 9092:9092
    depends_on:
      - zookeeper
    environment:
      # For more details see See https://rmoff.net/2018/08/02/kafka-listeners-explained/
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: INSIDE://localhost:9092
      KAFKA_LISTENERS: INSIDE://localhost:9092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INSIDE:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: INSIDE
      KAFKA_CREATE_TOPICS: "output_topic:2:2"
    networks:
      - my_network

networks:
  my_network:

I would hope to see no errors as a result of publishing to this topic. Instead, I'm getting:

kafka: client has run out of available brokers to talk to (Is your cluster reachable?)

Any ideas what I may be doing wrong? Thank you for your help.

Upvotes: 0

Views: 14907

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191874

You've made the broker only resolvable within the Kafka container itself (or from your host to the container) by setting the listeners only to localhost.

If you want another Docker service to be able to reach that container, you'll have to add <some protocol>://kafka:<some port> to the advertised listeners, and make the listeners as not localhost

Where the protocol is also added to KAFKA_LISTENER_SECURITY_PROTOCOL_MAP

FWIW, That blog should cover all those bases.

Upvotes: 3

Related Questions