Home Explore Blog CI



docker

4th chunk of `content/guides/kafka.md`
e5e05fdad8ac8edade0b7746a049a7d38a505802f76878c10000000100000419


In order to set this up, the `compose.yaml` for Kafka needs some additional configuration. Once you start overriding some of the defaults, you also need to specify a few other options in order for KRaft mode to work.

```yaml
services:
  kafka:
    image: apache/kafka-native
    ports:
      - "9092:9092"
    environment:
      # Configure listeners for both docker and host communication
      KAFKA_LISTENERS: CONTROLLER://localhost:9091,HOST://0.0.0.0:9092,DOCKER://0.0.0.0:9093
      KAFKA_ADVERTISED_LISTENERS: HOST://localhost:9092,DOCKER://kafka:9093
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,DOCKER:PLAINTEXT,HOST:PLAINTEXT

      # Settings required for KRaft mode
      KAFKA_NODE_ID: 1
      KAFKA_PROCESS_ROLES: broker,controller
      KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
      KAFKA_CONTROLLER_QUORUM_VOTERS: 1@localhost:9091

      # Listener to use for broker-to-broker communication
      KAFKA_INTER_BROKER_LISTENER_NAME: DOCKER

Title: Configuring Kafka in compose.yaml for Listeners and KRaft Mode
Summary
This section provides the configuration for the Kafka service in the `compose.yaml` file. It details how to set up listeners for both Docker and host communication using environment variables such as `KAFKA_LISTENERS` and `KAFKA_ADVERTISED_LISTENERS`. It also outlines the necessary settings for KRaft mode, including `KAFKA_NODE_ID`, `KAFKA_PROCESS_ROLES`, and `KAFKA_CONTROLLER_LISTENER_NAMES`. Additionally, it specifies the listener for broker-to-broker communication with the `KAFKA_INTER_BROKER_LISTENER_NAME` variable.