Git Product home page Git Product logo

kafka-cluster's Introduction

Kafka Cluster with Docker

A tool for creating a Kafka cluster with a single Zookeeper node.
Uses Kafka version 0.10.0.

Setup

Before starting the cluster you will need to install docker (docker-engine >= 0.10.0) and docker-compose. If you already have these installed, you can skip to Getting Started.

Setting up Docker

You can install docker from here.

Once you have docker installed, you can install docker-compose here

NB: If you are not in the docker beta program and are currently using macOS or Windows, you will need to setup a docker-machine VM to create the cluster. To learn more, I highly recommend reading the documentation before moving on.

Getting Started

With docker and docker-compose installed, the simplest way to get the cluster up and running is to run the bootstrap command. The bootstrap command will launch a 2 node Kafka cluster with a single Zookeeper node and create a Kafka topic test with 2 partitions plus a replication factor of 2. You can run the command by entering the following in your shell:

./kafka-cluster.sh bootstrap

You should see a success message if the bootstrap command ran successfully. To check and see if the docker containers are running, run the following

docker ps --filter "name=kafkacluster"

There should be three containers running where two will be named kafkacluster_kafka_<id> and one named kafkacluster_zookeeper_<id>.

At this point, you can start interacting with the cluster!

Connecting to the cluster

The cluster will currently be running in a docker network (defaults to kafkacluster_default), which means the easiest way of interacting with it is to attach a docker container to the network. The easiest way is to run the shell command which will drop you into the shell of a running container.

./kafka-cluster.sh shell

From here, we can start interacting with the zookeeper and kafka nodes.

Managing Kafka Topics

The rest of the tutorial assumes you have ran the bootstrap command.

Describing the topic

If you recall from the bootstrap command, we created a topic test as the final step. You can inspect the topic using the describe option with the kafka topic script.

$KAFKA_HOME/bin/kafka-topics.sh --describe --topic test --zookeeper $ZOOKEEPER_URL

You should see something similar to following output (with different broker ids):

Topic:test      PartitionCount:2        ReplicationFactor:2     Configs:
        Topic: test     Partition: 0    Leader: 437     Replicas: 437,979       Isr: 437,979
        Topic: test     Partition: 1    Leader: 979     Replicas: 979,437       Isr: 979,437k

Awesome! Now let's start publishing and reading from the topic.

Publishing and Subscribing to the topic

Open up two seperate terminals and enter only one of the following commands in each:

In terminal 1 (producer)

./kafka-cluster.sh shell  # wait for the shell to start
$KAFKA_HOME/bin/kafka-console-producer.sh --topic test --broker-list kafka:9092

In terminal 2 (consumer)

./kafka-cluster.sh shell  # wait for the shell to start
$KAFKA_HOME/bin/kafka-console-consumer.sh --topic test --zookeeper $ZOOKEEPER_URL

Both terminals should now be waiting to send and receive to each other. To test that they work, in your terminal 1 (the producer), type hello world!. In terminal 2 (the consumer) you should see the text pop up like this:

root@kafka-shell:/# $KAFKA_HOME/bin/kafka-console-consumer.sh --topic test --zookeeper $ZOOKEEPER_URL
hello world!

Creating a topic

To create a topic, you can use the same kafka topics script that we used earlier to describe the test topic. In this example, we are going to create an example topic with replication factor of 2 (since RF <= # of Kafka nodes) and 4 partitions.

$KAFKA_HOME/bin/kafka-topics.sh --create --topic example --partitions 4 --replication-factor 2 --zookeeper $ZOOKEEPER_URL 

After creating the topic, you can describe it using the previous describe command

$KAFKA_HOME/bin/kafka-topics.sh --describe --topic example --zookeeper $ZOOKEEPER_URL

List of helpful commands

Some environment variables in the cluster:

  • KAFKA_HOME: The directory containing the Kafka library.
  • ZOOKEEPER_URL: The <host>:<port> for the Zookeeper node.

Cluster Ops

Starts up the cluster with a default single kafka and zookeeper node.

./kafka-cluster.sh up

Scale up the kafka nodes. In this example it will increase kafka nodes to 4.

./kafka-cluster.sh scale 4

Stops the cluster then removes the stopped containers (recommended).

./kafka-cluster.sh down

Stop the cluster.

./kafka-cluster.sh stop

Debugging

Displays the cluster logs.

./kafka-cluster.sh logs

Or follow them with:

./kafka-cluster.sh logs -f

kafka-cluster's People

Contributors

spiside avatar

Watchers

none-p avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.