- I have Kafka deployed and running in Kubernetes cluster. I am using this image from docker hub - https://hub.docker.com/r/cloudtrackinc/kubernetes-kafka/
- I have 3 kube-nodes in my kubernetes cluster. I have 3 Kafka and 3 zookeeper applications running and I have services zoo1,zoo2,zoo3 and kafka-1, kafka-2 and kafka-3 running corresponding to them. I am able to publish/consume from inside kubernetes cluster but I am not able to publish/consume from outside of kubernetes cluster i.e., from external machine not part of kubernetes cluster.
- I am able to reach the kube-nodes from external machine - basically I can ping them using name/ip.
- I am not using any external load balancer but I have a DNS that can resolve both my external machine and kube-nodes.
- Using NodePort or ExternalIP to expose the Kafka service does not work in this case.
- Setting
KAFKA_ADVERTISED_HOST_NAME
orKAFKA_ADVERTISED_LISTENERS
in Kafka RC YML that ultimately setADVERTISED_HOST_NAME
/ADVERTISED_LISTENERS
properties inserver.properties
either does not help accessing kafka from outside of kubernetes cluster.
Please suggest how can I publish/consume from outside of kubernetes cluster. Thanks much!
I solved this problem by using Confluent's Kafka REST proxy image.
https://hub.docker.com/r/confluentinc/cp-kafka-rest/
Documentation of the REST Proxy is here:
http://docs.confluent.io/3.1.2/kafka-rest/docs/index.html
Step A: Build a Kafka broker docker image using latest Kafka version
I used a custom built Kafka broker image based on the same image you used. You basically just need to update cloudtrackinc's image to use Kafka version 0.10.1.0 or otherwise it won't work. Just update the Dockerfile from cloudertrackinc's image to use the latest wurstmeister kafka image and rebuild the docker image.
I set the ADVERTISED_HOST_NAME for each Kafka broker to POD's IP so each broker gets an unique URL.
Step B: Setup cp-kafka-rest proxy to use your Kafka broker cluster
Kafka Rest Proxy must be running within the same cluster as your Kafka broker cluster.
You need to provide two environment variables to the cp-kafka-rest image at the minimum for it to run. KAFKA_REST_HOST_NAME and KAFKA_REST_ZOOKEEPER_CONNECT. You can set KAFKA_REST_HOST_NAME to use POD's IP.
Step C: Expose the Kafka REST proxy as a service
spec: type: NodePort or LoadBalancer ports: - name: kafka-rest-port port: 8082 protocol: TCP
You can use NodePort or LoadBalancer to utilize single or multiple Kafka REST Proxy pods.
Pros and Cons of using Kafka REST proxy
Pros:
Cons:
So if you can live with the issues above, then give Kafka Rest Proxy a try.
I had the same problem with accessing kafka from outside of k8s cluster on AWS. I manage to solve this issue by using kafka listeners feature which from version 0.10.2 supports multiple interfaces.
here is how I configured kafka container.
Apart from that I configured two Services. One for internal(Headless) & one for external(LoadBalancer) communication.
Hopefully this will save people's time.
This seems not to be possible at the moment, the network architecture of kafka is pretty poor regarding to this topic. The new consumer uses a list of brokers, which return the host of the zookeeper, but unfortunately this is in a different network, so it is not possible to reach it from your local client. The poor part of kafka is, that is not possible to specify the brokers AND the zookeeper servers. This prevents clients accessing the system from outside.
We worked around this for the moment using a busybox, where we installed tools to interact with kafka. In our case plunger
I was able to solve my problem by doing the following changes -
Using NodeSelector in YML to make kafka pod run on a particular node of kube cluster.
Set
KAFKA_ADVERTISED_HOST_NAME
to Kube hostName where this Kafka POD has been configured to run on ( as configured in step 1 )Expose Kafka Service using NodePort and set POD port same as that of exposed NodePort as shown below -
Now, you can access Kafka brokers from outside of kube cluster using host:exposedPort