본문 바로가기
카테고리 없음

[Kubernetes] Kubernetes in action - probe

by guru_k 2021. 12. 7.
728x90
반응형

Kubernetes 에서 Container Probe를 사용해서 Container의 health check를 진행할 수 있다.

Probe 종류

Kubernetes 에서 사용할 수 있는 Probe는 LivenessProbe, ReadnessProbe 가 있다.

LivenessProbe

LivenessProbe 가 설정이 되어 있지 않은 경우의 default 는 success이며, LivenessProbe는 컨테이너의 동작 여부를 체크한다.

만약 LivenessProbe에 실패하게 되면 kubelet은 컨테이너를 종료시키며 설정된 restartPolicy에 따라 container의 다음상태를 결정한다.

LivenessProbe는 기본적으로 아래와 같이 설정할 수 있다.

apiVersion: v1
kind: Pod
metadata:
  name: foo
spec:
  containers:
  - image: foo  
    name: foo
    livenessProbe:                
      httpGet:                    
        path: /                   
        port: 8080

위에 설정은 HTTP GET Method를 사용하며 http request의 path는 /, Probe를 위한 연결 Port는 8080으로 설정된다.

위와 같이 LivenessProbe를 설정 후 Pod을 생성한 이후 describe를 통해 Liveness 상태와 Liveness로 인해 발생된 이벤트들의 로그를 확인할 수 있다.

$ kubectl describe po foo
Name:           foo
...
Containers:
  foo:
    ...
    Last State:         Terminated                                      ❷
      Reason:           Error                                           ❷
      Exit Code:        137                                             ❷
      Started:          SUN, 14 May 2021 12:40:00 +0000                 ❷
      Finished:         Sun, 14 May 2021 12:41:38 +0000                 ❷
    Ready:              True
    Restart Count:      1                                               ❸
    Liveness:           http-get http://:8080/ delay=0s timeout=1s
                        period=10s #success=1 #failure=3
    ...
Events:
... Killing container with id docker://95246981:pod "foo ..."
    container "foo" is unhealthy, it will be killed and re-created.

ReadinessProbe

ReadinessProbe는 Container가 요청을 처리할 준비가 되었는지에 대한 검사를 진행 한다. 만약에 ReadinessProbe가 failure 라면 엔드포인트 컨트롤러는 해당 Pod으로 연결을 차단하고 트래픽이 전송되지 않도록 한다.

ReadinessProbe 또한 아래와 같이 설정 할 수 있다.

apiVersion: v1
kind: Pod
metadata:
  name: foo
spec:
  containers:
  - image: foo  
    name: foo
    readinessProbe:                
      httpGet:                    
        path: /                   
        port: 8080

Probe Action

Probe Action에는 3가지가 있으며 각각은 다음과 같다.

- HTTP Get Probe: 해당 Probe는 HTTP Get Request를 Container의 IP를 설정된 Path와 Port로 호출을 하며, response를 정상적으로 받았으며 response가 error code를 포함하고 있지 않을경우 success라고 판단한다. 그래서 error response code가 return 되거나 응답이 전혀 없을 경우 failure 라고 판단하여 Container는 자동적으로 재시작 된다.

- TCP Socket Probe: 특정 Port로 TCP Connection을 열고 Connection이 정상적으로 연결되었으면 successful이라고 판단하며 그 반대의 경우 failure라고 판단하여 Container를 재시작한다.

- Exec Probe: Container 내부에서 별도의 Command를 실행한 이후 Command의 exit status code를 체크 한다. exit status code가 0일 경우 successful이라고 판단하며 다른 exit status code일 경우 failure라고 판단한다.

결론

Probe는 Container의 상태를 체크할 수 있는 좋은 기능이다. 서비스가 실제로 Production에 투입되기 전에 정상적으로 구동이 될 수 있는지 판단할 수 있는 좋은 도구이다.

 

728x90
반응형

댓글