Overview

Appendix A. Setting up a Kafka test environment

This appendix outlines how to create a realistic, multi-node Kafka test environment on a single machine for learning and experimentation. Instead of a single-broker demo, it uses three brokers to observe behavior under partial failures and to mirror key aspects of a real cluster. Each broker also acts as a controller (dual mode), with brokers listening on ports 9092, 9093, and 9094, and controller listeners on 9192, 9193, and 9194. The environment is explicitly intended for testing and not for production use.

The setup works across major operating systems because Kafka runs on the JVM; it was validated on macOS Sequoia (Apple Silicon) and Ubuntu 24.04 LTS, with WSL recommended for Windows users. The process involves downloading the latest Kafka release, extracting it to a local directory (for example, ~/kafka), reviewing its standard subdirectories (bin, config, libs, etc.), and adding the bin folder to the PATH for convenient use of command-line tools. Three broker configuration files are created, each defining a unique broker.id, data directory, dual listeners for PLAINTEXT and CONTROLLER, process roles, a shared controller quorum (listing all three nodes), the designated controller listener name, and a simple security protocol map for PLAINTEXT traffic.

Before starting the cluster, dedicated data directories are created and formatted with a single, shared cluster ID to prevent mixing data across clusters. Brokers are then launched—initially in the foreground to catch configuration issues—where early warnings about unreachable peers disappear once all nodes are up. After verification, brokers can be restarted in daemon mode to run in the background. Finally, a broker API versions command can be used against the three bootstrap addresses to confirm each node is online and to validate the cluster is operating correctly.

Figure A.1 Our test environment consists of three Kafka brokers in dual-mode.

A.6 Stopping Kafka

To stop all Kafka brokers you can simply execute the following command:

~/kafka/bin/kafka-server-stop.sh

But this command stops all Kafka brokers. To stop a a single broker you can create a new script ~/kafka/bin/kafka-broker-stop.sh:

#!/bin/bash
BROKER_ID="$1"
 
if [ -z "$BROKER_ID" ]; then
  echo "usage ./kafka-broker-stop.sh [BROKER-ID]"
  exit 1
fi
 
PIDS=$(ps ax | grep -i 'kafka\.Kafka' | grep java | grep "kafka${BROKER_ID}.properties" | grep -v grep | awk '{print $1}')
 
if [ -z "$PIDS" ]; then
  echo "No kafka server to stop"
  exit 1
else
  kill -s TERM $PIDS
fi

Instead of stopping all brokers, it will stop only the one for which the broker ID matches. To stop broker 1, for example, you can use the following command:

$ chmod +x ~/kafka/bin/kafka-broker-stop.sh
$ ~/kafka/bin/kafka-broker-stop.sh 1

You can check with the kafka-broker-api-versions.sh script whether this worked:

$ kafka-broker-api-versions.sh \
    --bootstrap-server localhost:9092,localhost:9093,localhost:9094
Connection to node -1 (localhost/127.0.0
.1:9092) could not be established. Broker may not be available.    #A
localhost:9093 (id: 2 rack: null) -> (
# A lot of text
)
localhost:9094 (id: 3 rack: null) -> (
# A lot of text
)

You can start the broker with the command above again or stop the whole environment. We wish you happy streaming, and have fun with the rest of the book.

FAQ

Can I use this Kafka setup for production?No. This appendix describes a single-machine, three-broker test setup meant for learning and experimentation. It helps you observe multi-node behavior and partial failures, but it is not suitable for production.
What does the test environment look like (brokers and ports)?It runs three Kafka brokers on one machine in dual-mode (each is both a broker and a controller). Client listeners: 9092 (broker 1), 9093 (broker 2), 9094 (broker 3). Controller listeners: 9192 (broker 1), 9193 (broker 2), 9194 (broker 3).
Which operating systems are supported or recommended?Kafka runs on the JVM and supports all major OSes. This setup was tested on macOS 15.2 (Apple Silicon) and Ubuntu 24.04 LTS (Intel). On Windows, use Windows Subsystem for Linux (WSL).
Where do I download Kafka and which artifact should I pick?Get the latest release from https://kafka.apache.org/downloads. For example, kafka_2.13-3.9.0.tgz where 2.13 is the Scala version and 3.9.0 is the Kafka version. Prefer the latest Scala line. Extract to a working directory such as ~/kafka.
How do I make Kafka command-line tools available in my shell?Add Kafka’s bin directory to your PATH, for example: export PATH=~/kafka/bin:"$PATH". To persist this, add the line to your shell rc file (e.g., ~/.bashrc or ~/.zshrc).
What minimum configuration is needed for each broker?For each broker file (e.g., ~/kafka/config/kafka1.properties): set a unique broker.id; set a dedicated log.dirs; define listeners (e.g., PLAINTEXT://:9092 and CONTROLLER://:9192); set process.roles=broker,controller; configure the controller quorum (e.g., controller.quorum.voters=1@localhost:9192,2@localhost:9193,3@localhost:9194); set controller.listener.names=CONTROLLER; and map security protocols (e.g., listener.security.protocol.map=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT).
Why do I need to format data directories, and how?Formatting binds the data directories to a specific cluster ID, preventing accidental reuse with another cluster. Steps: create the directories; generate a cluster ID via kafka-storage.sh random-uuid; run kafka-storage.sh format -t <CLUSTER_ID> -c <broker-config> for each broker.
How do I start the brokers, and is it normal to see warnings?Start each with kafka-server-start.sh <config>. The first broker may warn that other nodes are unreachable until they start. After confirming things work, you can background them with kafka-server-start.sh -daemon <config>.
How can I verify that all brokers are online?Run kafka-broker-api-versions.sh --bootstrap-server localhost:9092,localhost:9093,localhost:9094. The output lists each reachable broker and its API versions, making it easy to see which nodes are up.
What should I check if a broker crashes on startup?Common issues: misconfigured ports or listeners, duplicate broker.id, missing or incorrect log.dirs, data directories not formatted with the same cluster ID, or typos in quorum/role settings. Fix the configuration, then try again.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$399.99
only $33.33 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Apache Kafka in Action ebook for free
choose your plan

team

monthly
annual
$49.99
$399.99
only $33.33 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Apache Kafka in Action ebook for free
choose your plan

team

monthly
annual
$49.99
$399.99
only $33.33 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Apache Kafka in Action ebook for free