Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kafka in Kubernetes Cluster- How to publish/consume messages from outside of Kubernetes Cluster

  1. 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/
  2. 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.
  3. I am able to reach the kube-nodes from external machine - basically I can ping them using name/ip.
  4. I am not using any external load balancer but I have a DNS that can resolve both my external machine and kube-nodes.
  5. Using NodePort or ExternalIP to expose the Kafka service does not work in this case.
  6. Setting KAFKA_ADVERTISED_HOST_NAME or KAFKA_ADVERTISED_LISTENERS in Kafka RC YML that ultimately set ADVERTISED_HOST_NAME/ADVERTISED_LISTENERS properties in server.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!

like image 812
Manish Sinha Avatar asked Jan 26 '17 06:01

Manish Sinha


People also ask

How do I access Kafka outside Kubernetes cluster?

You can expose the Kafka cluster outside the Kubernetes cluster by declaring one or more externalListeners in the KafkaCluster custom resource. Above, externalListeners creates two external access points through which the Kafka cluster's brokers can be reached. These external listeners are registered in the advertized.

What command can be used to access a pod from outside of a Kubernetes cluster?

Pod/Container Port Not Exposed Use kubectl exec -it POD_NAME /bin/SHELL (where shell is often bash if it is installed in the container, or sh otherwise) to get a shell running inside the pod. The shell actually runs inside one of its containers.

How do you access pod outside cluster?

Ways to connect You have several options for connecting to nodes, pods and services from outside the cluster: Access services through public IPs. Use a service with type NodePort or LoadBalancer to make the service reachable outside the cluster. See the services and kubectl expose documentation.


1 Answers

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.

    ports:
    - containerPort: 9092
    - containerPort: 9093
    env:
    - name: KAFKA_ZOOKEEPER_CONNECT
      value: "zookeeper:2181"
    - name: KAFKA_LISTENER_SECURITY_PROTOCOL_MAP
      value: "INTERNAL_PLAINTEXT:PLAINTEXT,EXTERNAL_PLAINTEXT:PLAINTEXT"
    - name: KAFKA_ADVERTISED_LISTENERS
      value: "INTERNAL_PLAINTEXT://kafka-internal-service:9092,EXTERNAL_PLAINTEXT://123.us-east-2.elb.amazonaws.com:9093"
    - name: KAFKA_LISTENERS
      value: "INTERNAL_PLAINTEXT://0.0.0.0:9092,EXTERNAL_PLAINTEXT://0.0.0.0:9093"
    - name: KAFKA_INTER_BROKER_LISTENER_NAME
      value: "INTERNAL_PLAINTEXT"

Apart from that I configured two Services. One for internal(Headless) & one for external(LoadBalancer) communication.

Hopefully this will save people's time.

like image 127
Kamil Wojcik Avatar answered Oct 06 '22 03:10

Kamil Wojcik