【问题标题】:Spring Boot custom Kubernetes readiness probeSpring Boot 自定义 Kubernetes 就绪探针
【发布时间】:2021-12-03 05:31:35
【问题描述】:

我想实现自定义逻辑来确定我的 pod 是否准备就绪,我查看了这个:https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.endpoints.kubernetes-probes.external-state,他们提到了一个示例属性: management.endpoint.health.group.readiness.include=readinessState,customCheck

问题是 - 我如何覆盖customCheck? 在我的情况下,我想使用 HTTP 探针,所以 yaml 看起来像:

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

话又说回来 - 我应该在哪里以及如何应用逻辑来确定应用程序何时准备就绪(就像上面的链接一样,我想依赖外部服务以使其准备就绪)

【问题讨论】:

    标签: spring-boot kubernetes kubernetes-health-check


    【解决方案1】:

    customCheck 是您的自定义 HealthIndicator 的键。给定 HealthIndicator 的键是不带 HealthIndicator 后缀的 bean 的名称

    您可以阅读: https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.endpoints.health.writing-custom-health-indicators

    您正在定义 readinessProbe,因此可能点击 /actuator/health/readiness 是更好的选择。

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

    【讨论】:

      猜你喜欢
      • 2019-07-22
      • 2021-05-24
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-05
      • 2018-01-30
      相关资源
      最近更新 更多