0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【簡単Kubernetes】【livenessProbe】Kubernetesで livenessProbe(exec)を使ってPodの再起動を試してみた話

Posted at

今回は Kubernetes の livenessProbe を使って、意図的に Pod を「不健康」な状態にし、再起動をトリガーする方法について紹介します。


🎯 目的

  • livenessProbe の動作確認
  • exec モードによるシンプルなヘルスチェックの使い方
  • コンテナ内で一時的に「健康 → 不健康」をシミュレートし、Kubernetes の自動再起動を観察する

🛠️ 使用するYAML

以下のような Pod を定義しました:

apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness-test
  name: liveness-exec
spec:
  containers:
  - name: liveness-container
    image: k8s.gcr.io/busybox
    args:
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
    livenessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5   # コンテナ起動後、5秒経過してから最初のプローブを実行します
      periodSeconds: 5        # その後は5秒ごとにプローブを実行します

🧠 解説:このPodがやっていること

起動後に実行されるコマンド

touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
  1. /tmp/healthy ファイルを作成(= 健康な状態)
  2. 30秒間スリープ → この間は livenessProbe が成功
  3. /tmp/healthy を削除(= 不健康になる)
  4. さらに600秒スリープ(= プロセスがすぐ終了しないように)

livenessProbe の動作

livenessProbe:
  exec:
    command:
    - cat
    - /tmp/healthy
  • /tmp/healthy ファイルが存在すれば cat 成功 → Healthy
  • ファイルが消えると cat 失敗 → Unhealthy → Kubernetesが再起動

🔍 実行結果(確認方法)

kubectl apply -f liveness-test.yaml
kubectl describe pod liveness-exec
kubectl get pod liveness-exec -w
  • 起動後30秒はRunning
  • 30秒経過するとcat /tmp/healthyが失敗し、livenessProbeが失敗判定
  • Kubernetes により Pod が自動で再起動される(RESTARTSの値が増加)

✅ まとめ

  • livenessProbeexec モードは、シンプルなスクリプトやファイルの存在確認に便利。
  • この方法で自己診断・自動回復の仕組みを簡単に試すことができる。
  • 本番ではもっとしっかりしたヘルスチェック(例:HTTPリクエストなど)を使うのが一般的。

🔗 関連リンク


何かの参考になれば幸いです!
質問や改善点があれば、コメントでぜひ教えてください 🙌

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?