Could not resolve "kafka: 9092": name or service unknown - docker / php-rdkafka

I am trying to connect php to kafka everything in a docker container.

kafka php lib - https://github.com/arnaud-lb/php-rdkafka/

kockka docker container - https://hub.docker.com/r/wurstmeister/kafka/

Everything works and works fine, but when I try to connect to the PHP producer I get this:

httpd_1   | %3|1490816385.542|FAIL|rdkafka#producer-1| [thrd:kafka:9092/bootstrap]: kafka:9092/bootstrap: Failed to resolve 'kafka:9092': Name or service not known
httpd_1   | %3|1490816385.543|ERROR|rdkafka#producer-1| [thrd:kafka:9092/bootstrap]: kafka:9092/bootstrap: Failed to resolve 'kafka:9092': Name or service not known
httpd_1   | %3|1490816385.543|ERROR|rdkafka#producer-1| [thrd:kafka:9092/bootstrap]: 1/1 brokers are down

      

I am using the following in PHP

    $rk = new RdKafka\Producer();
    $rk->setLogLevel(LOG_DEBUG);
    $rk->addBrokers("kafka");

    $topicConf = new RdKafka\TopicConf();
    $topicConf->set("message.timeout.ms", 1000);
    $topic = $rk->newTopic("DEV", $topicConf);

    $topic->produce(RD_KAFKA_PARTITION_UA, 0, "Message");

    $rk->poll(1000);
    $kafkaConf = new RdKafka\Conf();
    $kafkaConf->setErrorCb(function ($rk, $err, $reason) {
        printf("Kafka error: %s (reason: %s)\n", rd_kafka_err2str($err), $reason);
    });
    $kafkaConf->setDrMsgCb(function ($rk, $message) {
        if ($message->err) {
            print_r($message);
        } else {
            print_r("ok");
        }
    });

      

I've been playing around trying to set the host IP in both docker-compose.yml and PHP code, but no joy. I also had it connection refused

, but I don't know if it's better or worse?

If it helps my docker-compose.yml

httpd:
  build: .
  ports:
  - 8180:80
  volumes:
  - ~/www:/var/www/html
zookeeper:
  image: wurstmeister/zookeeper
  ports:
  - "2181:2181"
kafka:
  build: ~/kafka-docker/.
  links:
  - zookeeper
  ports:
  - "9092:9092"
  environment:
    KAFKA_ADVERTISED_HOST_NAME: kafka
    KAFKA_ADVERTISED_PORT: 9092
    KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
  volumes:
  - /var/run/docker.sock:/var/run/docker.sock

      

  • PHP version: 7.0.17

  • librdkafka version: 0.9.5.0

  • php-rdkafka version: 2.0.1

My question is: Any idea how I can successfully connect to kafka from php?

+4


source to share


1 answer


Brokers will advertise themselve using advertised .listeners (which appear to be abstracted from KAFKA_ADVERTISED_HOST_NAME in this docker image) and hence clients will try to connect to those advertised hosts and ports.



Thus, you need to ensure that the client can resolve and achieve these declared hostnames, eg. by adding "kafka" to / etc / hosts on the client host.

+3


source







All Articles