Running Apache Kafka on Windows

I recently wrote a post about using Kafka with .NET, and in that post, I mentioned that I found it challenging to get Kafka running. But I didn’t explain in detail how I did it.

In this post, I walk through the process I used to get Apache Kafka running on Windows.

It’s not easy to figure out

Firstly, it wasn’t easy to figure out how to run Kafka locally. There seem to be many ways to do it. Some require you to install software you might not want for any other reason, they require multiple environment variables to be set, and they require you to run a long series of commands to get it running.

So, if you have struggled with this, you are probably not alone.

Here is an easy way

Install Docker Desktop

I love using Docker to run software locally. It makes it easy to install tools such as MSSQL, Redis, and CockroachDB, and easy to remove them.

It’s a tool I recommend for all developers who want to experiment with new software.

Follow the instructions to install Docker Desktop.

Start Docker Desktop and make sure it’s running.

Install Confluent CLI

There are many Docker images for Kafka, but I suggest not using them directly.

Instead, use the Confluent CLI for Windows. It’s a single executable that you can download and run.

You need to add the Confluent executable to your path, or you can run it from the folder where you unzipped it.

Install and start Kafka

That’s all the configuration you need to do. Now you can start Kafka with a single command.

Open a command prompt and run -

confluent local kafka start

This will pull the Kafka Docker image, start it, and show you the ports it’s running on.

+-----------------+-------+
| Kafka REST Port | 8082  |
| Plaintext Ports | 64886 |
+-----------------+-------+

The Plaintext Ports is the one you need to connect to Kafka from your applications.

If you want control over the ports, you can use the --plaintext-ports and --kafka-rest-port flags -

confluent local kafka start --plaintext-ports 64886 --kafka-rest-port 8082

Testing it out

Now that the Kafka server is running, you can test it out by creating a topic and sending messages to it.

Open a new command prompt and run -

confluent local kafka topic create alphabet

This will create a topic called alphabet.

Then run -

confluent local kafka topic produce 

Open another command prompt and run -

confluent local kafka topic consume alphabet

In the producer command prompt, type some messages and press enter. You should see them appear in the consumer command prompt.

The producer and consumer
The producer and consumer

Press Ctrl+C in the producer and consumer command prompts to stop them.

Stopping Kafka

Finally, go to the Docker Desktop and stop the container.

You can also use a confluent command, but it deletes the container, and the next time you start Kafka you need to create the topic again -

confluent local kafka stop

Conclusion

I hope this post helps you get Kafka running on Windows.

comments powered by Disqus

Related