没有 http 的微服务活性探针

sla*_*tir 5 java spring-boot kubernetes health-check

我有一个不是网络服务的微服务。

它是一个 Spring Boot (1.5) CommandLineRunner 应用程序,不需要公开 API 或对 http 执行任何操作。

但是,我需要对 Kubernetes 进行活性探测。

这可以在不将其重构为网络服务应用程序的情况下完成吗?

我添加了此配置以启用 Spring 的信息端点

management:
  endpoint:
    health:
      enabled: true
    info:
      enabled: true

# https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-endpoints
info:
  app:
    name: foo-parser
    description: parses binary files from S3 and updates the database
Run Code Online (Sandbox Code Playgroud)

我实现了这个健康检查类

import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health.Builder;

@Component
public class HealthCheck extends AbstractHealthIndicator {

  Logger log = LoggerFactory.getLogger("jsonLogger");

  private final MySQLAccessor mySQLAccessor;
  private static final String FOO_TABLE = "foo";

  @Autowired
  public HealthCheck(final MySQLAccessor mySQLAccessor) {
    this.mySQLAccessor = mySQLAccessor;
  }

  @Override
  protected void doHealthCheck(Builder builder) throws Exception {
    boolean result = mySQLAccessor.healthCheck(FOO_TABLE);
    if (result) {
      log.info("HELLO! the health check is good!");
      builder.up().withDetail("test", "good");
    }
    else {
      log.info("HELLO! OH NOES the health check is ungood!");
      builder.down().withDetail("test", "bad");
    }
  }
}


Run Code Online (Sandbox Code Playgroud)

这个想法可行吗?还是我必须重构它才能满足 Web 请求?

谢谢你的任何线索

str*_*y05 3

您可以使用 JMX 公开执行器端点详细信息,包括运行状况检查。

例子application.yml

management:
  endpoints:
    jmx:
      exposure:
        include: health,info,metrics,mappings
Run Code Online (Sandbox Code Playgroud)

然后定义活性探针来运行脚本(或java程序)来调用JMX端点并回答健康检查:

k8s 配置示例

apiVersion: v1
kind: Pod
spec:
  containers:
  - name: liveness
    image: my-app 
    livenessProbe:
      exec:
        command:
        - /bin/sh
        - test_app_with_jmx.sh 
      initialDelaySeconds: 5
      periodSeconds: 5
Run Code Online (Sandbox Code Playgroud)