본문 바로가기
CICD

[Kubernetes] ReadinessProbe, LivenessProbe(Pod)

by 장중앙 2022. 1. 16.

Probe

컨테이너에서 Kubelet에 의해 주기적으로 수행되는 진단

 

ReadinessProbe

Pod의 생명주기중 Pending 상태에서의 동작, 서비스 요청에 응답가능한지 확인

Service와 연결된 Pod를 확인하여 Readiness Probe에 대해 응답이 없거나 실해 응답을 보낸다면 해당 Pod를 사용불가능한 상태라고 판단하여 서비스 목록에서 제외(App구동 순간에 트래픽이 흐르지 않게)

애플리케이션이 시작할 준비가 되었는지 체크하기 위함

LivenessProbe

Pod의 생명주기중 Running 상태에서의 동작

컨테이너가 정상 실행중인지 확인(LivenessProbe를 설정하지 않으면 기본 상태값은 Success)

컨테이너의 상태를 주기적으로 체크하여, 응답이 없다면 컨테이너를 자동으로 재시작한다

애플리케이션이 응답가능한지 확인

* Liveness Probe는 컨테이너 상태가 비정상이라고 판단하면, 해당 Pod를 재시작하는 반면 ReadinessProbe는 해당 Pod를 사용할 수 없음으로 체크하고 서비스 등에서 제외

 

 

Readiness/Liveness Probe의 3가지 방식

  • Command probe
    • 컨테이너의 상태 체크를 쉘 명령으로 수행한 결과에 따라 정상여부를 체크
      • 결과값이 0이면 성공, 0이 아니면 실패로 간주
    • "exec"으로 정의하고 "command : " 아래에 실행하고자 하는 쉘 명령을 기입
  • Http probe
    • 가장 많이 사용하는 Probe 방식으로 Http GET을 이용하여 컨테이너 상태를 체크
    • 지정된 URL로 HTTP GET 요청을 보내, 리턴되는 HTTP 응답코드가 200~3xx이라면 정상으로 판단
  • TCP probe
    • 지정된 포트에 TCP 연결을 시도하여, 연결이 성공되면 컨테이너가 정상인것으로 판단

 

Readiness/Liveness Probe의 옵션

initialDelaySeconds(default : 0) 최초 Probe전의 지연 시간
periodSeconds(default : 10) Probe를 체크하는 시간 간격
timeoutSeconds(default : 1) 결과를 대기하는 시간까지의
successThreshold(default : 1) 몇번의 성공 결과를 수신해야 성공으로 인정하는지
failureThreshold(default : 3) 몇번의 실패 결과를 수신해야 실패하고 인정하는지

실습

Readiness Probe 실습

서비스 생성

apiVersion: v1
kind: Service
metadata:
  name: svc-readiness
spec:
  selector:
    app: readiness
  ports:
  - port: 8080
    targetPort: 8080

Service에 연결할 Pod생성

apiVersion: v1
kind: Pod
metadata:
  name: pod1
  labels:
    app: readiness  
spec:
  containers:
  - name: container
    image: kubetm/app
    ports:
    - containerPort: 8080	
  terminationGracePeriodSeconds: 0

 

생성된 Pod와 Service 확인

콘솔창에 Service의 클러스터 ID로 계속해서 트래픽 전달

    -> Service에 연동된 Pod가 1개 밖에 없기 때문에 해당 Pod의 hostnamedl 계속 출력

Volume을 /readiness, ReadinessProbe와 연동된 Pod 생성(service와 연동된 )

apiVersion: v1
kind: Pod
metadata:
  name: pod-readiness-exec1
  labels:
    app: readiness  
spec:
  containers:
  - name: readiness
    image: kubetm/app
    ports:
    - containerPort: 8080	
    readinessProbe:
      exec:
        command: ["cat", "/readiness/ready.txt"]
      initialDelaySeconds: 5
      periodSeconds: 10
      successThreshold: 3
    volumeMounts:
    - name: host-path
      mountPath: /readiness
  volumes:
  - name : host-path
    hostPath:
      path: /tmp/readiness
      type: DirectoryOrCreate
  terminationGracePeriodSeconds: 0

Service에 연동된 Pod 확인

Service의 hostname을 출력하는 명령에는 새로운 Pod가 출력 X

    -> Service에 Pod는 연동되었지만 ReadinessProbe에 의해 ready.txt 파일이 없어 트래픽이 흐르지 않음

Pod의 이벤트 출력

Readiness probe가 fail이라는 내역 출력

해당하는 readiness/ready.txt 파일이 없기 때문에

Pod 상태 출력

Ready : false로 출력

 

* 트래픽이 흐르지 않음 -> readiness/read.txt 파일이 없기 때문에

 

Pod가 존재하는 Node에 접속하여 파일 생성

파일이 생성된 후로 트래픽이 통하는 것을 확인

Pod 상태 확인

True로 출력


LivenessProbe 실습

Service생성

apiVersion: v1
kind: Service
metadata:
  name: svc-liveness
spec:
  selector:
    app: liveness
  ports:
  - port: 8080
    targetPort: 8080

Service에 연동할 Pod 생성

apiVersion: v1
kind: Pod
metadata:
  name: pod2
  labels:
    app: liveness
spec:
  containers:
  - name: container
    image: kubetm/app
    ports:
    - containerPort: 8080
  terminationGracePeriodSeconds: 0

생성된 Service와 Pod 확인

httpGet으로 LivenessProbe를 확인하는 Pod를 생성

apiVersion: v1
kind: Pod
metadata:
  name: pod-liveness-httpget1
  labels:
    app: liveness
spec:
  containers:
  - name: liveness
    image: kubetm/app
    ports:
    - containerPort: 8080
    livenessProbe:
      httpGet:
        path: /health
        port: 8080
      initialDelaySeconds: 5
      periodSeconds: 10
      failureThreshold: 3
  terminationGracePeriodSeconds: 0

Service에 연동된 Pod를 확인

Service의 클러스터 ID로 트래픽 전송

2개의 Pod에 트래픽이 흐르는 것을 확인

liveness Pod에 이벤트를 확인

정상적으로 이미지가 Pull되고 컨테이너가 생성된것을 확인

/health를 호출했을 때, 500상태코드가 나오게끔 변경

curl "Pod cluster ID":"Pod Port"/status500

Pod의 이벤트를 확인

liveness Probe가 실패했다고 출력

이러한 LivenessProbe를 3번 실패 확인 후, 재생성

Service의 Pod 상태를 확인

LivenessProbe가 Fail 상태에서 Internal Server Error이 발생

Pod가 재생성되는 동안 연결이 거부되다, 재생성 이후로는 정상적으로 트래픽이 흐르는 것을 확인

'CICD' 카테고리의 다른 글

[Kubernetes] Node Scheduling(Pod)  (0) 2022.01.20
[Kubernetes] QoS classes(Pod)  (0) 2022.01.16
[Kubernetes] Lifecycle(Pod)  (0) 2022.01.16
[Kubernetes] DeamonSet, Job, CronJob (Controller)  (0) 2022.01.11
[Kubernetes] Deployment(Controller)  (0) 2022.01.08

댓글