<p>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.</p>

<p>Here is my spring boot configuration</p>

<pre><code>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"
</code></pre>

<p>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</p>

<pre><code>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.
</code></pre>

<p>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</p>

<p>Sometimes, it will actually be your Kafka Kubernetes deployment that is at fault.</p>
<p>Can I see your deployment YAML/JSON file for the Kafka deployment, please?</p>
<p>It should look like this:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-kafka
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kafka
      id: &quot;0&quot;
  template:
    metadata:
      labels:
        app: kafka
        id: &quot;0&quot;
    spec:
      containers:
        - name: kafka
          image: wurstmeister/kafka:latest
          ports:
            - containerPort: 30035
          env:
            - name: KAFKA_ADVERTISED_HOST_NAME
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP

            - name: KAFKA_ADVERTISED_PORT
              value: &quot;9092&quot;

            - name: KAFKA_HEAP_OPTS
              value: -Xms320m

            - name: KAFKA_ZOOKEEPER_CONNECT
              value: &quot;zookeeper:2181&quot;

---
apiVersion: v1
kind: Service
metadata:
  name: api-kafka
  namespace: default
  labels:
    name: kafka
spec:
  ports:
    - port: 9092
      name: kafka-port
      protocol: TCP
  selector:
    app: kafka
    id: &quot;0&quot;

  type: NodePort

</code></pre>
<p>With an emphasis on the:</p>
<pre><code>- name: KAFKA_ADVERTISED_HOST_NAME
  valueFrom:
      fieldRef:
          fieldPath: status.podIP
</code></pre>
<p>Make sure your profiles are right and that'll fix all of your problems to do with the connections and the advertised hostnames within a Kubernetes Deployment</p>
