如何将Docker中的Kafka暴露给外界?

必须从Dockerized Spring-Boot应用程序与Kafka进行通信,我能够开始工作的唯一选择是Dockerizing Kafka .

这是我的 docker-compose-yml

version: '3.5'
services:
  zookeeper:
    image: wurstmeister/zookeeper
    ports:
      - "2181:2181"
    networks:
      - kafka-network
  kafka:
    image: wurstmeister/kafka
    ports:
      - "9092:9092"
    networks:
      - kafka-network
    environment:
      KAFKA_ADVERTISED_HOST_NAME: kafka
      KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    depends_on:
      - zookeeper
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
networks:
  kafka-network:
    name: kafka-network

这样我就可以使用url kafka:9092kafka-network 上的另一个容器连接到Kafka代理

如何从localhost和其他计算机上获取它?

UPDATE 我更新了我的docker-compose,如下所示:

version: '3.5'
services:
  zookeeper:
    image: wurstmeister/zookeeper
    ports:
      - "2181:2181"
    networks:
      - kafka-network
  kafka:
    image: wurstmeister/kafka
    ports:
      - "9092:9092"
    networks:
      - kafka-network
    environment:
      KAFKA_ADVERTISED_HOST_NAME: kafka
      KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_LISTENERS: ${KAFKA_LISTENERS:-PLAINTEXT://:9092}
      KAFKA_ADVERTISED_LISTENERS: ${KAFKA_ADVERTISED_LISTENERS:-PLAINTEXT://127.0.0.1:9092}
    depends_on:
      - zookeeper
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
networks:
  kafka-network:
    name: kafka-network

并创建了一个包含以下内容的.env文件:

KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://the.ip.of.machine:9092

我在我的PC上测试它(没有.env文件),我可以使用localhost中的kafkakat与代理进行通信:

$ docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                                                NAMES
6844a16fa14f        wurstmeister/kafka       "start-kafka.sh"         5 seconds ago       Up 3 seconds        0.0.0.0:9092->9092/tcp                               kafka-compose_kafka_1_9573f71109c7
15d62557f3bd        wurstmeister/zookeeper   "/bin/sh -c '/usr/sb…"   6 seconds ago       Up 4 seconds        22/tcp, 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp   kafka-compose_zookeeper_1_61a19213cde7

$ kafkacat -P -b localhost:9092 -t topic1
New test 
^C

$ kafkacat -C -b localhost:9092 -t topic1
New test
% Reached end of topic topic1 [0] at offset 1

$ kafkacat  -b localhost:9092 -L
Metadata for all topics (from broker -1: localhost:9092/bootstrap):
 1 brokers:
  broker 1001 at 127.0.0.1:9092
 4 topics:
 ...

但是我无法在服务器上执行相同的操作,唯一的区别是KAFKA_ADVERTISED_LISTENERS的主机IP .

我能看到的是它一直在说 leader not available

# kafkacat  -b localhost:9092 -L
Metadata for all topics (from broker -1: localhost:9092/bootstrap):
 1 brokers:
  broker 1002 at the.ip.of.machine:9092


 topic "__consumer_offsets" with 50 partitions:
    partition 0, leader -1, replicas: 1001, isrs: , Broker: Leader not available
    partition 1, leader -1, replicas: 1001, isrs: , Broker: Leader not available
    partition 2, leader -1, replicas: 1001, isrs: , Broker: Leader not available
    partition 3, leader -1, replicas: 1001, isrs: , Broker: Leader not available
    partition 4, leader -1, replicas: 1001, isrs: , Broker: Leader not available
    partition 5, leader -1, replicas: 1001, isrs: , Broker: Leader not available

我不应该为 KAFKA_ADVERTISED_LISTENERS 设置 IP of the server 吗?

回答(1)

2 years ago

您必须在 environments 中设置 LISTENERS 才能将Kafka代理公开给外部网络,如下所示:

KAFKA_LISTENERS: PLAINTEXT://:9092
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://one.prod.com:9092

这是一个例子:
https://github.com/wurstmeister/kafka-docker/blob/85821409d4d49a4edc7c5be83b68b71eceeab1bc/docker-compose-swarm.yml

您可以在这里查询更多详情:https://github.com/wurstmeister/kafka-docker/wiki/Connectivity