Configure Liveness, Readiness and Startup Probes
Liveness
ポッド(コンテナ)の状態を監視し不意に停止してしまったときに再起動をかけるためのポッドの設定条件
以下 livenessProbe
で /tmp/healthy
を5秒 (periodSeconds)
ごとに cat
するデモ
initialDelaySeconds
は kubelet
が最初に cat
コマンドを実行するまでの待ち時間
( /tmp/healthy
ファイルが作成後30秒後に削除されます)
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-exec
spec:
containers:
- name: liveness
image: registry.k8s.io/busybox
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 600
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
上記ポッドの作成・起動
注) 既存のポッドが起動していて変更した設定を適用する場合は kubectl apply
を指定するが、既存のポッドが存在しない場合には $ kubectl apply
または $ kubectl create
でも可
$ kubectl apply -f https://k8s.io/examples/pods/probe/exec-liveness.yaml
pod/liveness-exec created
30秒後にポッドが再起動していることを確認
$ kubectl describe pod liveness-exec
.....
.....
.....
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 65s default-scheduler Successfully assigned default/liveness-exec to minikube
Normal Pulling 61s kubelet Pulling image "registry.k8s.io/busybox"
Normal Pulled 54s kubelet Successfully pulled image "registry.k8s.io/busybox" in 6.604873952s
Normal Created 51s kubelet Created container liveness
Normal Started 50s kubelet Started container liveness
Warning Unhealthy 10s (x3 over 20s) kubelet Liveness probe failed: cat: can't open '/tmp/healthy': No such file or directory
Normal Killing 10s kubelet Container liveness failed liveness probe, will be restarted
ポッドの再起動回数確認
$ kubectl get pod liveness-exec
NAME READY STATUS RESTARTS AGE
liveness-exec 1/1 Running 3 (34s ago) 4m40s