简体   繁体   English

使用 docker-compose 时,Healthcheck 根本不起作用(我的服务在启动之前不等待 Kafka 启动)

[英]Healthcheck not working at all when using docker-compose (My service do not wait for Kafka to be started before launching)

I have three services on my docker-compose:我的 docker-compose 上有三项服务:

version: '3.4'
  setup-topics:
    image: 'bitnami/kafka:2'
    hostname: setup-topics
    container_name: setup-topics
    command: "bash -c 'echo Waiting for Kafka to be ready... && \
                       ./opt/bitnami/kafka/bin/kafka-topics.sh --create --if-not-exists --zookeeper zookeeper:2181 --partitions 1 --replication-factor 1 --topic orders && \
                       ./opt/bitnami/kafka/bin/kafka-topics.sh --create --if-not-exists --zookeeper zookeeper:2181 --partitions 1 --replication-factor 1 --topic redis'"
    environment:
      KAFKA_BROKER_ID: ignored
      KAFKA_ZOOKEEPER_CONNECT: ignored
    depends_on:
      - kafka

  kafka:
    container_name: kafka
    hostname: kafka
    image: 'bitnami/kafka:2'
    ports:
      - '9092:9092'
      - '29092:29092'
    volumes:
      - 'kafka_data:/opt/kafka'
      - './Ping.jar:/Ping.jar'
    environment:
      - KAFKA_HEAP_OPTS=-Xms1g -Xmx1g
      - KAFKA_JVM_PERFORMANCE_OPTS=-Xms512m -Xmx512M
      - KAFKA_BROKER_ID:1
      - KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper-server:2181
      - ALLOW_PLAINTEXT_LISTENER=yes
      - KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      - KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,PLAINTEXT_HOST://:29092
      - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://kafka-server:9092,PLAINTEXT_HOST://localhost:29092
    depends_on:
      - zookeeper

    healthcheck:
      test: ["CMD", "java", "-jar", "/Ping.jar", "localhost", "9092"]
      interval: 30s
      timeout: 10s
      retries: 4

  zookeeper:
    container_name: zookeeper
    hostname: zookeeper
    image: 'bitnami/zookeeper:3'
    ports:
      - '2181:2181'
    volumes:
      - 'zookeeper_data:/bitnami'
    environment:
      - ALLOW_ANONYMOUS_LOGIN=yes
      - ZOOKEEPER_CLIENT_PORT=32181
      - ZOOKEEPER_TICK_TIME=2000

And here the Ping.java file (Found it on here on stackoverflow answer: Docker-Compose: How to healthcheck OpenJDK:8 container? ):这里是 Ping.java 文件(在 stackoverflow 上找到它的答案: Docker-Compose: How to healthcheck OpenJDK:8 container? ):

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

public class Ping {
    public static void main(String[] args) {
        if (args.length != 2) {
            System.exit(-1);
        }

        String host = args[0];
        int port = 0;

        try {
            port = Integer.parseInt(args[1]);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            System.exit(-2);
        }

        try (Socket socket = new Socket()) {
            socket.connect(new InetSocketAddress(host, port), 10 * 1000);
            System.exit(0);
        } catch (IOException e) {
            System.exit(1);
        }
    }
}

Even with depends_on on the SETUP-TOPICS service to be dependent on Kafka in order to works, but he don't wait until Kafka is started before running and install new topics.即使SETUP-TOPICS服务上的depends_on依赖于 Kafka 才能工作,但他不会等到 Kafka 启动后再运行并安装新主题。

I can avoid this steps by using:我可以使用以下方法避免此步骤:

KAFKA_AUTO_CREATE_TOPICS_ENABLE=true

But for my developement purpose, I need to make it FALSE and create them one by one.但是出于我的开发目的,我需要将其设为 FALSE 并一一创建。

In top of this, I already tested with this command in the Healthcheck without requiring third party file:最重要的是,我已经在 Healthcheck 中使用此命令进行了测试,无需第三方文件:

healthcheck:
  test: ["CMD", "bash", "-c", "unset" , "JMX_PORT" ,";" ,"/opt/bitnami/kafka/bin/kafka-topics.sh","--zookeeper","zookeeper:2181","--list"]
  interval: 30s
  timeout: 10s
  retries: 4

And finally, here is the error message I am getting for both tries:最后,这是我两次尝试都收到的错误消息:

Waiting for Kafka to be ready...
Error while executing topic command : Replication factor: 1 larger than available brokers: 0.
[2020-06-01 15:06:28,809] ERROR org.apache.kafka.common.errors.InvalidReplicationFactorException: Replication factor: 1 larger than available brokers: 0.
 (kafka.admin.TopicCommand$)

I am aware that we can do it also using SLEEP command, but its not professional and if there will be performances issue on the server and Kafka take longer to start, this one will be missed and recieve again the same error as above.我知道我们也可以使用SLEEP命令来做到这一点,但它不专业,如果服务器上出现性能问题并且 Kafka 需要更长的时间来启动,这将被错过并再次收到与上述相同的错误。

I hear also about kafkacat (Which I didn't found yet an example on how to integrate it with docker-compose for this purpose).我也听说过kafkacat (我还没有找到一个关于如何为此目的将它与 docker-compose 集成的示例)。

I want to stay basic and use limited third party tools to achieve this goal, this is way I choosen JAVA file since the image already have Java dependency installed.我想保持基本并使用有限的第三方工具来实现这个目标,这是我选择 JAVA 文件的方式,因为图像已经安装了 Java 依赖项。

Hpe you understand my view, thank you in advance for your help. Hpe您理解我的观点,提前感谢您的帮助。

Not clear why you need a JAR file.不清楚为什么需要 JAR 文件。 This should work just as well这应该也可以

test: ["CMD", "nc", "-vz", "localhost", "9092"]

The problem is simply waiting for kafka has not been waiting long enough before you run your commands.问题只是waiting for kafka在运行命令之前等待的时间不够长。 depends_on does not wait for healthcheck , AFAIK depends_on不等待healthcheck ,AFAIK

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

相关问题 Docker-Compose:如何对 OpenJDK:8 容器进行健康检查? - Docker-Compose: How to healthcheck OpenJDK:8 container? 使用docker-compose启动solr cloud时,无法使用java代码索引数据 - Cannot index data using java code when started solr cloud using docker-compose Postgres docker-compose 使用测试容器不起作用 - Postgres docker-compose using testcontainers not working 将 sprig 连接到 kafka 开始使用 docker compose 进行本地主机开发 - connecting sprig to kafka started using docker compose for localhost development docker-compose 服务名称 - docker-compose service names Docker,Dockerfile并在我开始下一个服务之前使用wait-for-it - Docker, Dockerfile and using wait-for-it before I start my next service 当按服务名称从其他docker-compose服务请求时,Spring Boot REST应用程序返回400 - Spring Boot REST app returns 400 when requested from other docker-compose service by service name Docker-compose 容器连接到 MySql 不起作用 - Docker-compose container connection to MySql not working 在 Docker-compose 中使用 Kafka 运行 Spring Boot 应用程序 - Running Spring Boot application with Kafka in Docker-compose Spring 云 Stream Kafka 与微服务和 Docker-Compose 错误 - Spring Cloud Stream Kafka with Microservices and Docker-Compose Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM