Search code examples
javaspring-bootkuberneteshealth-check

liveness probe for microservice without http


I have a microservice that is not a webservice.

It is a Spring Boot (1.5) CommandLineRunner app that does not have a need to expose an API or do anything with http.

However, I need to give it a liveness probe for Kubernetes.

Can this be accomplished without refactoring it into a webservice app?

I have this configuration added to enable Spring's info endpoint

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

I implemented this health check class

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");
    }
  }
}


Can this idea work? Or do I have to refactor it to serve web requests?

Thank you for any clues


Solution

  • you can expose actuator endpoint details including the healthcheck using JMX.

    example application.yml

    management:
      endpoints:
        jmx:
          exposure:
            include: health,info,metrics,mappings
    

    Then define the liveness probe to run a script (or java program) to call the JMX endpoint and answer the healthcheck:

    example k8s config

    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