Docker-Compose:如何对OpenJDK:8容器进行健康检查?

时间:2018-09-15 17:25:31

标签: java docker docker-compose jhipster health-monitoring

我正在尝试对扩展了openjdk:8的容器进行健康检查。 相应的 Dockerfile 可在https://github.com/jhipster/jhipster-registry/blob/master/Dockerfile

中找到

我具有以下目录结构:

test/
├── central-server-config
│   └── application.yml
├── docker-compose.yml
├── jhipster-registry.yml
└── Ping.jar

application.yml

#common configuration shared between all applications
configserver:
    name: Docker JHipster Registry
    status: Connected to the JHipster Registry running in Docker

jhipster:
    security:
        authentication:
            jwt:
                secret: 3ac0a39ed9a2a58ca74d9d36c5227e51225480e2

eureka:
    client:
        service-url:
            defaultZone: http://admin:${jhipster.registry.password}@jhipster-registry:8761/eureka/

docker-compose.yml

version: '2.1'
services:
    jhipster-registry:
        extends:
            file: jhipster-registry.yml
            service: jhipster-registry
        mem_limit: 512m
        ports:
            - 8761:8761
        healthcheck:
          test: ["CMD", "java", "-jar", "Ping.jar", "localhost", "8761"]
          interval: 30s
          retries: 10 

jhipster-registry.yml

version: '2.1'
services:
    jhipster-registry:
        image: jhipster/jhipster-registry:v4.0.2
        volumes:
            - ./central-server-config:/central-config
        # By default the JHipster Registry runs with the "dev" and "native"
        # Spring profiles.
        # "native" profile means the filesystem is used to store data, see
        # http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html
        environment:
            - SPRING_PROFILES_ACTIVE=dev,native
            - SECURITY_USER_PASSWORD=password
            - JHIPSTER_REGISTRY_PASSWORD=password
            # - GIT_URI=https://github.com/jhipster/jhipster-registry/
            # - GIT_SEARCH_PATHS=central-config
        ports:
            - 8761:8761

Ping.java

package ping;

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

public class Main {
    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);
        }
    }
}

我通过运行启动容器

docker-compose up -d --force-recreate

在测试目录中,我可以通过浏览器访问jhipster-registry。也可以通过上面的jar程序来访问它:

izio@1z10:~$ cd Desktop/
izio@1z10:~/Desktop$ java -jar Ping.jar localhost 8761
izio@1z10:~/Desktop$ echo $?
0
izio@1z10:~/Desktop$ 

返回0,表示它可以连接到服务。

如果有人想知道,我也尝试使用curl但没有成功(几乎确定docker映像中没有提供它来减小大小),所以我切换到这个Java片段,希望没有问题,因为它是 openjdk ...

使用此配置,jhipster注册表在大约3分钟内启动,但是无论我等待多少时间,它始终显示为health starting,最后显示为unhealthy(10 x 30''= 300之后''= 5')。

关于此健康检查有什么问题的任何想法?我提供了最小无效示例,以便任何人都可以对其进行测试。 我需要进行此检查才能启动另一个服务,该服务仅在准备好接受连接时才需要连接到注册表,而不仅仅是在容器启动后才启动,否则应用程序将失败并停止,除非我设置restart: on-failure,但我宁愿仅在其他服务启动时才启动它,而不是一直重启以希望找到注册表已准备好。

1 个答案:

答案 0 :(得分:1)

这里的问题是Jar文件在容器的filesystem上不可用。 要使其可用,我们需要通过以下方式使用卷:

jhipster-registry:
    extends:
        file: jhipster-registry.yml
        service: jhipster-registry
    mem_limit: 512m
    ports:
        - 8761:8761
    networks:
      - backend
    volumes:
      - ${PWD}/Ping.jar:/Ping.jar
    healthcheck:
      test: ["CMD", "java", "-jar", "/Ping.jar", "localhost", "8761"]
      interval: 30s
      retries: 20

并从根目录(Jar)引用/文件。