Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

클라우드 엔지니어 꿈나무

Kubernetes(K8S) livenessProbe를 이용해 self-healing Pod 본문

kubernetes

Kubernetes(K8S) livenessProbe를 이용해 self-healing Pod

새싹싹이 2023. 10. 14. 15:42

livernessProbe (kubelet으로 컨테이너 진단하기)

Self-healing 기능, 건강한 container로만 서비스를 제공

Pod의 spec에 정의

 

일반 pod yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
  containers:
  - name: nginx-container
    image: nginx:1.14

livernessProbe 적용 pod yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
  containers:
  - name: nginx-container
    image: nginx:1.14
    livenessProbe:
      httpGet:
        path: /
        port: 80

livernessProbe 메커니즘

- httpGet probe: 지정한 IP 주소, port, path에 HTTP GET 요청을 보내 해당 컨테이너가 응답하는지 확인. 반환 코드가 200이 아닌 값이 나오면 오류, 컨테이너를 재시작

연속해서 세 번 실패 시, 건강하지 않은 컨테이너로 판단하여 container를 죽임 => docker hub에서 건강한 container를 다시 받아 실

livenessProbe:
  httpGet:
    path:
    port: 80

 

- tcpSocket probe: 지정된 포트에 TCP 연결 시도, 연결되지 않으면 컨테이너 재시작

livenessProbe:
  tcpSocket:
    port: 22

 

 

- exec probe: exec 명령을 전달하고 명령의 종료 코드가 0이 아니면 컨테이너 재시작 (pod 재시작이 아니라 컨테이너가 재시작 = ip 는 바뀌지 않음)

livenessProbe:
  exec:
    command:
    - ls
    - /data/file
    # 어떤 명령이든 컨테이너에서 실행할 command를 나열
apiVersion: v1
kind: Pod
metadata:
  neme: nginx-pod
spec:
  containers:
  - name: nginx-container
    image: nginx:1.14
    livenessProbe:
      httpGet:
        path: /
        port: 80 #80포트 주기적으로 점검
#밑에 애들은 안 쓰면 defalut 값으로 적용
      initialDelaySeconds: 15 
      perodSeconds: 20
      timeoutSeconds: 1
      successThreshold: 1
      failureThreshold: 3

 

# vi pod-nginx.yaml

apiVersion: v1
kind: Pod
metadata:
  neme: nginx-pod
spec:
  containers:
  - name: nginx-container
    image: nginx:1.14
    livenessProbe:
      httpGet:
        path: /
        port: 80

# kubectl create -f pod-nginx.yaml

# kubectl describe pod nginx-pod 

default 값

delay=0s (initialDealySeconds) -> running 되면 바로 검진, delay=5s 라면 running 되고 5초 후 검진

timeout=1s (timeoutSeconds)

period=10s (periodSeconds) : 얼마만에 한 번씩 검사할 건지, interval이 짧으면 자주 검사, 길면 downtime이 긺

success=1 (successThreshold) : 한 번 성공하면 성공으로 봄

failure=3 (failureThreshold) : 세 번 실패하면 실패로 봄

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
  containers:
  - name: nginx-container
    image: nginx:1.14
    ports:
    - containerPort: 80
      protocol: TCP
    livenessProbe:
      httpGet:
        path: /
        port: 80
      initialDelaySeconds: 15 #pod 가 running 되고 15초 후에 검진하겠다.
      periodSeconds: 30 # 30초마다 한 번씩 interval 을 가지고 건강 검진을 하겠다
      successThreshold: 1 #한 번까지 연속 성공하면 성공으로 보겠다
      timeoutSeconds: 3 # 3초 안에 응답이 안 떨어지면 실패로 보겠다
      failureThreshold: 3 #3번까지 실패하면 실패로 보겠다

# kubectl get pod nginx-pod -o yaml => 동작 중인 pod를 yaml 파일로 보기

 

Liveness Probe example 

unheathy 처음 5번까지는 200번 상태 코드 6번부터는 500번 실패 상태 코드를 전송하는 어플리케이션 컨테이너

# vi pod-liveness.yaml

apiVersion: v1
kind: Pod
metadata:
  name: liveness-pod
spec:
  containers:
  - image: smlinux/unhealthy
    name: unhealthy-container
    #unheathy 처음 5번까지는 200번 상태 코드 6번부터는 500번 실패 상태 코드를 전송하는 어플리케이션 컨테이너
    ports:
    - containerPort: 8080
      protocol: TCP
    livenessProbe:
      httpGet:
        path: /
        port: 8080

# kubectl apply -f pod-liveness.yaml

# kubectl describe pod liveness-pod

periodSeconds:10 (default) 이기 때문에 10초마다 검사하다가 smlinux/unhealthy로 인하여 여섯 번째부터는 실패가 됨

x3 over 27s => 3번 시도했는데 다 실패 

Killing으로 실행이 되는 것을 볼 수 있음

# kubectl describe pod liveness-pod => 다시 한 번 더

하기와 같이 killing 이후 새 container를 다시 Pull하여 재시도 하는 것을 확인할 수 있음 => 어플리케이션이 제대로 동작하지 않으면 새로운 컨테이너를 생성(관리 용이)

하기 liveness-exam.yaml 파일에 self-healig 기능을 추가하시오

- 동작되는 Pod내의 컨테이너에 /tmp/healthy 파일이 있는지 5초마다 확인

- Pod 실행 후, 10초 후부터 검사

- 성공 횟수 1번, 실패 횟수 연속 2번

apiVersion: v1
kind: Pod
metadata:
  name: liveness-exam
spec:
  containers:
  - name: busybox-container
    image: busybox
    args: #arguments 컨테이너 내부에서 실행할 명령 및 인수 지정
    - /bin/sh
    - -c #명령
    - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 60
      #busybox cantainers는 실행될 때, tmp디렉토리에 healthy 파일이 만들어짐, 그러다가 30초가 지나면 그 파일 삭제시켜 버리고 600초를 지나가게 함

기능 추가 후,

apiVersion: v1
kind: Pod
metadata:
  name: liveness-exam
spec:
  containers:
  - name: busybox-container
    image: busybox
    args:
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 60
    livenessProbe:
      periodSeconds: 5
      successThreshold: 1
      timeoutSeconds: 1
      failureThreshold: 2
      initialDelaySeconds: 10
      exec:
        command:
        - ls
        - tmp/healthy

# kubectl describe liveness-exam