Here’s a basic “Hello World” example for Apache Kafka using Docker and Docker Compose. This will set up a Kafka broker and a Zookeeper instance, allowing you to produce and consume messages.
docker-compose.yml
FileCreate a docker-compose.yml file that defines the services for Zookeeper and Kafka.
Run the following command to start the Kafka and Zookeeper containers:
cd devops-basics/topics/kafka/basic
docker-compose up -d
This command will start Zookeeper and Kafka in the background.
Once the containers are running, create a Kafka topic named helloworld
.
docker exec kafka kafka-topics.sh --create --topic helloworld --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
To send a message to the helloworld
topic:
docker exec -it kafka kafka-console-producer.sh --topic helloworld --bootstrap-server localhost:9092
Type a message (e.g., Hello, Kafka!
) and press Enter. This sends the message to the Kafka topic.
To read the message from the helloworld
topic:
docker exec -it kafka kafka-console-consumer.sh --topic helloworld --bootstrap-server localhost:9092 --from-beginning
You should see the message you produced earlier.
To stop and remove the Kafka and Zookeeper containers, run:
cd devops-basics/topics/kafka/basic
docker-compose down
This basic setup allows you to get hands-on experience with Kafka using Docker and Docker Compose. You can extend this setup to explore more advanced Kafka features.