This guide provides instructions on how to deploy Kafka on a Kubernetes cluster using K3s, and how to run a simple Kafka producer and consumer in Python.
- A Kubernetes cluster (K3s recommended).
kubectl
configured to interact with your Kubernetes cluster.- Python 3 installed with the
kafka-python
library.
- Create a Kubernetes YAML file for deploying Zookeeper.
- Apply the YAML file to your Kubernetes cluster using the command:
kubectl apply -f <your-zookeeper-deployment.yaml>
- Verify that the Zookeeper service and pod are running in the
kafka
namespace.
- Create a Kubernetes YAML file for deploying the Kafka broker.
- Apply the YAML file to your Kubernetes cluster using the command:
kubectl apply -f <your-kafka-deployment.yaml>
- Verify that the Kafka service and pod are running in the
kafka
namespace.
- Ensure that the
kafka-python
library is installed. If not, install it using:pip install kafka-python
- Create two Python scripts: one for User 1 and one for User 2.
- In each script, configure the producer and consumer to connect to your Kafka broker.
- Run the Python script for User 1 in one terminal.
- Run the Python script for User 2 in another terminal.
- Start sending and receiving messages between User 1 and User 2.
- To clean up your Kubernetes resources, delete the Kafka and Zookeeper deployments:
kubectl delete -f <your-kafka-deployment.yaml> kubectl delete -f <your-zookeeper-deployment.yaml>