5.3. How to create a health check script for your application
You can create workload or application health check scripts in the text editor of your choice using the example in this documentation. Save the scripts in the /etc/greenboot/check/required.d directory. When a script in the /etc/greenboot/check/required.d directory exits with an error, greenboot triggers a reboot in an attempt to heal the system.
Any script in the /etc/greenboot/check/required.d directory triggers a reboot if it exits with an error.
If your health check logic requires any post-check steps, you can also create additional scripts and save them in the relevant greenboot directories. For example:
-
You can also place shell scripts you want to run after a boot has been declared successful in
/etc/greenboot/green.d. -
You can place shell scripts you want to run after a boot has been declared failed in
/etc/greenboot/red.d. For example, if you have steps to heal the system before restarting, you can create scripts for your use case and place them in the/etc/greenboot/red.ddirectory.
5.3.1. About the workload health check script example リンクのコピーリンクがクリップボードにコピーされました!
The following example uses the MicroShift health check script as a template. You can use this example with the provided libraries as a guide for creating basic health check scripts for your applications.
5.3.1.1. Basic prerequisites for creating a health check script リンクのコピーリンクがクリップボードにコピーされました!
- The workload must be installed.
- You must have root access.
5.3.1.2. Example and functional requirements リンクのコピーリンクがクリップボードにコピーされました!
You can start with the following example health check script. Modify it for your use case. In your workload health check script, you must complete the following minimum steps:
- Set the environment variables.
- Define the user workload namespaces.
- List the expected pod count.
Choose a name prefix for your application that ensures it runs after the 40_microshift_running_check.sh script, which implements the Red Hat build of MicroShift health check procedure for its core services.
Example workload health check script
# #!/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