5.3. アプリケーションのヘルスチェックスクリプトの作成方法
このドキュメントの例を使用して、選択したテキストエディターでワークロードまたはアプリケーションのヘルスチェックスクリプトを作成できます。スクリプトを /etc/greenboot/check/required.d
ディレクトリーに保存します。/etc/greenboot/check/required.d
ディレクトリー内のスクリプトがエラーで終了すると、greenboot はシステムを修復するために再起動をトリガーします。
/etc/greenboot/check/required.d
ディレクトリー内のスクリプトは、エラーを出して終了すると再起動をトリガーします。
ヘルスチェックロジックでチェック後の手順が必要な場合は、追加のスクリプトを作成して、関連する greenboot ディレクトリーに保存することもできます。以下に例を示します。
-
ブートの成功が宣言された後に実行するシェルスクリプトを
/etc/greenboot/green.d
に配置することもできます。 -
ブートの失敗が宣言された後に実行するシェルスクリプトを
/etc/greenboot/red.d
に配置できます。たとえば、再起動する前にシステムを修復する手順がある場合は、ユースケース用のスクリプトを作成し、/etc/greenboot/red.d
ディレクトリーに配置できます。
5.3.1. ワークロードヘルスチェックスクリプトの例
次の例では、MicroShift ヘルスチェックスクリプトをテンプレートとして使用します。この例は、アプリケーションの基本的なヘルスチェックスクリプトを作成するためのガイドとして、提供されたライブラリーで使用できます。
5.3.1.1. ヘルスチェックスクリプト作成の基本要件
- ワークロードがインストールされている。
- root アクセスがある。
5.3.1.2. および機能要件の例
次のヘルスチェックスクリプトの例から始めることができます。ユースケースに合わせて変更します。ワークロードヘルスチェックスクリプトでは、最低でも、以下の手順を完了する必要があります。
- 環境変数を設定します。
- ユーザーワークロード namespace を定義します。
- 予想される Pod 数を一覧表示します。
アプリケーションの名前接頭辞を選択して、アプリケーションが 40_microshift_running_check.sh
スクリプトの後に実行されるようにします。このスクリプトは、コアサービスむけに Red Hat build of MicroShift ヘルスチェックの手順を実装します。
ワークロードヘルスチェックスクリプトの例
#!/bin/bash Update these two lines with at least one namespace and the pod counts that are specific to your workloads. Use the kubernetes <namespace> where your workload is deployed. Set greenboot to read and execute the workload health check functions library. Set the exit handler to log the exit status. Set the script exit handler to log a `FAILURE` or `FINISHED` message depending on the exit status of the last command. args: None return: None Set the system to automatically stop the script if the user running it is not 'root'. Set the script to stop without reporting an error if the MicroShift service is not running. Set the wait timeout for the current check based on the boot counter. Set the script to wait for the pod images to be downloaded. Set the script to wait for pods to enter ready state. Verify that pods are not restarting by running, which could indicate a crash loop.
# #!/bin/bash
set -e
SCRIPT_NAME=$(basename $0)
PODS_NS_LIST=(<user_workload_namespace1> <user_workload_namespace2>)
PODS_CT_LIST=(<user_workload_namespace1_pod_count> <user_workload_namespace2_pod_count>)
# Update these two lines with at least one namespace and the pod counts that are specific to your workloads. Use the kubernetes <namespace> where your workload is deployed.
# Set greenboot to read and execute the workload health check functions library.
source /usr/share/microshift/functions/greenboot.sh
# Set the exit handler to log the exit status.
trap 'script_exit' EXIT
# Set the script exit handler to log a `FAILURE` or `FINISHED` message depending on the exit status of the last command.
# args: None
# return: None
function script_exit() {
[ "$?" -ne 0 ] && status=FAILURE || status=FINISHED
echo $status
}
# Set the system to automatically stop the script if the user running it is not 'root'.
if [ $(id -u) -ne 0 ] ; then
echo "The '${SCRIPT_NAME}' script must be run with the 'root' user privileges"
exit 1
fi
echo "STARTED"
# Set the script to stop without reporting an error if the MicroShift service is not running.
if [ $(systemctl is-enabled microshift.service 2>/dev/null) != "enabled" ] ; then
echo "MicroShift service is not enabled. Exiting..."
exit 0
fi
# Set the wait timeout for the current check based on the boot counter.
WAIT_TIMEOUT_SECS=$(get_wait_timeout)
# Set the script to wait for the pod images to be downloaded.
for i in ${!PODS_NS_LIST[@]}; do
CHECK_PODS_NS=${PODS_NS_LIST[$i]}
echo "Waiting ${WAIT_TIMEOUT_SECS}s for pod image(s) from the ${CHECK_PODS_NS} namespace to be downloaded"
wait_for ${WAIT_TIMEOUT_SECS} namespace_images_downloaded
done
# Set the script to wait for pods to enter ready state.
for i in ${!PODS_NS_LIST[@]}; do
CHECK_PODS_NS=${PODS_NS_LIST[$i]}
CHECK_PODS_CT=${PODS_CT_LIST[$i]}
echo "Waiting ${WAIT_TIMEOUT_SECS}s for ${CHECK_PODS_CT} pod(s) from the ${CHECK_PODS_NS} namespace to be in 'Ready' state"
wait_for ${WAIT_TIMEOUT_SECS} namespace_pods_ready
done
# Verify that pods are not restarting by running, which could indicate a crash loop.
for i in ${!PODS_NS_LIST[@]}; do
CHECK_PODS_NS=${PODS_NS_LIST[$i]}
echo "Checking pod restart count in the ${CHECK_PODS_NS} namespace"
namespace_pods_not_restarting ${CHECK_PODS_NS}
done