<p>My setup is like, I have a producer service provisioned as part of minikube cluster and it is trying to publish messages to a kafka instance running on the host machine.</p>

<p>I have written a kafka service and endpoints yaml as follows:</p>

<pre><code>kind: Service
apiVersion: v1
metadata:
  name: kafka
spec:
  ports:
    - name: "broker"
      protocol: "TCP"
      port: 9092
      targetPort:  9092
      nodePort: 0
---
kind: Endpoints
apiVersion: v1
metadata:
  name: kafka
  namespace: default
subsets:
  - addresses:
      - ip: 10.0.2.2
    ports:
      - name: "broker"
        port: 9092
</code></pre>

<p>The ip address of the host machine from inside the minikube cluster mentioned in the endpoint is acquired from the following command:</p>

<pre><code>minikube ssh "route -n | grep ^0.0.0.0 | awk '{ print \$2 }'"
</code></pre>

<p>The problem I am facing is that the topic is getting created when producer tries to publish message for the first time but no messages are getting written on to that topic.</p>

<p>Digging into the pod logs, I found that producer is trying to connect to kafka instance on localhost or something (not really sure of it).</p>

<pre><code>2020-05-17T19:09:43.021Z [warn] org.apache.kafka.clients.NetworkClient [] - 
[Producer clientId=/system/sharding/kafkaProducer-greetings/singleton/singleton/producer] 
Connection to node 0 (omkara/127.0.1.1:9092) could not be established. Broker may not be available.
</code></pre>

<p>Following which I suspected that probably I need to modify <code>server.properties</code> with the following change:</p>

<pre><code>listeners=PLAINTEXT://localhost:9092
</code></pre>

<p>This however resulted in the change in the ip address in the log:</p>

<pre><code>2020-05-17T19:09:43.021Z [warn] org.apache.kafka.clients.NetworkClient [] - 
[Producer clientId=/system/sharding/kafkaProducer-greetings/singleton/singleton/producer] 
Connection to node 0 (omkara/127.0.0.1:9092) could not be established. Broker may not be available.
</code></pre>

<p>I am not sure what ip address must be mentioned here? Or what is an alternate solution? And if it is even possible to connect from inside the kubernetes cluster to the kafka instance installed outside the kubernetes cluster.</p>

<p>Since producer kafka client is on the very same network as the brokers, we need to configure an additional listener like so:</p>

<pre><code>listeners=INTERNAL://0.0.0.0:9093,EXTERNAL://0.0.0.0:9092
listener.security.protocol.map=INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT
advertised.listeners=INTERNAL://localhost:9093,EXTERNAL://10.0.2.2:9092
inter.broker.listener.name=INTERNAL
</code></pre>

<p>We can verify messages in topic like so:</p>

<pre><code>kafka-console-consumer.sh --bootstrap-server INTERNAL://0.0.0.0:9093 --topic greetings --from-beginning
{"name":"Alice","message":"Namastey"}
</code></pre>

<p>You can find a detailed explaination on understanding &amp; provisioning kafka listeners <a href="https://rmoff.net/2018/08/02/kafka-listeners-explained/" rel="nofollow noreferrer">here</a>.</p>
