Kafka "Connection to node -1 ... could not be established" in CI
The Kafka client (bootstrap node id -1) cannot reach a broker on 9092. Either the broker is still starting, or its advertised.listeners point at an address the client cannot use. Wait for readiness and set the advertised listener to what clients actually connect to.
What this error means
The client logs "Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available." repeatedly before timing out.
WARN [Producer clientId=producer-1] Connection to node -1 (localhost/127.0.0.1:9092)
could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)Common causes
The broker is not ready yet
Kafka brokers take time to start (and, in KRaft mode, to finish quorum). Connections in that window fail with node -1 unreachable.
advertised.listeners point at an unreachable address
If the broker advertises an internal hostname the client cannot resolve, the client connects to bootstrap once but then fails to reach the advertised address.
How to fix it
Set advertised.listeners to a reachable address
Advertise the address clients use to connect (here localhost on the mapped port).
services:
kafka:
image: bitnami/kafka:3.7
ports: ['9092:9092']
env:
KAFKA_CFG_LISTENERS: PLAINTEXT://:9092,CONTROLLER://:9093
KAFKA_CFG_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,CONTROLLER:PLAINTEXTWait for the broker before producing
Poll a broker API (list topics) until it responds, then start tests.
until kafka-topics.sh --bootstrap-server localhost:9092 --list >/dev/null 2>&1; do
echo "waiting for kafka"; sleep 3
doneHow to prevent it
- Advertise the exact host:port clients connect on.
- Wait for the broker to answer an admin call before producing.
- Keep listener and advertised-listener config consistent.