This guide will help you set up a Kafka cluster with 3 brokers on Minikube using Strimzi. It includes all steps, from setting up Minikube to interacting with Kafka topics, producers, and consumers.
Before starting, ensure you have the following installed:
- Minikube: Install Minikube
- Kubectl: Install Kubectl
- Helm (optional, if needed for installation): Install Helm
Run Minikube to start a Kubernetes cluster:
minikube start --memory=8192 --cpus=4Strimzi is used to manage Kafka on Kubernetes. Run the following to install it:
kubectl create namespace kafka
kubectl apply -f https://strimzi.io/install/latest | kubectl apply -f -Create a file called kafka-cluster-3nodes.yaml and paste the following YAML:
apiVersion: kafka.strimzi.io/v1beta2
kind: Kafka
metadata:
name: my-cluster
namespace: kafka
spec:
kafka:
version: 3.9.0
replicas: 3
listeners:
- name: plain
port: 9092
type: internal
tls: false
- name: tls
port: 9093
type: internal
tls: true
config:
offsets.topic.replication.factor: 3
transaction.state.log.replication.factor: 3
transaction.state.log.min.isr: 2
default.replication.factor: 3
min.insync.replicas: 2
log.message.format.version: "3.9"
storage:
type: persistent-claim
size: 5Gi
deleteClaim: false
zookeeper:
replicas: 3
storage:
type: persistent-claim
size: 5Gi
deleteClaim: false
entityOperator:
topicOperator: {}
userOperator: {}Apply the YAML to Kubernetes:
kubectl apply -f kafka-cluster-3nodes.yaml -n kafkaCheck the status of your pods:
kubectl get pods -n kafkaYou should see Kafka and Zookeeper pods running.
Check the Kafka custom resource:
kubectl get kafka -n kafkaYou should see the cluster listed as READY.
To connect to Kafka from outside Minikube, expose the Kafka service by port-forwarding:
kubectl port-forward -n kafka svc/my-cluster-kafka-bootstrap 9092:9092You can connect to any of the Kafka brokers to run Kafka commands. For example, connect to my-cluster-kafka-0:
kubectl exec -it my-cluster-kafka-0 -n kafka -- bashkafka-topics.sh --bootstrap-server localhost:9092 --create --topic my-test-topic --partitions 3 --replication-factor 2kafka-topics.sh --bootstrap-server localhost:9092 --listkafka-console-producer.sh --bootstrap-server localhost:9092 --topic my-test-topicType your messages and hit Enter. Press CTRL+C to exit.
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic my-test-topic --from-beginningYou should see the messages you produced.
kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic my-test-topicOnce you're done, delete the Kafka cluster:
kubectl delete kafka my-cluster -n kafkaYou now have a 3-broker Kafka cluster running in Minikube with Strimzi. You can interact with it, create topics, produce and consume messages.
Feel free to modify the kafka-cluster-3nodes.yaml to scale the number of brokers, change replication settings, or customize your deployment.
Enjoy your Kafka experience!