Installing Kafka on Mac OSX

facebooktwittergoogle_plusredditpinterestlinkedinmail

Apache Kafka is a highly-scalable publish-subscribe messaging system that can serve as the data backbone in distributed applications. With Kafka’s Producer-Consumer model it becomes easy to implement multiple data consumers that do live monitoring as well persistent data storage for later analysis.

STEP 1: Installation, the best way to install the latest version of the Kafka server on OS X and to keep it up to date is via Homebrew.

1
$ brew install kafka

this will also install all dependencies, like zookeeper which is required to run the server.

STEP 2: we need to start Zookeeper before we can start Kafka.

1
$ zkserver start

start_zookeeperSTEP 3: Once Zookeeper is up start the Kafka server.

1
$ kafka-server-start.sh /usr/local/etc/kafka/server.properties

kafka_start

STEP 4: Start a consumer, Kafka also has a command line consumer that will dump out messages to standard out.

1
2
$ kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
Sending a message in Kafka

STEP 5: Send some messages, Kafka comes with a command line client producer that will take input from console and send it out as messages to the Kafka cluster. By default each line will be sent as a separate message. The topic test is created automatically when messages are sent to it. Omitting logging you should see something like this:

1
2
$ kafka-console-producer.sh --broker-list localhost:9092 --topic test
Sending a message in Kafka

messages

Run each of the above commands in different terminal, then you should now be able to type messages into the producer terminal and see them appear in the consumer terminal. Both of these command line tools have additional options.

STEP 6: Setting up a multi-broker cluster

So far we have been running against a single broker, but that’s no fun. For Kafka, a single broker is just a cluster of size one, so nothing much changes other than starting a few more broker instances. But just to get feel for it, let’s expand our cluster to three nodes (still all on our local machine).

First we make a config file for each of the brokers:

1
2
cp config/server.properties config/server-1.properties
cp config/server.properties config/server-2.properties

Now edit these new files and set the following properties:

1
2
3
4
5
6
7
8
9
config/server-1.properties:
    broker.id=1
    port=9093
    log.dir=/tmp/kafka-logs-1
 
config/server-2.properties:
    broker.id=2
    port=9094
    log.dir=/tmp/kafka-logs-2

The broker.id property is the unique and permanent name of each node in the cluster. We have to override the port and log directory only because we are running these all on the same machine and we want to keep the brokers from trying to all register on the same port or overwrite each others data.

We already have Zookeeper and our single node started, so we just need to start the two new nodes. However, this time we have to override the JMX port used by java too to avoid clashes with the running node:

1
2
3
4
JMX_PORT=9997 bin/kafka-server-start.sh config/server-1.properties
...
JMX_PORT=9998 bin/kafka-server-start.sh config/server-2.properties
...

Now create a new topic with a replication factor of three:

1
bin/kafka-create-topic.sh --zookeeper localhost:2181 https://www.acheterviagrafr24.com/citrate-sildenafil-naturel-vidal/ --replica 3 --partition 1 --topic my-replicated-topic

Okay but now that we have a cluster how can we know which broker is doing what? To see that run the “list topics” command:

1
2
3
bin/kafka-list-topic.sh --zookeeper localhost:2181
topic: my-replicated-topic  partition: 0  leader: 1  replicas: 1,2,0  isr: 1,2,0
topic: test                 partition: 0  leader: 0  replicas: 0      isr: 0

Here is an explanation of output:

  • “leader” is the node responsible for all reads and writes for the given partition. Each node would be the leader for a randomly selected portion of the partitions.
  • “replicas” is the list of nodes that are supposed to server the log for this partition regardless of whether they are currently alive.
  • “isr” is the set of “in-sync” replicas. This is the subset of the replicas list that is currently alive and caught-up to the leader.

Note that both topics we created have only a single partition (partition 0). The original topic has no replicas and so it is only present on the leader (node 0), the replicated topic is present on all three nodes with node 1 currently acting as leader and all replicas in sync.

As before let’s publish a few messages message:

1
2
3
4
5
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic my-replicated-topic
...
my test message 1
my test message 2
^C

Now consume this message:

1
2
3
4
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic my-replicated-topic
...
my test message 1
my test message 2

Now let’s test out fault-tolerance. Kill the broker acting as leader for this topic’s only partition:

1
pkill -9 -f server-1.properties

Leadership should switch to one of the slaves:

1
2
3
4
bin/kafka-list-topic.sh --zookeeper localhost:2181
...
topic: my-replicated-topic  partition: 0    leader: 2   replicas: 1,2,0 isr: 2
topic: test partition: 0    leader: 0   replicas: 0 isr: 0

And the messages should still be available for consumption even though the leader that took the writes originally is down:

1
2
3
4
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic my-replicated-topic
...
my test message 1
my test message 2
facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *