Kafka
Docker-compose
DevOps
Containerization
System Setup

Kafka setup with docker-compose

Master System Design with Codemia

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

Apache Kafka is a popular open-source stream-processing software platform developed by LinkedIn and donated to the Apache Software Foundation. It is used for building real-time data pipelines and streaming apps. It is horizontally scalable, fault-tolerant, and incredibly fast. Given this capability, deploying Kafka using Docker and managing it with Docker Compose is a practical approach, especially for development environments. This article outlines how to set up Kafka with Docker Compose including all the basic components required for a functional Kafka environment.

Understanding Kafka and Docker

Apache Kafka is a distributed event store and stream-processing system. It allows applications to publish and subscribe to streams of records (similar to a message queue), store streams of records in a fault-tolerant way, and process them as they occur.

Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers. The use of Linux containers to deploy applications is called containerization.

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services, networks, and volumes. After defining the configuration, you can create and start all the services with a single command.

Setting up Kafka with Docker Compose

To run Kafka within Docker, you'll need the following components:

  1. ZooKeeper: A centralized service for maintaining configuration information, naming, and providing distributed synchronization.
  2. Kafka Broker: Handles storage, reading, and writing of messages in the system.

Below is a simple docker-compose.yml example that sets up a single-node Kafka cluster along with ZooKeeper.

yaml
1version: '2'
2
3services:
4  zookeeper:
5    image: wurstmeister/zookeeper:3.4.6
6    ports:
7      - "2181:2181"
8
9  kafka:
10    image: wurstmeister/kafka:2.12-2.1.1
11    ports:
12      - "9092:9092"
13    environment:
14      KAFKA_CREATE_TOPICS: "Topic1:1:1"
15      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
16      KAFKA_ADVERTISED_HOST_NAME: localhost
17    volumes:
18      - /var/run/docker.sock:/var/run/docker.sock
19    depends_on:
20      - zookeeper

Explanation of the docker-compose.yml Components

  • ZooKeeper Service: The zookeeper service uses an image that includes ZooKeeper installed and set up. Ports 2181 are exposed for ZooKeeper communication.
  • Kafka Service: This service uses an image configured for Kafka. It sets up environment variables such as KAFKA_CREATE_TOPICS to create a Kafka topic named Topic1 with one partition and one replica, KAFKA_ZOOKEEPER_CONNECT to establish a connection with ZooKeeper, and KAFKA_ADVERTISED_HOST_NAME to define how the broker is advertised to other brokers and clients.
  • Volumes: The Kafka container mounts the Docker socket to enable Docker from within itself. This is typically used for Kafka to dynamically manage brokers.
  • Ports: Kafka's default port 9092 is exposed for connection from external applications.

Key Command Summary

Command (From project directory)Description
docker-compose up -dStart all services defined in docker-compose.yml in detached mode.
docker-compose downStop and remove all running containers and networks created by docker-compose up.
docker-compose logsView output from containers.

Additional Tools and Integrations

  • Kafka Manager: A tool for managing Apache Kafka's aspects like topics, brokers, and consumer groups.
  • Kafka Connect: A tool for scalably and reliably streaming data between Apache Kafka and other data systems.
  • Kafka Streams: A client library for building applications and microservices, where the input and output data are stored in Kafka clusters.

Conclusion

Setting up Kafka using Docker and Docker Compose simplifies the process of deployment and is beneficial for development environments. It ensures consistency between environments and eases the process of scaling Kafka. By using Docker Compose, managing multi-container Kafka applications becomes far more manageable, replicable, and error-free. The above guide provides a straightforward method to start with a Kafka setup that can be extended and customized for different needs or scaled to handle more significant operations.


Course illustration
Course illustration

All Rights Reserved.