Setting up Zookeeper and Kafka

This was tested on an m1.medium instance on AWS on Ubuntu 12.04.

Installing Zookeeper

Zookeeper is needed for Kafka. To quote the Zookeeper Apache project page,

Zookeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services.

In other words, we need Zookeeper to maintain configuration for Kafka nodes in a multi-node environment. In this tutorial, we’ll only be setting up a single Kafka node instance, but Kafka relies on Zookeeper to for configuration information.

We can start off by grabbing Zookeeper:

$ wget

Extract the files:

$ tar -zxvf zookeeper-3.4.6.tar.gz

Change to the zookeeper/conf directory:

$ cd zookeeper-3.4.6/conf

In this directory, create the zoo.cfg file from the provided template configuration file named zoo_sample.cfg:

$ cp zoo_sample.cfg zoo.cfg

Open up zoo.cfg and change the dataDir variable to point to /var/lib/zookeeper. (I used the vi editor to make these changes).


Since this directory doesn’t exist yet, create it:

$ sudo mkdir /var/lib/zookeeper

Install Java on a fresh Ubuntu 12.04:

$ sudo apt-get update
$ sudo apt-get install openjdk-7-jdk

In the zookeeper-3.4.6 directory, start Zookeeper:

$ sudo bin/ start

You should see a message similar to:


You can test to see that it can be connected from Java:

$ bin/ -server


Great! Now you have Zookeeper running.

Installing Kafka

We’ll setup using kafka_2.9, since at the time of this writing, Storm-0.9.2 (which we will be using) plays nicer with this version of Kafka.

First grab Kafka, and then extract it:

$ wget
$ tar xvzf kafka_2.9.2-

Change to the kafka_2.9.2- directory:

$ cd kafka_2.9.2-

Here, change the log.dirs property in the file to /var/lib/kafka-logs:


Create that directory:

$ sudo mkdir /var/lib/kafka-logs

In the file again, point the property to the public DNS (the address that others can connect to) of your instance:

In the kafka_2.9.2- directory, you can start the Kafka server:

$ sudo bin/ config/ &

Or, start it as a daemon:

$ sudo bin/ -daemon config/

You can stop the server anytime:

$ sudo bin/

Create a “topic” named test with a single partition and one replica:

$ bin/ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

See it at the command line:

$ bin/ --list --zookeeper localhost:2181


You can also start inputting command-line message, and read the output in another terminal. First start the producer to the topic test:

$ bin/ --broker-list localhost:9092 --topic test

Type a message and press ‘enter’. For instance, the message can be “hello”.


Open another terminal, and in the kafka_2.9.2- directory, start the consumer:

$ bin/ --zookeeper localhost:2181 --topic test --from-beginning

This will start consuming all messages since the beginning of the topic, but you can set the offset. Messages survive for 24 hours by default, but can be tuned to your desire.

You should see each message given from the producer:


Talking to Kafka with Python

Instead of the command line, maybe we’d like to push messages to Kafka programmatically, say, from a Python script.

First, get ‘pip’ so that you can install Python packages.

$ sudo apt-get install python-pip

Now install the kaka-python package:

$ sudo pip install kafka-python

While leaving your consumer terminal open, run the following Python script:

You should see the message show up in the consumer:


You’ve now successfully pushed messages to Kafka using a Python script. There are many more things you can do with the kafka-python package, and you are encouraged to look through the documentation on the kafka-python package Github page.


In this tutorial, you set up Zookeeper, Kafka, and used a Python script to push messages to Kafka. If you’ve seen any errors in this tutorial, please feel free to leave comments in the section below.