Trying to implement graceful shutdown in spring boot 2 application running in kubernetes

I am trying to implement graceful shutdown in our spring boot 2 application to remove 5xx errors when scale down of pods occurs

I have set

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

server.shutdown: graceful

inside my application.yaml file and I am using /actuator/health endpoint as my liveness and readiness probe inside my deployment.yaml file.

I have set a sleep period of 50 seconds inside preStopHook and my terminationGracePeriod is 120 seconds

My deployment.yaml file

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

The weird thing is this thing is working in one environment (let’s call it dev1) but not in other (dev2).

When I delete a pod and when I send a curl request to /actuator/health endpoint it returns {"status":"DOWN","groups":["liveness","readiness"]} in dev1 but it doesn’t returns any response in dev2 env. I get the following output curl: (52) Empty reply from server after 50 seconds which is also my prestopHook sleep time. And weirdly our own custom health check endpoint which we used earlier returns correct response during this period

After that if i do a curl request again i get following output

*   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

Also strangely in dev1 env where graceful shutdown seems to work properly the pod exits after 50 seconds which is weird since terminationGracePeriod is 120 seconds

I have added the following predestroy code in my spring boot application

@PreDestroy
  public void tearDown() {
    logger.log("Application shutting down");
  }

All the configuration, docker image of spring boot application, kubernetes version in both env are same.

I have also added this swagger configuration for actuator to work together with swagger

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

A few things: Springboot provides liveness and readiness probes, so you don’t have to use /actuator/health

In your application.yaml, do:

management:
  health:
    livenessState:
      enabled: true
    readinessState:
      enabled: true

Then in your deployment yaml:

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

Also, the pod exiting after 50 seconds is correct. If graceful shutdown only takes 50 seconds, there is no reason for the pod to wait beyond that. If it is always waiting 120 seconds, then graceful shutdown is not happening properly. You should see “Graceful shutdown completed” at the end of the log so verify it like that.

As for the graceful shutdown working in 1 env but not the other, that is indeed strange. Is the deployment file used the exact same one? How are you starting the application? If you want to really verify that the shutdown is working, you best option is to look at the application log.

8

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật