Docker-compose for a local kafka cluster
May 17, 2022-2 min read
Let's check how to set up a local kafka cluster using Docker and Docker Compose.
Starting the containers
We will startup three kafka instances, at ports 9091, 9092 and 9093; one instance of zookeeper at port 2181; and one instance of kafdrop at port 9000.
This is the content of the docker-compose.yml
file:
version: '3'networks:kafka-cluster:name: kafka-clusterdriver: bridgeservices:zookeeper:image: bitnami/zookeeper:3.6.2hostname: zookeeperports:- '2181:2181'environment:- ALLOW_ANONYMOUS_LOGIN=yesnetworks:- kafka-clusterhealthcheck:test: nc -z localhost 2181 || exit -1interval: 10stimeout: 5sretries: 3start_period: 10skafka-1:image: bitnami/kafka:2.7.0hostname: kafka-1ports:- '9093:9093'environment:- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181- ALLOW_PLAINTEXT_LISTENER=yes- KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CLIENT:PLAINTEXT,EXTERNAL:PLAINTEXT- KAFKA_CFG_LISTENERS=CLIENT://:9092,EXTERNAL://:9093- KAFKA_CFG_ADVERTISED_LISTENERS=CLIENT://kafka-1:9092,EXTERNAL://localhost:9093- KAFKA_INTER_BROKER_LISTENER_NAME=CLIENT- KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE=truedepends_on:zookeeper:condition: service_healthynetworks:- kafka-clusterkafka-2:image: bitnami/kafka:2.7.0hostname: kafka-2ports:- '9094:9094'environment:- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181- ALLOW_PLAINTEXT_LISTENER=yes- KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CLIENT:PLAINTEXT,EXTERNAL:PLAINTEXT- KAFKA_CFG_LISTENERS=CLIENT://:9092,EXTERNAL://:9094- KAFKA_CFG_ADVERTISED_LISTENERS=CLIENT://kafka-2:9092,EXTERNAL://localhost:9094- KAFKA_INTER_BROKER_LISTENER_NAME=CLIENT- KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE=truedepends_on:zookeeper:condition: service_healthynetworks:- kafka-clusterkafka-3:image: bitnami/kafka:2.7.0hostname: kafka-3ports:- '9095:9095'environment:- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181- ALLOW_PLAINTEXT_LISTENER=yes- KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CLIENT:PLAINTEXT,EXTERNAL:PLAINTEXT- KAFKA_CFG_LISTENERS=CLIENT://:9092,EXTERNAL://:9095- KAFKA_CFG_ADVERTISED_LISTENERS=CLIENT://kafka-3:9092,EXTERNAL://localhost:9095- KAFKA_INTER_BROKER_LISTENER_NAME=CLIENT- KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE=truedepends_on:zookeeper:condition: service_healthynetworks:- kafka-clusterkafdrop:image: obsidiandynamics/kafdrop:latestcontainer_name: kafdropports:- 9000:9000environment:- KAFKA_BROKERCONNECT=kafka-1:9092,kafka-2:9092,kafka-3:9092depends_on:- kafka-1- kafka-2- kafka-3networks:- kafka-cluster
Start the services using:
docker-compose up -d
The containers should be up within a few seconds:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMEScdcddee0b664 obsidiandynamics/kafdrop:latest "/kafdrop.sh" 8 weeks ago Up 6 seconds 0.0.0.0:9000->9000/tcp kafdropc2e24673ae8e bitnami/kafka:2.7.0 "/opt/bitnami/script…" 8 weeks ago Up 7 seconds 9092/tcp, 0.0.0.0:9095->9095/tcp kafka-kafka-3-1f15abb636885 bitnami/kafka:2.7.0 "/opt/bitnami/script…" 8 weeks ago Up 7 seconds 9092/tcp, 0.0.0.0:9094->9094/tcp kafka-kafka-2-1983c7065d4b7 bitnami/kafka:2.7.0 "/opt/bitnami/script…" 8 weeks ago Up 7 seconds 9092/tcp, 0.0.0.0:9093->9093/tcp kafka-kafka-1-160d1ecb26d23 bitnami/zookeeper:3.6.2 "/opt/bitnami/script…" 8 weeks ago Up 21 seconds (healthy) 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, 8080/tcp kafka-zookeeper-1
To verify that everything is in order, point your browser to kafdrop at http://localhost:9090. You should see a list of brokers similar to this:
BrokersID Host Port Rack Controller Number of partitions (% of total)1001 kafka-2 9092 - No 18 (34%)1003 kafka-1 9092 - No 18 (34%)1002 kafka-3 9092 - Yes 17 (32%)
Taking it all down
Take down containers and delete their corresponding volumes:
docker-compose down -v