Kafka Tutorial
Apache Zookeeper
Guide to Kafka
Starting Zookeeper
Kafka Initialization

How to start Zookeeper and then Kafka?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you are running classic Kafka mode, ZooKeeper must be up before Kafka brokers can register and become healthy. Most local startup issues come from wrong paths, port conflicts, or stale data directories. A clean startup sequence plus quick validation commands will save a lot of time.

Confirm Your Kafka Mode Before Starting

Kafka now supports KRaft mode, which does not require ZooKeeper. This article covers the classic flow where ZooKeeper is still required, which you may see in older clusters, tutorials, or compatibility environments.

Before starting anything, confirm:

  • Java is installed.
  • KAFKA_HOME points to your extracted Kafka directory.
  • ports 2181 for ZooKeeper and 9092 for Kafka are free.
bash
1java -version
2ls "$KAFKA_HOME"
3lsof -i :2181
4lsof -i :9092

If ports are busy, either stop conflicting services or update config files to different ports.

Start ZooKeeper First

In classic Kafka distributions, ZooKeeper scripts are under Kafka bin directory.

Start ZooKeeper:

bash
cd "$KAFKA_HOME"
bin/zookeeper-server-start.sh config/zookeeper.properties

This command runs in foreground. Keep the terminal open while testing. For background mode:

bash
bin/zookeeper-server-start.sh -daemon config/zookeeper.properties

Important settings in config/zookeeper.properties:

  • dataDir, where ZooKeeper snapshots are stored.
  • clientPort, usually 2181.
  • maxClientCnxns, useful for local stress tests.

If startup fails with file permission errors, create and own dataDir properly.

Start Kafka Broker After ZooKeeper Is Healthy

In a second terminal, start Kafka broker:

bash
cd "$KAFKA_HOME"
bin/kafka-server-start.sh config/server.properties

Or as daemon:

bash
bin/kafka-server-start.sh -daemon config/server.properties

Critical broker settings to verify in config/server.properties:

  • broker.id, unique per broker.
  • listeners, where broker binds.
  • advertised.listeners, what clients should use.
  • log.dirs, where partition data is stored.
  • zookeeper.connect, which should match your ZooKeeper endpoint.

Local single-node example:

properties
1listeners=PLAINTEXT://localhost:9092
2advertised.listeners=PLAINTEXT://localhost:9092
3zookeeper.connect=localhost:2181
4log.dirs=/tmp/kafka-logs

If advertised.listeners is wrong, clients may connect to broker but fail metadata fetches.

Validate End to End With Topic and Messages

Once both processes are running, create a test topic:

bash
1bin/kafka-topics.sh --create \
2  --topic quickstart-events \
3  --bootstrap-server localhost:9092 \
4  --partitions 1 \
5  --replication-factor 1

List topics:

bash
bin/kafka-topics.sh --list --bootstrap-server localhost:9092

Produce messages:

bash
bin/kafka-console-producer.sh --topic quickstart-events --bootstrap-server localhost:9092

Type a few lines and press enter after each line.

Consume messages in another terminal:

bash
1bin/kafka-console-consumer.sh \
2  --topic quickstart-events \
3  --from-beginning \
4  --bootstrap-server localhost:9092

If producer and consumer both work, startup sequence is correct.

Stop Services Cleanly

For foreground processes, use keyboard interrupt. For daemon mode, use stop scripts:

bash
bin/kafka-server-stop.sh
bin/zookeeper-server-stop.sh

Clean shutdown helps avoid log corruption and confusing restart behavior.

Common Local Failure Patterns and Fixes

  1. ZooKeeper starts, Kafka fails with connection error.
  • Check zookeeper.connect host and port.
  • Confirm ZooKeeper process is still alive.
  1. Kafka starts but clients cannot connect.
  • Verify advertised.listeners value.
  • Check whether firewall or container networking rewrites hostnames.
  1. Startup fails after previous crash.
  • Remove stale lock files or clean local log directories in development environment.
  • Avoid deleting production data paths accidentally.
  1. Broker starts then exits quickly.
  • Read first error line in broker logs, not last cascading stack trace.

Useful checks:

bash
jps -l
netstat -an | grep 2181
netstat -an | grep 9092

Notes for Multi-Broker Development

If you run multiple local brokers, each needs distinct values for:

  • broker.id
  • listeners port
  • log.dirs

You can duplicate server.properties into separate files such as server-1.properties, server-2.properties, then start each broker with its own file.

Common Pitfalls

  • Starting Kafka before ZooKeeper in classic mode.
  • Misconfigured advertised.listeners causing client metadata failures.
  • Reusing the same log.dirs across multiple brokers.
  • Ignoring port conflicts on 2181 or 9092.
  • Assuming old ZooKeeper flow is required when running a KRaft-only setup.

Summary

  • In classic mode, start ZooKeeper first, then Kafka broker.
  • Validate with topic creation plus producer and consumer commands.
  • Check zookeeper.connect, listener settings, and data directories first when debugging.
  • Use daemon mode carefully and always stop services cleanly.
  • For new deployments, verify whether KRaft can replace ZooKeeper requirements.

Course illustration
Course illustration

All Rights Reserved.