Search code examples
spring-bootkubernetesminikube

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


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

Solution

  • I eventually got a fix, and it's simpler than I thought:

    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/