Spring Boot custom Kubernetes readiness probe

10/14/2021

I want to implement custom logic to determine readiness for my pod, and I went over this: https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.endpoints.kubernetes-probes.external-state and they mention an example property: management.endpoint.health.group.readiness.include=readinessState,customCheck

Question is - how do I override customCheck? In my case I want to use HTTP probes, so the yaml looks like:

readinessProbe:
  initialDelaySeconds: 10
  periodSeconds: 10
  httpGet:
    path: /actuator/health
    port: 12345

So then again - where and how should I apply logic that would determine when the app is ready (just like the link above, i'd like to rely on an external service in order for it to be ready)

-- Hummus
kubernetes
kubernetes-health-check
spring-boot

1 Answer

11/20/2021

customCheck is a key for your custom HealthIndicator. The key for a given HealthIndicator is the name of the bean without the HealthIndicator suffix

You can read: https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.endpoints.health.writing-custom-health-indicators

You are defining readinessProbe, so probably hiting /actuator/health/readiness is a better choice.

public class CustomCheckHealthIndicator extends AvailabilityStateHealthIndicator {

    private final YourService yourService;

    public CustomCheckHealthIndicator(ApplicationAvailability availability, YourService yourService) {
        super(availability, ReadinessState.class, (statusMappings) -> {
            statusMappings.add(ReadinessState.ACCEPTING_TRAFFIC, Status.UP);
            statusMappings.add(ReadinessState.REFUSING_TRAFFIC, Status.OUT_OF_SERVICE);
        });
        this.yourService = yourService;
    }

    @Override
    protected AvailabilityState getState(ApplicationAvailability applicationAvailability) {
        if (yourService.isInitCompleted()) {
            return ReadinessState.ACCEPTING_TRAFFIC;
        } else {
            return ReadinessState.REFUSING_TRAFFIC;
        }
    }

}
-- KrzysztofS
Source: StackOverflow