You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Airflow连接Kafka Broker出现DNS解析失败问题求助

解决Airflow向Kafka Broker发送数据时的DNS解析失败问题

问题背景

尝试从Airflow向Kafka Broker发送数据时,出现DNS查找失败错误,无法解析broker:29092。

Broker配置(docker-compose.yml)

broker:
    image: confluentinc/cp-server:7.4.0
    hostname: broker
    container_name: broker
    depends_on:
      zookeeper:
        condition: service_healthy
    ports:
      - "9092:9092"
      - "9101:9101"
      - "29092:29092"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:29092,PLAINTEXT_HOST://localhost:9092
      KAFKA_METRIC_REPORTERS: io.confluent.metrics.reporter.ConfluentMetricsReporter
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
      KAFKA_CONFLUENT_LICENSE_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_CONFLUENT_BALANCER_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
      KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
      KAFKA_JMX_PORT: 9101
      KAFKA_JMX_HOSTNAME: localhost
      KAFKA_CONFLUENT_SCHEMA_REGISTRY_URL: http://schema-registry:8081
      CONFLUENT_METRICS_REPORTER_BOOTSTRAP_SERVERS: broker:29092
      CONFLUENT_METRICS_REPORTER_TOPIC_REPLICAS: 1
      CONFLUENT_METRICS_ENABLE: 'false'
      CONFLUENT_SUPPORT_CUSTOMER_ID: 'anonymous'
    networks:
      - confluent
    healthcheck:
      test: [ "CMD", "bash", "-c", 'nc -z localhost 9092' ]
      interval: 10s
      timeout: 5s
      retries: 5

Airflow代码片段(kafka_stream.py)

def stream_data():
    from kafka import KafkaProducer
    res = get_data()
    res = format_data(res)
    print("Function Started")
    print(res)

    try:
        producer = KafkaProducer(bootstrap_servers=['broker:29092'],api_version = (2, 5, 0), max_block_ms=500000)
        producer.send('user_created', res)
    except Exception as e:
        print("Failed to connect to Kafka broker : ", e)

with DAG('user_automation',
         default_args=default_args,
         schedule_interval='@daily',
         ) as dag:
    streaming_task = PythonOperator(
        task_id = 'stream_data_from_api',
        python_callable=stream_data
    )

报错信息

[2024-07-28, 16:04:19 UTC] {conn.py:1276} WARNING - DNS lookup failed for broker:29092, exception was [Errno -3] Temporary failure in name resolution. Is your advertised.listeners (called advertised.host.name before Kafka 9) correct and resolvable?
[2024-07-28, 16:04:19 UTC] {conn.py:297} ERROR - DNS lookup failed for broker:29092 (0)

问题原因

broker:29092是Kafka容器在Docker内部网络(confluent网络)的访问地址,只有处于同一Docker网络的服务才能解析broker这个主机名。如果Airflow运行在宿主机,或者不在confluent网络的容器中,就会出现DNS解析失败。

解决方案

方案1:让Airflow加入Kafka所在的Docker网络

如果Airflow是通过docker-compose部署的,修改Airflow的docker-compose配置:

  1. 在Airflow服务的配置中加入网络声明:
    networks:
      - confluent
    
  2. 若confluent网络是由Kafka的docker-compose创建的外部网络,需在Airflow的docker-compose中额外声明:
    networks:
      confluent:
        external: true
    

方案2:修改Airflow代码中的bootstrap地址为宿主机暴露端口

Kafka配置已映射PLAINTEXT_HOST://localhost:9092到宿主机,直接替换代码中的bootstrap_servers:

producer = KafkaProducer(bootstrap_servers=['localhost:9092'], api_version=(2, 5, 0), max_block_ms=500000)

该方案适用于Airflow运行在宿主机,或能访问宿主机localhost的容器环境。

方案3:Hosts文件添加映射(仅测试用)

本地开发时,可在宿主机hosts文件中添加127.0.0.1 broker,强制解析broker到本地,但不推荐用于生产环境。

内容的提问来源于stack exchange,提问作者Dhainik Suthar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 05:42:13