Kafka Deployment Task
This tutorial follows the Zookeeper deployment and guides you through deploying Apache Kafka, a distributed event streaming platform. It is designed for students and junior DevOps engineers learning to deploy stateful, networked applications on Kubernetes with production-like configurations.
By completing this task, you will create a Kubernetes Deployment for Kafka with specific performance and configuration settings, and expose it for internal cluster communication with a ClusterIP Service.
In this tutorial, you will be tasked to:
Translate a detailed list of Kafka configuration needs into a Kubernetes manifest.
Configure environment variables for connectivity, listener protocols, and performance tuning (e.g., JVM heap size and log retention).
Create a
ClusterIPservice to enable communication within the Kubernetes cluster.Prepare and apply the manifests needed to deploy Kafka.
Before you start
Make sure that:
The Zookeeper Deployment and Service from the previous task are running successfully in the cluster.
You are SSHed into the
k3smainnode and have switched to the shared user account.
Part 1: Create the Kafka Deployment Manifest
Your first goal is to create a Kubernetes deployment file named kafka-deployment.yaml. This deployment will run the Kafka broker pod with a specific set of configurations.
Deployment Requirements
The Kafka instance must be configured with the following specifications:
Replica Count: The deployment should manage a single (
1) replica.Node Affinity: The pod must be scheduled to run on the node named
k3smain.Container Image: Use the specific
bitnami/kafka:3.9image.Resource Management: The container must be allocated specific CPU and memory resources:
Requests:
1.5Giof memory and1CPU.Limits:
2Giof memory and1CPU.
Storage: For this simulation, Kafka should use a non-persistent, ephemeral volume (
emptyDir) mounted at/bitnami/kafka.Port: The container must expose the standard Kafka port,
9092.Environment Configuration: The following environment variables must be set to configure the Kafka broker:
KAFKA_ZOOKEEPER_CONNECT_TIMEOUT_MS: Set to20000.KAFKA_CFG_ZOOKEEPER_CONNECT: Set tozookeeper-service:2181to connect to the Zookeeper service.ALLOW_PLAINTEXT_LISTENER: Must be set toyes.KAFKA_CFG_LISTENERS: Configure the internal listener toPLAINTEXT://:9092.KAFKA_CFG_ADVERTISED_LISTENERS: Advertise the in-cluster address asPLAINTEXT://kafka-service:9092.KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP: Map the listener to its protocol withPLAINTEXT:PLAINTEXT.KAFKA_HEAP_OPTS: Set the Java heap size with-Xms1536m -Xmx1536m.KAFKA_CFG_LOG_RETENTION_MS: Set to300000.KAFKA_CFG_LOG_SEGMENT_MS: Set to300000.KAFKA_CFG_LOG_SEGMENT_BYTES: Set to104857600.
Skeleton File for Deployment
Create a file named kafka-deployment.yaml and fill it out according to the detailed requirements above.
Part 2: Expose Kafka with a Service
Because Kafka's ADVERTISED_LISTENERS setting points to kafka-service, you need to create a service with that exact name so other pods in the cluster can resolve it. A ClusterIP service is appropriate for this internal-only communication.
Service Requirements
The Service must:
Have the name
kafka-service.Be of type
ClusterIP, which is the default service type.Expose port
9092and forward traffic totargetPort9092.Use a
selectorto correctly identify the Kafka pod (app: kafka).
Skeleton File for Service
Create a second file named kafka-service.yaml and complete the skeleton below.
Part 3: Deployment and Verification
Once you have created and completed both YAML files, apply them and check the logs to ensure the Kafka broker starts successfully and connects to Zookeeper.
Apply the manifests:
kubectl apply -f kafka-deployment.yamlkubectl apply -f kafka-service.yamlVerify the deployment:
kubectl get pods -l app=kafkaCheck for errors:
kubectl logs <pod name>
Once the deployment is successful, you can proceed to the Streamer task.