Three ways to run the Kafka Producer API:
Prerequisites: Kafka running in Minikube
./deploy-minikube.shminikube service kafka-producer-api --url
# Output: http://127.0.0.1:XXXXXURL=$(minikube service kafka-producer-api --url)
curl $URL/health
curl -X POST $URL/api/publish -H "Content-Type: application/json" -d '{"topic":"test-topic","message":"Hello"}'# Bump version in deployment.yaml (v3 -> v4)
docker build --no-cache -t kafka-producer-api:v4 .
minikube image load kafka-producer-api:v4
# Edit k8s/deployment.yaml: change image tag to v4
kubectl apply -f k8s/deployment.yamlPrerequisites:
- Node.js installed
- Kafka accessible (use port-forward if in Kubernetes)
kubectl port-forward svc/kafka-sasl 9092:9092Edit .env:
KAFKA_BROKERS=localhost:9092
KAFKA_SASL_USERNAME=kafka-client
KAFKA_SASL_PASSWORD=client-secretnpm install
npm startcurl http://localhost:3000/health
curl -X POST http://localhost:3000/api/publish \
-H "Content-Type: application/json" \
-d '{"topic":"test-topic","message":"Hello"}'Ctrl+C
Prerequisites: Docker installed
docker build -t kafka-producer-api:local .# If Kafka is in Kubernetes
kubectl port-forward svc/kafka-sasl 9092:9092
# Run container (in another terminal)
docker run --rm -p 3000:3000 \
-e KAFKA_BROKERS=host.docker.internal:9092 \
-e KAFKA_SASL_USERNAME=kafka-client \
-e KAFKA_SASL_PASSWORD=client-secret \
kafka-producer-api:localcurl http://localhost:3000/health
curl -X POST http://localhost:3000/api/publish \
-H "Content-Type: application/json" \
-d '{"topic":"test-topic","message":"Hello"}'Ctrl+C
| Method | Pros | Cons | Best For |
|---|---|---|---|
| Minikube | Production-like, auto-restart, service discovery | Requires K8s knowledge | Production simulation |
| Local Machine | Fast iteration, easy debugging | Manual start/stop | Development |
| Docker | Isolated environment, consistent | Extra build step | Testing |
# Minikube
kubectl logs -l app=kafka-producer-api -f
# Local/Docker
# Logs appear in terminal# All methods
curl http://localhost:3000/health
# or
curl $(minikube service kafka-producer-api --url)/health# Replace URL with your endpoint
curl -X POST http://localhost:3000/api/publish \
-H "Content-Type: application/json" \
-d '{"topic":"test-topic","message":"Test message"}'- Minikube: Check pod is running:
kubectl get pods -l app=kafka-producer-api - Local: Ensure port-forward is running:
kubectl port-forward svc/kafka-sasl 9092:9092 - Docker: Use
host.docker.internalinstead oflocalhost
- Verify credentials in
.envmatch Kafka configuration - Check Kafka JAAS config:
kubectl exec -it <kafka-pod> -- cat /etc/kafka/secrets/jaas.conf
- Using Kubernetes Kafka? Must use port-forward to localhost:9092
- Or add to
/etc/hosts:echo "127.0.0.1 kafka-sasl" | sudo tee -a /etc/hosts
| Variable | Description | Example |
|---|---|---|
KAFKA_BROKERS |
Kafka broker addresses | localhost:9092 or kafka-sasl:9092 |
KAFKA_SASL_USERNAME |
SASL username | kafka-client |
KAFKA_SASL_PASSWORD |
SASL password | client-secret |
KAFKA_SASL_ENABLED |
Enable SASL auth | true |
KAFKA_DEFAULT_TOPIC |
Default topic | test-topic |
PORT |
API server port | 3000 |
- ✅ Choose your deployment method
- ✅ Start the API
- ✅ Test with curl or Postman
- ✅ Check README.md for full API documentation
API Docs: GET /api/docs