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:
Step 2: Deploy Kafka
Deploy a Kafka cluster using Strimzi by applying a Kafka resource:
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
Consume Messages
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
| Feature | Description |
| Kafka on Kubernetes | Scalable, managed through Kubernetes orchestration. |
| Strimzi Operator | Simplifies deployment and management of Kafka within Kubernetes. |
| External Access | Achieved through external listeners; loadbalancer type exposes brokers publicly. |
| Security | Includes 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.

