Kafka "Broker may not be available" / NoBrokersAvailable in CI
The client reached (or tried to reach) the bootstrap server but could not get broker metadata before the timeout, so it reports "Broker may not be available" or raises NoBrokersAvailable. The usual cause is connecting before the broker is ready or pointing at the wrong bootstrap address.
What this error means
A producer or consumer fails with "kafka.errors.NoBrokersAvailable: NoBrokersAvailable" or repeated "Broker may not be available" warnings, then a metadata timeout.
kafka.errors.NoBrokersAvailable: NoBrokersAvailableCommon causes
The client connects before the broker is ready
Metadata fetch fails while the broker is still starting or forming its KRaft quorum, so the client sees no available brokers.
The bootstrap server address is wrong
A bootstrap.servers that does not match the mapped port (or the advertised listener) leaves the client with no reachable broker.
How to fix it
Wait for broker readiness before connecting
Loop on an admin call until the broker answers, then run the client.
until kafka-broker-api-versions.sh --bootstrap-server localhost:9092 >/dev/null 2>&1; do
echo "waiting for kafka broker"; sleep 3
donePoint bootstrap.servers at the mapped address
Use the published host:port that matches the broker advertised listener.
env:
KAFKA_BOOTSTRAP_SERVERS: localhost:9092How to prevent it
- Wait for the broker to answer an admin call before producing or consuming.
- Keep
bootstrap.serversaligned with the advertised listener. - Allow enough metadata timeout for a slow first broker start.