You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 2在Kubernetes中优雅关闭环境表现不一致问题排查

Spring Boot 2优雅关闭在Kubernetes环境不一致问题排查

为解决Kubernetes Pod缩容时出现的5xx错误,我在Spring Boot 2应用中配置了优雅关闭,具体配置及现象如下:

应用配置(application.yaml)

management:
  endpoints:
    jmx: 
      exposure:
        include: "*"
    web:
      exposure:
        include: "*"
  endpoint:
    shutdown:
      enabled: true

server.shutdown: graceful

Kubernetes Deployment配置

lifecycle:
            preStop:
              exec:
                command: ["/bin/sh","-c","sleep 50;","rm -rf /mnt/bixby/log/$HOSTNAME"]
terminationGracePeriod: 120

livenessProbe:
    httpGet:
      port: 8080
      path: /actuator/health
    initialDelaySeconds: 45
    periodSeconds: 5
readinessProbe:
    httpGet:
      port: 8080
      path: /actuator/health
    initialDelaySeconds: 45
    periodSeconds: 5

环境差异现象

配置在dev1环境正常工作,但dev2环境出现异常:

  • 删除Pod后,dev1中调用/actuator/health返回:
    {"status":"DOWN","groups":["liveness","readiness"]}
    
  • dev2中等待50秒(preStopHook的睡眠时间)后调用/actuator/health返回curl: (52) Empty reply from server,但自定义健康检查端点在此期间能正常响应;再次调用则返回:
    *   Trying ::1...
    * TCP_NODELAY set
    * Connected to localhost (::1) port 8080 (#0)
    > GET /actuator/health HTTP/1.1
    > Host: localhost:8080
    > User-Agent: curl/7.64.0
    > Accept: */*
    > 
    < HTTP/1.1 503 Service Unavailable
    < Connection: keep-alive
    < Content-Length: 0
    
  • 额外问题:dev1环境中Pod在50秒后退出,与设置的120秒terminationGracePeriod不符。

已添加的代码

  1. @PreDestroy方法:
@PreDestroy
public void tearDown() {
  logger.log("Application shutting down");
}
  1. Swagger与Actuator兼容配置:
@Bean // This bean is required when you want swagger and actuator to work together
public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
  return new BeanPostProcessor() {
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) 
        throws BeansException {
      if (bean instanceof WebMvcRequestHandlerProvider 
          || bean instanceof RequestHandlerProvider) {
        customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
      }
      return bean;
    }

    private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(
        List<T> mappings) {
      List<T> copy = mappings.stream().filter(mapping -> mapping.getPatternParser() == null)
          .collect(Collectors.toList());
      mappings.clear();
      mappings.addAll(copy);
    }

    @SuppressWarnings("unchecked")
    private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
      try {
        Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
        field.setAccessible(true);
        return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
      } catch (IllegalArgumentException | IllegalAccessException e) {
        throw new IllegalStateException(e);
      }
    }
  };
}

两个环境的配置、Spring Boot应用镜像、Kubernetes版本均一致,求排查该环境不一致问题的原因。

内容的提问来源于stack exchange,提问作者Shaenfre

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 20:22:12