프로세스 생존과 의존성 준비 상태를 구분합니다.
liveness는 프로세스 재시작 필요 여부, readiness는 현재 트래픽을 받을 준비가 됐는지 나타냅니다. DB와 필수 broker 상태는 readiness에서 제한 시간과 함께 확인합니다.
외부 의존성 하나의 일시 실패가 전체 pod 재시작 폭주로 이어지지 않게 probe 의미를 구분합니다.
응답에는 비밀 연결 문자열이나 내부 topology를 노출하지 않습니다.
health endpoint에서 무제한 DB 쿼리를 실행하면 장애 때 probe가 부하를 키웁니다.
로컬 프로젝트에서 확인해보세요ready=false일 때 HTTP 503을 반환하도록 endpoint를 분리하세요.
import "reflect-metadata";
import { Controller, Get, Module } from "@nestjs/common";
import { NestFactory } from "@nestjs/core";
@Controller("health")
class HealthController {
@Get("live") live() {
return { status: "ok" };
}
@Get("ready") ready() {
const checks = { database: "up", memory: "ok" };
return {
status: Object.values(checks).every((v) => v === "up" || v === "ok")
? "ready"
: "not-ready",
checks,
};
}
}
@Controller()
class HomeController {
@Get() home() {
return `<main><a href="/health/live">Liveness</a> <a href="/health/ready">Readiness</a></main>`;
}
}
@Module({ controllers: [HomeController, HealthController] })
class AppModule {}
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(4173, "0.0.0.0");
}
bootstrap();