본문 바로가기

Container/Kubernetes

[K8S] Liveness Probe (Self-healing) Pod

Self-healing

Restarts containers that fail, replaces and reschedules containers when nodes die, kills containers that don't respond to your user-defined health check, and doesn't advertise them to clients until they are ready to serve.

 

# kubelet 으로 컨테이너 진단하기

# Liveness Probe

Pod가 계속 실행할 수 있음을 보장 (=self healing)

✔ Pod의 spec에 정의

 

# LivenessProbe 매커니즘

  • httpGet probe : 지정한 IP주소, port, path에 HTTP GET 요청을 보내, 해당 컨테이너가 응답하는지를 확인한다. 반환코드가 200이 아닌 값이 나오면 오류, 컨테이너 재시작
livenessProbe:
  httpGet:
    path: /
    port: 80
# 80port의 root(/)로 주기적으로 건강검진 요청 - 200번 상태 코드 : 건강
  • tcpSocket Probe: 지정된 포트에 TCP 연결 시도, 연결되지 않으면 컨테이너 다시 시작
livenessProbe:
  tcpSocket:
    port: 22
  • exec probe: exec 명령을 전달하고 명령의 종료코드가 0이 아니면 컨테이너를 다시 시작한다.
livenessProbe:
  exec:
    command:
    - ls
    - /data/file

 

👉 Pod restart가 아닌 Container restart이다. ✔ Pod는 그대로이기 때문에 IP address는 바뀌지 않는다.

 

# 매개변수

  • periodSeconds: health check 반복 실행 시간
  • initialDelaySeconds: Pod 실행 후 delay할 시간
  • timeoutSeconds: health check후 응답 기다리는 시간
[master ~]$ cat >  pod-nginx-liveness.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod-liveness
spec:
  containers:
  - name: nginx-container
    image: nginx:1.14
    ports:
    - containerPort: 80
      protocol: TCP
    livenessProbe:
      httpGet:   # webserver니까 http로 받아줌
        path: /
        port: 80
        
[master ~]$ kubectl create -f pod-nginx-liveness.yaml 
pod/nginx-pod-liveness created
[master ~]$ kubectl get pods
NAME                 READY   STATUS    RESTARTS   AGE
nginx-pod-liveness   1/1     Running   0          16s

[master ~]$ kubectl describe pod nginx-pod-liveness 
Name:             nginx-pod-liveness
Namespace:        default
...
Containers:
  nginx-container:
    Container ID:   containerd://260cbecac4922ca1bd4c9cb53291730a037b341827792d54d060bcc49da7cb8f
    Image:          nginx:1.14
    Image ID:       docker.io/library/nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Fri, 16 Feb 2024 01:09:19 +0000
    Ready:          True
    Restart Count:  0
    Liveness:       http-get http://:80/ delay=0s timeout=1s period=10s #success=1 #failure=3 # 요기!!!
    Environment:    <none>

 

운용중인 pod의 상태 yaml 을 참고하여 필요에 따라 yaml 수정하기

[master ~]$ kubectl get pod nginx-pod-liveness -o yaml
...
spec:
  containers:
  - image: nginx:1.14
    imagePullPolicy: IfNotPresent
    livenessProbe:
      failureThreshold: 3
      httpGet:
        path: /
        port: 80
        scheme: HTTP
      periodSeconds: 10
      successThreshold: 1
      timeoutSeconds: 1
      ...
# 필요에 따라 수정이 가능하다.
[master ~]$ cat pod-nginx-liveness.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod-liveness
spec:
  containers:
  - name: nginx-container
    image: nginx:1.14
    ports:
    - containerPort: 80
      protocol: TCP
    livenessProbe:
      httpGet:
        path: /
        port: 80
      periodSeconds: 30
      successThreshold: 1
      timeoutSeconds: 3
      failureThreshold: 3

 

# Liveness Probe example 

smlinux/unhealthy 컨테이너는 HTTP connection이 있을 때마다 내부 서버 오류로 HTTP 500 ERROR를 발생한다.

[node1 ~]$ kubectl describe pod liveness-pod 
Name:             liveness-pod
Namespace:        default
...

반응형

'Container > Kubernetes' 카테고리의 다른 글

[K8S] pause container / Static container  (0) 2024.02.19
[K8S] init container  (0) 2024.02.18
[K8S] YAML 템플릿 및 Pod  (0) 2024.02.13
[K8S] namespace란  (0) 2024.02.13
[K8S] 쿠버네티스 개념 및 Architecture  (0) 2024.02.09