wurstmeister / kafka-docker

Dockerfile for Apache Kafka
http://wurstmeister.github.io/kafka-docker/
Apache License 2.0
6.92k stars 2.73k forks source link

Broker id -1 not in current metadata #708

Open stevenhurwitt opened 2 years ago

stevenhurwitt commented 2 years ago

I somehow broke my zookeeper and kafka setup and I can't figure out how to reset everything. I've tried deleting the kafka logs folder, the zookeeper data folder and running docker-compose stop && docker-compose rm -vsf && docker-compose up -d. But I still get the error:

Broker id -1 not in current metadata

Here's part of my docker-compose.yaml:

zookeeper:
    image: wurstmeister/zookeeper
    container_name: zookeeper
    ports:
      - "2181:2181"
    restart: always
    volumes:
      - ./cluster_config/zookeeper/data:/opt/zookeeper-3.4.13/data
  kafka:
    image: wurstmeister/kafka
    container_name: kafka
    hostname: kafka
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: 172.16.100.34 #ip of local machine
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_LOG_DIRS: /kafka/kafka-logs
      # KAFKA_BROKER_ID: 999
      # KAFKA_CREATE_TOPICS: "test-topic:5:2"
    restart: always
    depends_on:
      - zookeeper
    # entrypoint: /tmp/entrypoint.sh
    volumes: 
      - ./cluster_config/kafka/logs:/kafka/kafka-logs
      - ./cluster_config/kafka/config:/opt/kafka/config
      # - ./entrypoint.sh:/tmp/entrypoint.sh
      - /var/run/docker.sock:/var/run/docker.sock

This is in kafka meta.properties:

#Mon May 09 21:57:54 GMT 2022
cluster.id=D8X3OwI8Svyr4DD8JNIrYw
version=0
broker.id=1001

and this is in server.properties:

broker.id=-1
zookeeper.connect=zookeeper:2181
advertised.port=9092
log.dirs=/kafka/kafka-logs
advertised.host.name=172.16.100.34
port=9092
log4j.opts=opt/kafka/config/log4j.properties

I know the broker id's are different here, but when i manually set them to be the same, or set it as an environment variable in the docker-compose.yaml I still get the same error.

This is the code I'm using:

from kafka import KafkaProducer
from kafka.admin import KafkaAdminClient, NewTopic

admin_client = KafkaAdminClient(
        bootstrap_servers="kafka:9092"
        # client_id='test'
    )

topic_list = []
topic_list.append(NewTopic(name="reddit_" + subreddit, num_partitions=1, replication_factor=1))
admin_client.create_topics(new_topics=topic_list, validate_only=False)
print("created topic")
OneCricketeer commented 2 years ago

Personally, I use Docker volumes rather than host mounts, and multiple restarts seem to work fine.

ex. https://github.com/OneCricketeer/apache-kafka-connect-docker/blob/master/docker-compose.yml#L65-L69