Search code examples
pythonkubernetesapache-kafkakubernetes-helmmicrok8s

Microk8s Kafka Producer Connection Failure


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:

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

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 reviewed these resources, 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.

Steps to Reproduce:

The Python scripts to generate the producer and consumer can be found here.

For Microk8s installation, I followed these instructions. I also installed Helm, since my project requirements dictate that I use Helm charts.

I then installed Kafka using:

helm repo add bitnami https://charts.bitnami.com/bitnami
helm install kafka-release bitnami/kafka

Solution

  • The Python code in the linked post uses 'localhost:9092', as the error also shows - Connect to ipv4#localhost:9092 failed

    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.

    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).


    At a high level, the advertisted.listeners 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 listeners). If you advertise kafka.svc.cluster.local, then it will try to connect to the kafka service in the default namespace... But you still need to actually set boostrap.servers = kafka.svc.cluster.local:9092, for example.