6.3. 如何为您的应用程序创建健康检查脚本
您可以使用本文档中的示例在您选择的文本编辑器中创建工作负载或应用程序健康检查脚本。将脚本保存到 /etc/greenboot/check/required.d 目录中。当 /etc/greenboot/check/required.d 目录中的脚本退出时,greenboot 会触发重启尝试修复系统。
注意
如果 /etc/greenboot/check/required.d 目录中的任何脚本退出并显示错误,则会触发重新引导。
如果您的健康检查逻辑需要任何 post-check 步骤,您也可以创建额外的脚本并将其保存到相关的 greenboot 目录中。例如:
-
您还可以在
/etc/greenboot/green.d中声明成功后要运行的 shell 脚本。 -
您可以将在声明引导失败后要运行的 shell 脚本放在
/etc/greenboot/red.d中。例如,如果您在重启前有修复系统的步骤,您可以为用例创建脚本并将其放在/etc/greenboot/red.d目录中。
6.3.1. 关于工作负载健康检查脚本示例 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
以下示例使用 MicroShift 健康检查脚本作为模板。您可以将此示例与提供的库一起使用,作为为您的应用程序创建基本健康检查脚本的指南。
6.3.1.1. 创建健康检查脚本的基本先决条件 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
- 必须安装工作负载。
- 您必须有 root 访问权限。
6.3.1.2. 示例和功能要求 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
您可以使用以下示例健康检查脚本开始。根据您的用例修改它。在工作负载健康检查脚本中,您必须完成以下最小步骤:
- 设置环境变量。
- 定义用户工作负载命名空间。
- 列出预期的 pod 数量。
重要
为您的应用程序选择一个名称前缀,以确保它在 40_microshift_running_check.sh 脚本后运行,该脚本为其核心服务实施 MicroShift 健康检查过程。
工作负载健康检查脚本示例
# #!/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