Accessing kafka from spring boot application running on kubernetes

3/11/2019

I have a kafka installation running within in kubernetes cluster. I have a pod running a spring boot application which is using the default bootstrap.servers (localhost:9092) configuration and not the one passed in (bootstrap.kafka.svc.cluster.local:9092). The pod then fails to start as kafka is not running on localhost.

Here is my spring boot configuration

spring:
  kafka:
    consumer:
      group-id: spring-template
      auto-offset-reset: earliest
      bootstrap-servers: "bootstrap.kafka.svc.cluster.local:9092"
    producer:
      bootstrap-servers: "bootstrap.kafka.svc.cluster.local:9092"
    bootstrap-servers: "bootstrap.kafka.svc.cluster.local:9092"

When setting up the consumers on startup the group id and auto-offset-reset are being correctly passed in from the above configuration. However the bootstrap-servers configuration is not and the application is actually using localhost:9092 as per the log below

2019-03-11 07:34:36.826  INFO 1 --- [  restartedMain] o.a.k.clients.consumer.ConsumerConfig    : ConsumerConfig values:
        auto.commit.interval.ms = 5000
        auto.offset.reset = earliest
        bootstrap.servers = [localhost:9092]
        check.crcs = true
        client.id =
        connections.max.idle.ms = 540000
        default.api.timeout.ms = 60000
        enable.auto.commit = true
        exclude.internal.topics = true
        fetch.max.bytes = 52428800
        fetch.max.wait.ms = 500
        fetch.min.bytes = 1
        group.id = spring-template
        heartbeat.interval.ms = 3000
        interceptor.classes = []
        internal.leave.group.on.close = true
        isolation.level = read_uncommitted
        key.deserializer = class org.apache.kafka.common.serialization.StringDeserializer
        max.partition.fetch.bytes = 1048576
        max.poll.interval.ms = 300000
        max.poll.records = 500
        metadata.max.age.ms = 300000
        metric.reporters = []
        metrics.num.samples = 2
        metrics.recording.level = INFO
        metrics.sample.window.ms = 30000
        partition.assignment.strategy = [class org.apache.kafka.clients.consumer.RangeAssignor]
        receive.buffer.bytes = 65536
        reconnect.backoff.max.ms = 1000
        reconnect.backoff.ms = 50
        request.timeout.ms = 30000
        retry.backoff.ms = 100
        sasl.client.callback.handler.class = null
        sasl.jaas.config = null
        sasl.kerberos.kinit.cmd = /usr/bin/kinit
        sasl.kerberos.min.time.before.relogin = 60000
        sasl.kerberos.service.name = null
        sasl.kerberos.ticket.renew.jitter = 0.05
        sasl.kerberos.ticket.renew.window.factor = 0.8
        sasl.login.callback.handler.class = null
        sasl.login.class = null
        sasl.login.refresh.buffer.seconds = 300
        sasl.login.refresh.min.period.seconds = 60
        sasl.login.refresh.window.factor = 0.8
        sasl.login.refresh.window.jitter = 0.05
        sasl.mechanism = GSSAPI
        security.protocol = PLAINTEXT
        send.buffer.bytes = 131072
        session.timeout.ms = 10000
        ssl.cipher.suites = null
        ssl.enabled.protocols = [TLSv1.2, TLSv1.1, TLSv1]
        ssl.endpoint.identification.algorithm = https
        ssl.key.password = null
        ssl.keymanager.algorithm = SunX509
        ssl.keystore.location = null
        ssl.keystore.password = null
        ssl.keystore.type = JKS
        ssl.protocol = TLS
        ssl.provider = null
        ssl.secure.random.implementation = null
        ssl.trustmanager.algorithm = PKIX
        ssl.truststore.location = null
        ssl.truststore.password = null
        ssl.truststore.type = JKS
        value.deserializer = class org.apache.kafka.common.serialization.StringDeserializer

2019-03-11 07:34:36.942  INFO 1 --- [  restartedMain] o.a.kafka.common.utils.AppInfoParser     : Kafka version : 2.0.1
2019-03-11 07:34:36.945  INFO 1 --- [  restartedMain] o.a.kafka.common.utils.AppInfoParser     : Kafka commitId : fa14705e51bd2ce5
2019-03-11 07:34:37.149  WARN 1 --- [  restartedMain] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-1, groupId=spring-template] Connection to node -1 could not be established. Broker may not be available.

I have kubernetes service called bootstrap, running in namespace kafka in the kubernetes cluster. Here is a snippet of the log file. Why is the spring boot application not picking up the configured bootstrap.servers configuration

-- Khetho Mtembo
apache-kafka
kubernetes
spring-kafka

1 Answer

3/11/2019

The bootstrap-servers property expects a list of Kafka server addresses. By putting quotation marks around the value you signaled that the value is a string and therefore create a type conflict.

To solve this issue you should either remove the quotation marks or put the value explicitly in a list form. e.g:

spring:
  kafka:
    consumer:
      bootstrap-servers: ["bootstrap.kafka.svc.cluster.local:9092"]
-- Lukas Eichler
Source: StackOverflow