Connect to Kafka on host from Docker (ksqlDB) [closed]
Asked Answered
W

1

-1

I'm running ksqldb-server from a docker-compor found here https://ksqldb.io/quickstart.html#quickstart-content

My kafka bootstrap server is running on the same VM in standard alone mode. I can see the messages in one topic with a console consumer:

sudo kafka-avro-console-consumer --from-beginning --bootstrap-server localhost:9092 --topic source-air-input  --property print.key=true --max-messages 2 

Unfortunatly running ksql from docker gives me this error.

ksqldb-server    | [2021-07-15 23:12:58,772] ERROR Failed to start KSQL (io.confluent.ksql.rest.server.KsqlServerMain:66)
ksqldb-server    | java.lang.RuntimeException: Failed to get Kafka cluster information
ksqldb-server    |      at io.confluent.ksql.services.KafkaClusterUtil.getKafkaClusterId(KafkaClusterUtil.java:107)
ksqldb-server    |      at io.confluent.ksql.rest.server.KsqlRestApplication.buildApplication(KsqlRestApplication.java:624)
ksqldb-server    |      at io.confluent.ksql.rest.server.KsqlServerMain.createExecutable(KsqlServerMain.java:152)
ksqldb-server    |      at io.confluent.ksql.rest.server.KsqlServerMain.main(KsqlServerMain.java:59)
ksqldb-server    | Caused by: java.util.concurrent.TimeoutException
ksqldb-server    |      at org.apache.kafka.common.internals.KafkaFutureImpl$SingleWaiter.await(KafkaFutureImpl.java:108)
ksqldb-server    |      at org.apache.kafka.common.internals.KafkaFutureImpl.get(KafkaFutureImpl.java:272)
ksqldb-server    |      at io.confluent.ksql.services.KafkaClusterUtil.getKafkaClusterId(KafkaClusterUtil.java:105)

My docker-compose.yml is the following.

 ---
version: '3.9'

services:
  ksqldb-server:
    image: confluentinc/ksqldb-server:0.18.0
    hostname: ksqldb-server
    container_name: ksqldb-server
    extra_hosts:
      - "host.docker.internal:host-gateway"
    ports:
      - "8088:8088"
    environment:
      KSQL_LISTENERS: http://0.0.0.0:8088
      KSQL_BOOTSTRAP_SERVERS: host.docker.internal:9092
      KSQL_KSQL_LOGGING_PROCESSING_STREAM_AUTO_CREATE: "true"
      KSQL_KSQL_LOGGING_PROCESSING_TOPIC_AUTO_CREATE: "true"

  ksqldb-cli:
    image: confluentinc/ksqldb-cli:0.18.0
    container_name: ksqldb-cli
    depends_on:
      - ksqldb-server
    entrypoint: /bin/sh
    tty: true

I tried many possible configurations for the address without success. What might be wrong? I tried the suggestions from this question From inside of a Docker container, how do I connect to the localhost of the machine? without success.

Wilscam answered 15/7, 2021 at 23:20 Comment(9)
Kafka is not running inside the KSQL container, so the bootstrap cannot be localhostWeaner
@Weaner I thought about that, so, what is the address I shoud give?Wilscam
@Weaner it is not a duplicated post. I tried everything there.Wilscam
So, what error do you get with KSQL_BOOTSTRAP_SERVERS: docker.host.internal:9092? Note: You also, need to modify advertised.listemers to list the same.Weaner
Secondly: You don't really need Docker - you can install KSQL as an RPM ksqldb.io/quickstart-standalone-rpm.html#quickstart-contentWeaner
@Weaner I got this error, org.apache.kafka.common.config.ConfigException: Invalid value docker.host.internal:8088 for configuration listeners: Not valid URL: unknown protocol: docker.host.internalWilscam
@Weaner running standalone I got this error: Exception in thread "main" java.lang.BootstrapMethodError: java.lang.NoSuchMethodError:Wilscam
8088 is the KSQL REST endpoint, not the Kafka brokerWeaner
No solution again: Caused by: org.apache.kafka.common.config.ConfigException: No resolvable bootstrap urls given in bootstrap.serversWilscam
W
3

Modify Kafka's server.properties

listener.security.protocol.map=PLAINTEXT_DOCKER:PLAINTEXT,PLAINTEXT_LOCAL:PLAINTEXT

listeners=PLAINTEXT_DOCKER://:29092,PLAINTEXT_LOCAL://localhost:9092

advertised.listeners=PLAINTEXT_DOCKER://host.docker.internal:29092,PLAINTEXT_LOCAL://localhost:9092

inter.broker.listener.name=PLAINTEXT_LOCAL

Update your Compose like so to point at the host rather than itself

version: '3.9'

services:

  # TODO: add schema-registry
  #   environment: 
  #     SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: host.docker.internal:29092
  #   extra_hosts:
  #     - "host.docker.internal:host-gateway"

  # or any other Kafka client 
  ksqldb-server:
    image: confluentinc/ksqldb-server:0.18.0
    hostname: ksqldb-server
    container_name: ksqldb-server
    ports:
      - "8088:8088"
    extra_hosts:
      - "host.docker.internal:host-gateway"
    environment:
       KSQL_BOOTSTRAP_SERVERS: host.docker.internal:29092
       ...

(Tested on Mac), Getting /info endpoint of KSQL

http :8088/info
HTTP/1.1 200 OK
content-length: 133
content-type: application/json

{
    "KsqlServerInfo": {
        "kafkaClusterId": "ZH2-h1W_SaivCW0qa8DQGA",
        "ksqlServiceId": "default_",
        "serverStatus": "RUNNING",
        "version": "0.18.0"
    }
}

Replace all host.docker.internal above with the external hostname/IP of the machine, if Kafka is a remote server

Weaner answered 16/7, 2021 at 16:46 Comment(6)
Caused by: org.apache.kafka.common.config.ConfigException: No resolvable bootstrap urls given in bootstrap.serversWilscam
The host machine is CentOS 7Wilscam
If you are using Docker-for-Linux 20.10.0+, you can also use the host host.docker.internal if you started your Docker container with the --add-host host.docker.internal:host-gateway option.Weaner
docs.docker.com/compose/compose-file/compose-file-v3/…Weaner
I believe you need to change the version field in the compose file to be 3 (or 3.9) for the extra_hosts to workWeaner
Ok, it seems to have worked when I changed the port to 29092.Wilscam

© 2022 - 2024 — McMap. All rights reserved.