#023. 무중단 배포: Readiness 와 Liveness 설정하기
goal(목표)
(1) Spring boot에서 무중단 배포(Zero-downtime Deployment)를 한다.
(2) 빌드가 정상적으로 뜨지 않는 경우 배포하지 않는다.
(3) 배포 실패 또는 서버가 죽었을 때 슬랙으로 알림을 받는다
(4) 개발 단계에서도 클라이언트와 사이좋게 지내기..
누가 무중단 없이 배포 소리를 내었는가?..
Non-goal
Cloud 환경 설정
플로우 다이어그램
서버 내부 구현
(1) Kubernetes Probes란
Probe는 kubelet이 주기적으로 컨테이너의 상태를 체크하는 것이다.
Spring Boot 2.3 이후부터는 Kubernetes probes 기능이 통합되어서 스프링부트에서 직접 설정할 수 있게 되었다.
Spring Boot에 spring-boot-starter-actuator를 implement하면 liveness와 readiness의 endpoint를 만들어주는데, 이 endpoint를 pod definition에도 작성해서 pod의 liveness probe를 HTTP get 요청으로 받도록 한다. (관련 자세한 설정은 Liveness and Readiness Probes in Spring Boot 참고)
liveness: /actuator/health/liveness
readiness: /actuator/health/readiness
(2) Readiness와 Liveness
Readiness에는 두 가지 ENUM 상태값이 있다.
ACCEPTING_TRAFFIC: traffic을 받을 준비가 된 상태
REFUSING_TRAFFIC: 현재 application이 request를 받을 수 없음.
Liveness에도 두 가지 ENUM 상태값이 있는데, 각각 아래와 같다.
CORRECT: appication이 실행되며 그 상태가 정상적이다.
BROKEN: 실행은 되지만 치명적인 failure들이 있다.
Spring의 life cycle을 보면 각각 상태가 어디서 확인되는 지 알 수 있다.
liveness는 말그대로 container가 살아있는 지(started and alive)를 확인한다면 Readiness는 서버가 request를 처리할 수 있는 지(즉, traffic을 받을 수 있는 지)를 확인한다. 또 liveness는 실패할 경우 재시작하지만 readiness는 재시작하지 않는다. 지금은 build 하는 데 까지 시간이 오래 걸리고 무중단으로 request를 처리하고 싶은 상황이기 때문에 readiness를 사용하는 것이 더 낫겠다는 판단을 했다.
1. Registering listeners and initializers
2. Preparing the Environment
3. Preparing the ApplicationContext
4. Loading bean definitions
5. Changing the liveness state to CORRECT // <--- liveness
6. Calling the application and command-line runners
7. Changing the readiness state to ACCEPTING_TRAFFIC // <--- readiness
(3) Spring boot에서 설정하기
// deployment.yaml
readinessProbe:
httpGet:
path: /actuator/health/readiness
port: 80
// application.yaml
management:
endpoint:
health:
probes:
enabled: true
health:
readinesstate:
enabled: true
배포에 실패하거나 서버가 죽는 경우 알림을 설정할 수 있다.
@Component
class LivenessEventListener {
@EventListener
fun onEvent(event: AvailabilityChangeEvent<LivenessState>): Void {
switch (event.getState()) {
case BROKEN:
// notify others
break;
case CORRECT:
// we're back
}
}
}
(4) 실행확인
replica:2에 빌드가 깨진 상황:
서버가 정상적으로 실행되지 않않기 때문에 readiness 가 DOWN 상태이고, readiness를 확인하기 때문에 재실행되지 않는다.
기존의 서버가 내려가지 않는다. (무중단 배포가 아닌 경우 기존 서버가 먼저 내려갈 수 있다)
TBD
배포 과정 자동화 (Github Action)
Reference
https://www.baeldung.com/spring-liveness-readiness-probes
https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/
https://velog.io/@hoonki/%EC%BF%A0%EB%B2%84%EB%84%A4%ED%8B%B0%EC%8A%A4-Probe