[Kubernetes]
Health check
4) Health check(헬스 체크)
- Command probe:
- ✅ HTTP probe:
- TCP probe:
💡 Liveness probe, Readiness probe 차이점?
# Liveness Probe
# Readiness probe
FROM alpine:latest
RUN apk update && apk add --no-cache nodejs npm
WORKDIR /
ADD ./package.json /
RUN npm install
ADD ./webapl.js /
CMD node /webapl.js
const express = require('express')
const app = express()
var start = Date.now()
// Liveness 프로브 핸들러
// 기동 후 40초가 되면, 500 에러를 반환한다.
// 그 전까지는 HTTP 200 OK를 반환한다.
// 즉, 40초가 되면, Liveness프로브가 실패하여 컨테이너가 재기동한다.
//
app.get('/healthz', function(request, response) {
var msec = Date.now() - start
var code = 200
if (msec > 40000 ) {
code = 500
}
console.log('GET /healthz ' + code)
response.status(code).send('OK')
})
// Rediness 프로브 핸들러
// 애플리케이션의 초기화 시간으로
// 기동 후 20초 지나고 나서부터 HTTP 200을 반환한다.
// 그 전까지는 HTTPS 200 OK를 반환한다.
app.get('/ready', function(request, response) {
var msec = Date.now() - start
var code = 500
if (msec > 20000 ) {
code = 200
}
console.log('GET /ready ' + code)
response.status(code).send('OK')
})
// 첫 화면
//
app.get('/', function(request, response) {
console.log('GET /')
response.send('Hello from Node.js')
})
// 서버 포트 번호
//
app.listen(3000);
apiVersion: v1
kind: Pod
metadata:
name: webapl
spec:
containers:
- name: webapl
image: [이미지] # (1)핸들러를 구현한 애플리케이션
ex) minji/hc-repo:1.0
livenessProbe: # (2)애플리케이션이 살아있는지 확인
httpGet:
path: /healthz # 확인 경로
port: 3000
initialDelaySeconds: 3 # 검사 개시 대기 시간
periodSeconds: 5 # 검사 간격
readinessProbe: # (3) 애플리케이션이 준비되었는지 확인
httpGet:
path: /ready # 확인 경로
port: 3000
initialDelaySeconds: 15
periodSeconds: 6
docker build --tag [도커허브ID]/[레포지터리 이름]:[버전]
[ex]
docker build --tag ghdalswl77/hc-test:1.0 ./
docker run -d --name [컨테이너 이름] [도커허브ID]/[레포지터리 이름]:[버전]
[ex]
docker run -d --name test ghdalswl77/hc-test:1.0
docker inspect [컨테이너 이름]
curl http://[컨테이너 IP]:3000/healthz -v
curl http://172.17.0.2:3000/healthz -v
docker login
docker push ghdalswl77/hc-test:1.0
apiVersion: v1
kind: Pod
metadata:
name: webapl
spec:
containers:
- name: webapl
image: [이미지] # (1)핸드러를 구현한 애플리케이션 ex) ghdalswl/hc-repo:1.0
livenessProbe: # (2)애플리케이션이 살아있는지 확인
httpGet:
path: /healthz # 확인 경로
port: 3000
initialDelaySeconds: 3 # 검사 개시 대기 시간
periodSeconds: 5 # 검사 간격
readinessProbe: # (3) 애플리케이션이 준비되었는지 확인
httpGet:
path: /ready # 확인 경로
port: 3000
initialDelaySeconds: 15
periodSeconds: 6
kubectl apply -f [파일명]
kubectl get pods -o wide
kubectl describe pod webapl
![[Kubernetes] Liveness·Readiness Probe로 헬스 체크하기](/posts/kubernetes-liveness-readiness-probe/images/image_1.jpg)