livenessProbe는 파드의 건강검진과 같다고 생각할 수 있다. Pod가 계속해서 실행할 수 있음을 보장해준다.
kubelet은 livenessProbe를 사용해 컨테이너를 재시작해야할 때를 알 수 있다.
livenessProbe를 Pod의 Spec에 정의해두면 이 내용을 기반으로 Container의 상태를 주기적으로 확인한다.
만약 Container 상태가 비정상적이라면, Container를 재시작한다.
이때 Container만 재시작하기 때문에 Pod의 IP Address는 그대로 유지된다.
livenessProbe 매커니즘
✅ httpGet probe (web 기반)
지정한 IP 주소, Port, Path에 HTTP Get 요청을 보내 응답을 확인한다.
만약 nginx webserver를 80포트로 띄워둔 상황이라면, 주기적으로 80포트로 Get 요청을 보내고, 응답을 확인한다.
응답이 200이 아닌 값이 오면 오류로 간주하고 컨테이너를 다시 시작한다.
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
nginx-pod의 로그를 확인해보면 10초마다 Get 요청을 보내는 것을 확인할 수 있다.

✅ tcpSocket Probe
지정된 포트에 TCP 연결을 시도하고, 연결되지 않으면 컨테이너를 다시 시작한다.
livenessProbe:
tcpSocket:
port: 22
✅ exec Probe
exec 명령어를 전달하고 명령의 종료 코드가 0이 아니면 컨테이너를 다시 시작한다.
예를 들어 데이터 베이스에서 데이터를 읽어오는 파드가 있다고 하자.
파드에 성공적으로 데이터를 읽어왔는지 확인하기 위해서 아래와 같은 livenessProbe를 지정할 수 있다.
livenessProbe:
exec:
command:
- ls
- /data/file
[예시]
busybox 컨테이너에 /tmp/healthy 디렉터리를 확인하는 livenessProbe를 정의했다.
livenessProbe 매개변수는 아래에 정리해두었다.
apiVersion: v1
kind: Pod
metadata:
name: busybox
spec:
containers:
- name: busybox-container
image: busybox
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 50; rm -rf /tmp/healthy; sleep 30;
livenessProbe:
exec:
command:
- ls
- /tmp/healthy
periodSeconds: 10
initialDelaySeconds: 10
successThreshold: 1
failureThreshold: 2
/tmp/healthy 디렉터리는 생성된지 50초만에 지워지기 때문에 livenessProbe에 의해 파드는 재시작한다.

livenessProbe 매개변수
- periodSeconds: health check 반복 실행 시간 (초)
- initialDelaySeconds: Pod 실행 후 delay할 시간 (초)
- timeoutSeconds: health check 후 응답을 기다리는 시간 (초)
- failureThreshold: n번 실패 시 실패
- successThreshold: n번 성공 시 성공
[실습]
- image: smlinux/unhealthy
해당 이미지는 처음 5회만 200으로 응답오는 이미지이다.
이 이미지를 이용해 pod-liveness pod를 생성해보자.
apiVersion: v1
kind: Pod
metadata:
name: pod-liveness
spec:
containers:
- name: unhealthy
image: smlinux/unhealthy
livenessProbe:
httpGet:
path: /
port: 80
failureThreshold: 2
initialDelaySeconds: 10
periodSeconds: 20
$ kubectl describe pod pod-liveness

'Kubernetes' 카테고리의 다른 글
mount.nfs: access denied by server while mounting (0) | 2022.03.02 |
---|---|
5-3. Init container (0) | 2022.02.07 |
4. kubernetes architecture (0) | 2022.02.06 |
Deployment를 이용해 Container 실행하기 (0) | 2022.02.05 |
kubeconfig에 대해서 알아보자 (0) | 2022.02.05 |