How to Connect to kafka on localhost (host machine) from app inside kubernetes (minikube)

Viewed 432

I am trying to connect my springboot app (running inside minikube) to kafka on my localhost (ie, laptop).

I have tried many things, including headless services, services without selectors, updating minikube \etc\hosts, but nothing works yet.

I get error from spring boot saying No resolvable bootstrap urls given in bootstrap.servers

Can someone please point me to what I am doing wrong?

My Headless Service

apiVersion: v1
kind: Service
metadata:
  name: es-local-kafka
  namespace: demo
spec:
  clusterIP: None
---
apiVersion: v1
kind: Endpoints
metadata:
  name: es-local-kafka
subsets:
  - addresses:
      - ip: "10.0.2.2"
    ports:
      - name: "kafkabroker1"
        port: 9191
      - name: "kafkabroker2"
        port: 9192
      - name: "kafkabroker3"
        port: 9193

My application properties for kafka:

kafka.bootstrap-servers=${LOCALHOST}:9191,${LOCALHOST}:9192,${LOCALHOST}:9193

My Config Map:

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: null
  name: rr-config
  namespace: demo
data:
  LOCALHOST: es-local-kafka.demo.svc
2 Answers

Not sure how you are trying to connect service running on Minikube or on the local system and want to leverage kafka on minikube.

If your application running on local system and Kafka on minikube

you can connect the application to Kafka cluster with the IP of minikube also.

Here is good example : https://github.com/d1egoaz/minikube-kafka-cluster

Git clone : https://github.com/d1egoaz/minikube-kafka-cluster

cd minikube-kafka-cluster

kubectl apply -f 00-namespace/

kubectl apply -f 01-zookeeper/

kubectl apply -f 02-kafka/

kubectl apply -f 03-yahoo-kafka-manager/

kubectl get svc -n kafka-ca1 (Note the port of kafka 31445)

list the Ip of minikube

minikube ip 

Now from your local system to minikube kafka you can connect with, http://minikube-ip:port you will see UI of kafka manager in browser

If you are running sprint boot application on the minikube

If both services are running in same namespace you just have to use the service name only to connect

Only service name in sprint boot, if port required you can also pass it

es-local-kafka

try with passing full service also

<servicename>.<namespace>.svc.cluster.local

Headless service is for different purposes and service without a selector is weird in that case your service wont be able to connect to PODs.

I eventually got a fix, and doesn't need all the crazy stuff I was referring to in my question:

  1. You need to make sure your kafka broker is bound to 0.0.0.0 instead of 127.0.0.0 (localhost) . By default, in the single node kafka broker setup, this is what is used. I went with this, due to both time constraint, and the fact that this was just for a POC in my local (prod will have a specific dns-able kafka URL anyway, and no such localhost shenanigans needed)

  2. In the kafka URL in your application properties file, instead of localhost, you need to give ip as as the minikube ip. This is the same ip that you will get if you do the command minikube ip :)

Read more about how this works here: https://minikube.sigs.k8s.io/docs/handbook/host-access/

Related