kafka docker报错: Connection to node xxxx could not be established. Broker may not be available

背景

在使用docker中搜索kafka时,排在第一位的是wurstmeister/kafka

➜  ~ docker search kafka
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
wurstmeister/kafka                 Multi-Broker Apache Kafka Image                 783                                     [OK]
spotify/kafka                      A simple docker image with both Kafka and Zo…   333                                     [OK]
sheepkiller/kafka-manager          kafka-manager                                   149                                     [OK]
ches/kafka                         Apache Kafka. Tagged versions. JMX. Cluster-…   110                                     [OK]
confluentinc/cp-kafka              Official Confluent Docker Image for Kafka (O…   101                                     
confluentinc/cp-kafka-connect      Official Confluent Docker Image for Kafka Co…   55                                      
hlebalbau/kafka-manager            Kafka Manager Docker Images Build.              24                                                                        [OK]
                               

kafka是依赖zookeeper的,需要通过docker-compose的方式启动,wurstmeister/kafka提供了一个docker-compose.yml

version: '2'
services:
  zookeeper:
    image: wurstmeister/zookeeper
    ports:
      - "2181:2181"
  kafka:
    build: .
    ports:
      - "9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: 192.168.99.100
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

这里看上去只需要修改一下KAFKA_ADVERTISED_HOST_NAME为本机的IP地址,使用docker-compose up -d启动是可以成功的

➜  ~ docker ps -a
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS                    PORTS                                        NAMES
059f9781b64f        wurstmeister/kafka   "start-kafka.sh"         5 hours ago         Up 5 seconds              0.0.0.0:32770->9092/tcp                      kafka_kafka_1_fdc376ef63fd
8b05ff5707e4        zookeeper            "/docker-entrypoint.…"   3 days ago          Up 5 seconds              2888/tcp, 0.0.0.0:2181->2181/tcp, 3888/tcp   kafka_zookeeper_1_f74c610d349b

但进入kafka容器看日志/opt/kafka/logs/server.log会有报错输出

Connection to node xxxx could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)

办法

将docker-compose.yml中的- "9092"改成- "9092:9092"重启就没有问题了。

你可能感兴趣的:(kafka)