<p>I am attempting to create a simple producer and consumer with two Python scripts, using Kafka deployed on Microk8s. However, when running the producer.py script, I get the following error on repeat:</p>
<pre><code>...|FAIL|rdkafka#producer-1| [thrd:...:9092/bootstrap]: ...:9092/bootstrap: Connect to ipv4#localhost:9092 failed: Connection refused (after 0ms in state CONNECT, ... identical error(s) suppressed
</code></pre>
<p>I am fairly confident that this issue is a result of the listeners not being configured correctly, but I have so far been unable to figure out what I need to do to fix them, due to what I assume is my complete lack of any knowledge in this area. I have <a href="https://strimzi.io/blog/2019/04/23/accessing-kafka-part-2/" rel="nofollow noreferrer">reviewed</a> <a href="https://www.confluent.io/blog/kafka-listeners-explained/" rel="nofollow noreferrer">these</a> <a href="https://stackoverflow.com/questions/57232424/configuring-listener-with-localhost-causes-a-failure-to-retrieve-meta-data-abo">resources</a>, in addition to several others from this site, but have been unable to find a solution, or at least a solution I can understand enough to act upon.</p>
<p><strong>Steps to Reproduce:</strong></p>
<p>The Python scripts to generate the producer and consumer can be found <a href="https://towardsdatascience.com/how-to-build-a-simple-kafka-producer-and-consumer-with-python-a967769c4742" rel="nofollow noreferrer">here</a>.</p>
<p>For Microk8s installation, I followed <a href="https://microk8s.io/docs/getting-started" rel="nofollow noreferrer">these</a> instructions. I also installed Helm, since my project requirements dictate that I use Helm charts.</p>
<p>I then installed Kafka using:</p>
<pre><code>helm repo add bitnami https://charts.bitnami.com/bitnami
helm install kafka-release bitnami/kafka
</code></pre>

wb1210
<p>The Python code in the linked post uses <code>'localhost:9092'</code>, as the error also shows - <code>Connect to ipv4#localhost:9092 failed</code></p>
<p>If you are trying to run that code in a k8s pod, then you need to give the external broker DNS addresses, not the local pod address.</p>
<p>If you run the Python code from outside the k8s cluster, you need to expose a ClusterIP / NodePort external service or Ingress (as the linked Strimzi post shows; plus, you can can still use Strimzi Operator with Helm, so you don't really need the Bitnami Charts).</p>
<hr />
<p>At a high level, the <code>advertisted.listeners</code> tells clients how to connect to a specific broker. If you advertise localhost, the pod will try to connect to itself, even if the bootstrap connection worked (setup by just <code>listeners</code>). If you advertise <code>kafka.svc.cluster.local</code>, then it will try to connect to the <code>kafka</code> service in the <code>default</code> namespace... But you still need to actually set <code>boostrap.servers = kafka.svc.cluster.local:9092</code>, for example.</p>
