无法从另一个Docker容器连接到kafka docker容器

时间:2019-10-01 16:17:58

标签: docker apache-kafka apache-zookeeper

我正在尝试从另一个Docker容器连接到kafka docker容器。但是没有连接。

有用于kafka消息传递的容器列表 enter image description here

网络 kafka-docker_default 有两个容器 kafka-docker_zookeeper_1 kafka-docker_kafka0_1 enter image description here

为了运行kafka和zookeeper,我使用了docker-compose文件:

version: '2'
services:
  zookeeper:
    image: "confluentinc/cp-zookeeper:latest"
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000

# This has three listeners you can experiment with. 
# BOB for internal traffic on the Docker network
# FRED for traffic from the Docker-host machine (`localhost`)
# ALICE for traffic from outside, reaching the Docker host on the DNS name `never-gonna-give-you-up`
# Use 
  kafka0:
    image: "confluentinc/cp-kafka"
    ports:
      - '9092:9092'
      - '29094:29094'
    depends_on:
      - zookeeper
    environment:
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_LISTENERS: LISTENER_BOB://kafka0:29092,LISTENER_FRED://kafka0:9092,LISTENER_ALICE://kafka0:29094
      KAFKA_ADVERTISED_LISTENERS: LISTENER_BOB://kafka0:29092,LISTENER_FRED://localhost:9092,LISTENER_ALICE://never-gonna-give-you-up:29094
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: LISTENER_BOB:PLAINTEXT,LISTENER_FRED:PLAINTEXT,LISTENER_ALICE:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: LISTENER_BOB
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1

容器 dockercompose17138956372294708100_kafkatest.producer_1 dockercompose17138956372294708100_kafkatest.consumer_1 用于生成和使用消息。 当容器 dockercompose17138956372294708100_kafkatest.producer_1 尝试使用bootstrap.servers参数(localhost:9092或never-gonna-give-you-up:29094)连接到kafka群集时,它显示错误

%7|1569945335.015|BROKERFAIL|rdkafka#producer-1| [thrd:never-gonna-give-you-up:29094/bootstrap]: never-gonna-give-you-up:29094/bootstrap: failed: err: Local: Host resolution failure: (errno: Bad address)
%3|1569945335.015|FAIL|rdkafka#producer-1| [thrd:never-gonna-give-you-up:29094/bootstrap]: never-gonna-give-you-up:29094/bootstrap: Failed to resolve 'never-gonna-give-you-up:29094': Name or service not known (after 1656ms in state CONNECT)
%3|1569945335.015|ERROR|rdkafka#producer-1| [thrd:never-gonna-give-you-up:29094/bootstrap]: never-gonna-give-you-up:29094/bootstrap: Failed to resolve 'never-gonna-give-you-up:29094': Name or service not known (after 1656ms in state CONNECT)
%7|1569945335.015|STATE|rdkafka#producer-1| [thrd:never-gonna-give-you-up:29094/bootstrap]: never-gonna-give-you-up:29094/bootstrap: Broker changed state CONNECT -> DOWN
%3|1569945335.015|ERROR|rdkafka#producer-1| [thrd:never-gonna-give-you-up:29094/bootstrap]: 1/1 brokers are down

我该如何解决?

2 个答案:

答案 0 :(得分:1)

请尝试以下配置:

<rewrite>
    <rules>
        <rule name="restrict admin access by IP address" stopProcessing="true">
            <match url="(.*)" />
            <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_URI}" pattern="^/admin/login(.*)" />
                <!-- localhost -->
                <add input="{HTTP_True_Client_IP}" pattern="^127\.0\.0\.1$" negate="true"/>
                <!-- my officec -->
                <add input="{HTTP_True_Client_IP}" pattern="^{your ip address here}$" negate="true"/>
            </conditions>
            <action type="CustomResponse" statusCode="404" statusReason="Not Found" statusDescription="The resource you are looking for has been removed, had its name changed, or is temporarily unavailable." />
        </rule>
    </rules>
</rewrite>

通过这种配置,如果从主机连接到代理的容器,则将使用IP version: '3' services: zookeeper: image: confluentinc/cp-zookeeper:latest ports: - "32181:32181" environment: ZOOKEEPER_CLIENT_PORT: 32181 ZOOKEEPER_TICK_TIME: 2000 kafka0: image: confluentinc/cp-kafka:latest ports: - "9090:9090" depends_on: - zookeeper environment: KAFKA_ZOOKEEPER_CONNECT: zookeeper:32181 KAFKA_ADVERTISED_LISTENERS: LISTENER_INTERNAL://kafka0:29090,LISTENER_EXTERNAL://localhost:9090 KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: LISTENER_INTERNAL:PLAINTEXT,LISTENER_EXTERNAL:PLAINTEXT KAFKA_INTER_BROKER_LISTENER_NAME: LISTENER_INTERNAL KAFKA_AUTO_CREATE_TOPICS_ENABLE: "false" 。如果从具有相同网络的其他容器连接,则使用localhost:9090

答案 1 :(得分:1)

  

dockercompose17138956372294708100_kafkatest.producer_1尝试使用bootstrap.servers参数(localhost:9092never-gonna-give-you-up:29094)连接到kafka群集

从另一个Docker容器中,这两个都不正确。

  1. never-gonna-give-you-up在Docker配置中作为主机名/ DNS记录不存在任何地方。根据错误,您的外部DNS服务器也不知道

  2. localhost是指Kafka客户端容器,而不是您的主机或代理容器。

根据您的撰写配置,您需要连接到 kafka0 主机/服务,该主机/服务在端口 29092 上发布,用于bootstrap.servers

# BOB for internal traffic on the Docker network
KAFKA_ADVERTISED_LISTENERS: LISTENER_BOB://kafka0:29092

您还应该删除LISTENER_ALICE,因为它不会提供任何有用的连接详细信息,除非您主机的外部主机名实际上是never-gonna-give-you-up

并且您希望将生产者和消费者作为组成文件的一部分