Spring Boot Actuator readiness, liveness probes on k8s

Spring Boot 를 사용하여 서버 어플리케이션을 개발하고 Kubernetes 상에서 운영할 때 Container 의 상태를 확인하고 복구가 불가능한 경우 재시작 시켜야 되는지 알 필요가 있다. 또한 Container 가 트래픽을 받아들일 준비가 되었는지 상태를 알아야한다. 이때 각각 liveness, readiness probes 를 사용한다.


아래와 같이 Kubernetes deployment 에 liveness 와 readiness probes 를 설정한다. initialDelaySeconds 는 Container 가 실행되고 90초 이후에 설정된 path 에 Get 요청을 통해서 정상상태를 확인한다. 200 ~ 399 status code 를 5초이내에 응답 받으면 성공으로 확인한다. periodSeconds 는 10초 주기로 확인한다. 연속해서 3번 비정상 응답을 받을 경우 liveness 는 실패로 돌아가고 Kubelet 은 Container 를 재시작시킨다.



이번엔 Spring Boot 어플리케이션을 보자. gradle 을 사용할 때 아래와 같이 의존성을 추가한다.


그리고 application.yaml 에 아래와 같이 설정한다. 유의해야할 점은 exposure.include 에 * 를 쓰게되면 불필요한 정보가 모두 노출되어 보안에 취약해진다. 예를 들어, heapdump 또는 shutdown 같은 기능을 노출하게 되면 외부에서 공격점이 될 수 있다.



/actuator path 요청시 아래와 같이 응답을 받는다.  exposure 에 health, info 만 설정해서 두가지만 확인할 수 있다.

/actuator/health 를 확인해보자. endponint.health.probes.enabled=true 로 설정해서 liveness, readiness 를 지원한다.


/actuator/health/liveness 와 /actuator/health/readiness 를 확인해보자. 

spring boot application 에 mysql 과 redis 를 활용하고 있다고 가정해보자. 이 때 mysql 과 redis 의 health 상태를 readiness 에 포함시켜서 상태를 관리하고 싶다면 어떻게 할까?

우선 아래와 같이 application.yaml 을 변경해보자. endpoint.health.show-details=always 와 endpoint.health.group.readiness.include=db, redis 를 추가했다.


/actuator/health 를 다시 확인해보자. db 와 redis 를 확인할 수 있다.


Spring Actuator 는 HealthIndicator 인터페이스를 활용한다. 그리고 해당 인터페이스를 구현한 DataSourceHealthIndicatorRedisHealthIndicator 를 통해서 각각 상태를 확인한다.

endpoint.health.group.readiness.include=db, redis 을 통해서 /actuator/health/readiness 에서 각각의 healthIndicator 를 포함하여 상태를 나타낸다.

기본적 readiness, liveness health indicator 는 각각 ReadinessStateHealthIndicator LivenessStateHealthIndicator 이다. 이 들을 ApplicationAvailability 를 활용한다. ApplicationAvilability 의 상태를 변경하기 위해서는 AvailabilityChangeEvent 가 ApplicationEventPublisher 에 의해서 publish 되어야한다.




참고

https://docs.spring.io/spring-boot/docs/current/reference/html/deployment.html#deployment.cloud.kubernetes.container-lifecycle

댓글

이 블로그의 인기 게시물

About JVM Warm up

About idempotent

About Kafka Basic

About ZGC

sneak peek jitpack

About Websocket minimize data size and data transfer cost on cloud

About G1 GC

대학생 코딩 과제 대행 java, python, oracle 네 번째