简体   繁体   English

卡夫卡经纪人认为没有经纪人(甚至不是自己)

[英]Kafka broker thinks there are no brokers (not even itself)

Question

I am trying to run a single Kafka instance in docker. 我试图在docker中运行一个Kafka实例。 I can create topics but cannot consume from or produce to them. 我可以创建主题但不能消费或生成主题。 When I try my Kafka broker logs an error message. 当我尝试我的Kafka代理时记录一条错误消息。

kafka_1      | [2019-03-24 16:45:05,263] ERROR [KafkaApi-5] Number of alive brokers '0' does not meet the required replication factor '1' for the offsets topic (configured via 'offsets.topic.replication.factor'). This error can be ignored if the cluster is starting up and not all brokers are up yet. (kafka.server.KafkaApis)

I don't understand the Number of alive brokers '0' bit as it is a Kafka broker that is logging this so surely there must be at least one broker (itself)? 我不明白Number of alive brokers '0'位数,因为它是一个Kafka经纪人正在记录这个肯定必须至少有一个经纪人(本身)?

I am using the default server.properties file with the following changes: 我使用默认的server.properties文件,并进行了以下更改:

listeners=PLAINTEXT://0.0.0.0:9092
advertised.listeners=PLAINTEXT://192.168.0.109:9092
zookeeper.connect=zookeeper:2181

where 192.168.0.109 is my host machine IP. 其中192.168.0.109是我的主机IP。

Files

docker-compose 泊坞窗,撰写

version: '3.4'
services:
  zookeeper:
    build:
      context: ./
      dockerfile: zookeeper.dockerfile
      network: host
    image: zookeeper
    ports:
      - "2181:2181"
  kafka:
    build:
      context: ./
      dockerfile: kafka.dockerfile
      network: host
    image: kafka
    ports:
      - "9092:9092"

kafka.dockerfile kafka.dockerfile

FROM alpine:3.7

# Set the name for a non-priliaged user
ENV APPUSER=appuser

# Install dependnacies
RUN apk add bash
RUN apk add openjdk8-jre

# Add a non-privilaged user and change into it's home directory
RUN adduser -D $APPUSER
WORKDIR /home/$APPUSER

# Download kafka and set its owner to $APPUSER
RUN wget https://www-eu.apache.org/dist/kafka/2.1.0/kafka_2.11-2.1.0.tgz
RUN chown $APPUSER kafka_2.11-2.1.0.tgz

# Change to the non-privilaged user
USER $APPUSER

# Extract kafka
RUN tar -xzf kafka_2.11-2.1.0.tgz

# copy custom server.properties into image 
COPY ./server.properties ./

# Set working directory to kafka dir
WORKDIR /home/$APPUSER/kafka_2.11-2.1.0

# start kafka with customer server.properties file
CMD ["bin/kafka-server-start.sh", "../server.properties"]

zookeeper.dockerfile zookeeper.dockerfile

FROM alpine:3.7

# Set the name for a non-priliaged user
ENV APPUSER=appuser

# Install dependnacies
RUN apk add bash
RUN apk add openjdk8-jre

# Add a non-privilaged user and change into it's home directory
RUN adduser -D $APPUSER
WORKDIR /home/$APPUSER

# Download kafka and set its owner to $APPUSER
RUN wget https://www-eu.apache.org/dist/kafka/2.1.0/kafka_2.11-2.1.0.tgz
RUN chown $APPUSER kafka_2.11-2.1.0.tgz

# Change to the non-privilaged user
USER $APPUSER

# Extract kafka
RUN tar -xzf kafka_2.11-2.1.0.tgz

# Set working directory to kafka dir
WORKDIR /home/$APPUSER/kafka_2.11-2.1.0

# Run zookeeper
CMD ["bin/zookeeper-server-start.sh", "config/zookeeper.properties"]

This is down to the listeners configuration. 这取决于侦听器配置。

When you specify advertised.listeners=PLAINTEXT://192.168.0.109:9092 then Kafka will try to connect to 192.168.0.109 from within the Docker container . 当您指定advertised.listeners=PLAINTEXT://192.168.0.109:9092 Kafka将尝试从Docker容器中连接到192.168.0.109 But if this is a host address, and if you've not set up the appropriate Docker networking wizardry, it will fail. 但是,如果这是一个主机地址,并且如果您没有设置适当的Docker网络向导,它将失败。

When you specify advertised.listeners=PLAINTEXT://localhost:9092 then Kafka will connect to localhost within the Docker container, which of course resolves to itself . 当您指定advertised.listeners=PLAINTEXT://localhost:9092 Kafka将连接到Docker容器中的localhost ,当然这将解析为自身 The catch here is that you would be unable to use a Kafka client outside of the Docker container (either on your host machine, or another Docker container) because they would try to connect to localhostlocally to themselves on which there would not be a Kafka broker. 这里的问题是你将无法使用Docker容器之外的Kafka客户端(在主机或其他Docker容器上),因为他们会尝试连接到localhost - 本地连接到自己 ,而不会有卡夫卡经纪人。

This article explains in more detail the issue, and appropriate configuration to use. 本文将更详细地解释该问题以及要使用的适当配置。 If you want a pre-built example of Kafka in Docker then this Docker Compose shows it. 如果你想在Docker中预先构建Kafka的例子,那么这个Docker Compose会显示它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM