Kubernetes
Kafka
Message Publishing
Cluster Management
Microservices

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

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Apache Kafka is a robust event streaming platform that allows you to manage large streams of data efficiently. Kafka is known for its high throughput, reliability, and replication capabilities. Running Kafka in a Kubernetes cluster enhances its scalability and makes it easier to manage with Kubernetes orchestration. However, a common operational requirement is the ability to interact with Kafka from outside the Kubernetes cluster, whether for the purposes of data integration, monitoring, or management of the service.

Architecture Considerations

When deploying Kafka on Kubernetes, it is essential to consider both internal and external communication strategies. Internally, Kafka pods should communicate with one another for replication and other cluster operations, typically managed by Kubernetes services. Externally, you need mechanisms to allow applications outside of the Kubernetes cluster to publish to and consume from Kafka.

Setting Up Kafka in Kubernetes

You can set up Kafka in Kubernetes manually or by using operators like Strimzi, which simplify the process of running Kafka on Kubernetes. For this article, we will focus on using Strimzi for simplicity and effectiveness.

Step 1: Install Strimzi

First, you need to install the Strimzi Kafka operator in your Kubernetes cluster. This operator makes it easier to manage Kafka's lifecycle:

bash
kubectl create namespace kafka
kubectl apply -f 'https://strimzi.io/install/latest?namespace=kafka' -n kafka

Step 2: Deploy Kafka

Deploy a Kafka cluster using Strimzi by applying a Kafka resource:

yaml
1apiVersion: kafka.strimzi.io/v1beta1
2kind: Kafka
3metadata:
4  name: my-cluster
5spec:
6  kafka:
7    version: 2.7.0
8    replicas: 3
9    listeners:
10      plain: {}
11      external:
12        type: loadbalancer
13        tls: false
14    config:
15      offsets.topic.replication.factor: 3
16      transaction.state.log.replication.factor: 3
17      transaction.state.log.min.isr: 2
18  zookeeper:
19    replicas: 3
20  entityOperator:
21    topicOperator: {}
22    userOperator: {}

This YAML file describes a Kafka cluster with three brokers and three ZooKeeper instances, which is enough for most production scenarios.

External Access - Publishing and Consuming Messages

The key to interaction from outside Kubernetes is the configuration of the Kafka listeners, specifically the external listener.

Configuring Load Balancers

In the provided example, we used type: loadbalancer, which instructs Kubernetes to provision a load balancer for each Kafka broker in your cluster through the cloud provider's load balancing feature. This configuration automatically assigns a public IP address to each broker.

Publishing and Consuming Messages

Now, with the Kafka cluster running and each broker accessible via its own public IP, external clients can publish and consume messages. Here's an example using Kafka's command-line tools:

Publish Messages

bash
bin/kafka-console-producer.sh --broker-list EXTERNAL-IP1:9094,EXTERNAL-IP2:9094 --topic test

Consume Messages

bash
bin/kafka-console-consumer.sh --bootstrap-server EXTERNAL-IP1:9094 --topic test --from-beginning

Security Considerations

Opening Kafka to the public internet includes potential security risks. Consider the following security mechanisms:

  • Authentication and Authorization: Leveraging TLS and SASL for secure, authenticated communication.
  • Network Policies: Defining Kubernetes network policies to restrict which clients can communicate with your Kafka brokers.
  • Encryption: Utilizing encryption in transit to protect your data.

Summary Table

FeatureDescription
Kafka on KubernetesScalable, managed through Kubernetes orchestration.
Strimzi OperatorSimplifies deployment and management of Kafka within Kubernetes.
External AccessAchieved through external listeners; loadbalancer type exposes brokers publicly.
SecurityIncludes TLS, SASL, and Kubernetes network policies for secure external access.

By understanding these components and their configurations, you can effectively set up and manage Kafka within a Kubernetes cluster while ensuring that external applications can securely publish to and consume from your Kafka clusters.


Course illustration
Course illustration

All Rights Reserved.