Search code examples
pythondockerapache-kafkaminikubekafka-python

How to reach kafka from within container on minikube?


I'm setting up a python app, and I would like to produce a message for kafka. The kafka server is running locally on my computer (not in minikube) and the app.py runs in a container in minikube.

The code of app.py is this:

try:
    producer = KafkaProducer(bootstrap_servers=['addr:9092'])
    producer.send('flask.logs', json.dumps("HELLO").encode('utf-8'))
    producer.flush()
    print(f"[P-KAFKA] kafka log posted")
except Exception as e:
    print(f"[P-KAFKA] kafka bad posted")
    raise e

I tried to do few things like:

  1. with localhost in place of addr, I bridged port 9092 of localhost with port 9092 of minikube but this solution doesn't work.

  2. with 192.168.1.3 (my PC computer's IP) in place of addr it still doesn't work.

I don't know what to do.

--SOLUTION-- i used ngrok, basically ngrok forwards traffic from your local port, that you can choose, in a public adress. very cool. otherwise this is almost the same question : Calling an external service from within Minikube


Solution

  • The kafka server is running locally on my computer (not in minikube) and the app.py runs in a container in minikube.

    Then you don't need ngrok.

    Minikube routes request to the host machine. Your host machine contains a running broker. There's never a need for an external service, especially not routing over the internet.

    You simply need to use your host IP or DNS name in your Python code, preferably via an environment variable, say KAFKA_BOOTSTRAP.

    And you need to add the same address to your server.properties as an advertised.listeners address