Kafka
Docker
Advertised Host Name
Distributed Systems
Middleware

Kafka with Docker dynamic advertised_host_name

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a highly popular distributed event streaming platform capable of handling trillions of events a day. Initially conceived as a messaging queue, Kafka is based on an abstraction of a distributed commit log. When used in conjunction with Docker, a platform for developing, shipping, and running applications inside lightweight containers, Kafka offers significant flexibility, particularly in dynamic environments such as cloud-based infrastructures.

One important aspect to master when configuring Kafka with Docker is the handling of the advertised.listeners setting, which tells other brokers and clients how to connect to the current Kafka broker. Managing this setting becomes complex when dealing with dynamic environments like Docker where the IP address or hostname can change.

Understanding advertised.listeners

Kafka brokers are configured with a list of listeners (also called endpoints) that are used for intra-broker communication and to connect with clients. The advertised.listeners parameter is crucial as it defines the address of the Kafka broker that is advertised to other brokers and clients. In a Docker context, where the IP or hostname can be non-static, managing this configuration dynamically is essential.

Docker and Dynamic advertised.listeners

When deploying Kafka on Docker, especially on platforms like Kubernetes or Docker Swarm, the internal IP address of the container can change whenever the container restarts or when there is any orchestration action. Thus, hard-coding IP addresses or hostnames in your Kafka configuration isn't feasible.

To handle this, we need a method to dynamically set the advertised.listeners value. One common approach is to utilize environment variables or templating tools that configure this setting on container initialization. Here's a basic example of how you might achieve this using Docker Compose:

yaml
1version: '3'
2
3services:
4  kafka:
5    image: wurstmeister/kafka
6    ports:
7      - "9092:9092"
8    environment:
9      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
10      KAFKA_ADVERTISED_LISTENERS: INSIDE://:9092,OUTSIDE://_{HOSTNAME_COMMAND}:9094
11      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
12      KAFKA_INTER_BROKER_LISTENER_NAME: INSIDE
13      HOSTNAME_COMMAND: "route -n | awk '/UG[ \t]/{print $$2}'"
14    volumes:
15      - /var/run/docker.sock:/var/run/docker.sock

In this Docker Compose snippet, KAFKA_ADVERTISED_LISTENERS is dynamically set using a command to fetch the host IP. The HOSTNAME_COMMAND can be replaced or modified based on the specific needs or networking setup of your Docker environment.

Summary Table

ParameterDescriptionExample Value
KAFKA_ZOOKEEPER_CONNECTConnection string for Kafka to connect to Zookeeper.zookeeper:2181
KAFKA_ADVERTISED_LISTENERSThe listeners Kafka advertises to the outside world. Can dynamically pick up the host IP.INSIDE://:9092,OUTSIDE://_{HOSTNAME_COMMAND}:9094
KAFKA_LISTENER_SECURITY_PROTOCOL_MAPMaps listener names to security protocols.INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
KAFKA_INTER_BROKER_LISTENER_NAMEPreferred listener for communication between brokers in the same cluster.INSIDE

Troubleshooting Common Issues

Configuring advertised.listeners incorrectly can lead to issues where brokers may not be reachable. Always ensure that:

  • The listener’s network matches with Docker's network configurations.
  • Ports are correctly exposed and mapped in Docker settings.
  • Security settings and authentication are correctly set if you’re using SASL/SSL for Kafka.

Conclusion

Deploying Kafka within Docker containers offers a scalable and flexible messaging solution, especially when combined with dynamic configuration management for advertised.listeners. As enterprises move towards microservices and cloud-native architectures, such integrations are becoming increasingly critical. Properly configuring dynamic settings ensures robust and responsive Kafka deployments, adaptable to various environments with minimal manual intervention.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.