
Apache Kafka is a high-throughput, distributed event-streaming platform used for building real-time data pipelines and streaming applications. It provides persistent, fault-tolerant message storage and supports large-scale publish-subscribe workloads with low latency. Kafka is widely used for log aggregation, metrics collection, event processing, microservices communication, and real-time analytics.
This article explains how to deploy Apache Kafka using Docker Compose, configure persistent storage, define environment variables, and verify that the Kafka broker is reachable.
Before you begin, you need to:
sudo privileges.In this section, you create a project directory for Kafka and define environment variables in a .env file that Docker Compose loads automatically.
Create the project directory.
Change the data directory permission.
This permission change is required because the Apache Kafka container runs using an internal application user appuser with UID 1000, and it must have write access to the kafka-data directory for logs and persisted messages.
Navigate into the project directory.
Generate a UUID for the cluster identifier.
Copy the generated value for use in the .env file.
Create a .env file to store the environment variables.
Add the following variables:
Replace:
KAFKA_NODE_ID with a unique broker ID (keep 1 for single-node).KAFKA_CLUSTER_ID with the unique cluster identifier you generated in the previous step.KAFKA_PORT with the port Kafka should listen on.KAFKA_ADVERTISED_HOST with the hostname or IP clients should use to connect.If your applications run on the same server as Kafka, keep KAFKA_ADVERTISED_HOST=localhost. If your applications run on a different server, replace localhost with your public server IP address so external clients can connect.
Save and close the file.
In this section, you create the Docker Compose manifest that deploys Apache Kafka in KRaft mode, enables persistent storage, and exposes the broker for client connections.
Create the Docker Compose manifest.
Add the following content:
Save and close the file.
In the manifest:
9092) on the host.KAFKA_NODE_ID: Unique broker ID.KAFKA_PROCESS_ROLES: Runs both broker and controller in one container.KAFKA_LISTENERS: Defines internal listener endpoints.KAFKA_ADVERTISED_LISTENERS: Defines the external address clients use to connect.KAFKA_CONTROLLER_QUORUM_VOTERS: Configures the internal controller quorum.KAFKA_LOG_DIRS: Path where Kafka stores log and topic data.KAFKA_CLUSTER_ID: Initializes the KRaft cluster.1 for single-node operation../kafka-data persists Kafka topic and log data across restarts.Start the Kafka service.
Verify that Kafka service is running.
Ensure that the output shows the Kafka container is healthy and running.
View logs if needed.
For more information on managing Docker Compose services, see the How To Use Docker Compose article.
In this section, you verify that your Apache Kafka broker is reachable and accepting client connections by using the Kafka CLI tools.
Update the package index and install the default Java runtime.
Download the Kafka CLI tools archive. Refer to the official Apache Kafka downloads page for newer versions.
Extract the archive to /opt.
Add the Kafka binaries from /opt to your PATH.
Apply the updated PATH.
Create a test topic.
Start the producer.
This command opens an interactive console where you type messages and press ENTER after each one to publish the messages to the topic.
Open a new terminal and start a consumer to read messages from the beginning.
This command starts the consumer and displays all messages sent to the specified topic.
Verify the topic configuration.
This command displays detailed information about the topic configuration.
You successfully deployed Apache Kafka using Docker Compose in single-node KRaft mode with persistent storage enabled. You configured all required environment variables, prepared the data directory with correct permissions, and exposed the broker for client access. After installing the Kafka CLI tools, you verified end-to-end message flow by producing and consuming test messages.
0 Comments
Be the first to comment and share your perspective with the community.