|
1 |
| -// Module included in the following assemblies: |
| 1 | +//Updated title and ID: |
| 2 | +//Module included in the following assemblies: |
2 | 3 | //
|
3 |
| -// * microshift_running applications/microshift-greenboot.adoc |
| 4 | +//* microshift_running_apps/microshift-greenboot-workload-scripts.adoc |
4 | 5 |
|
5 |
| -:_content-type: PROCEDURE |
6 |
| -[id="microshift-greenboot-create-health-check-script_{context}"] |
7 |
| -= Creating a health check script |
| 6 | +:_content-type: CONCEPT |
| 7 | +[id="microshift-greenboot-app-health-check-script_{context}"] |
| 8 | += How to create a health check script for your application |
8 | 9 |
|
9 |
| -You can create a health check script for installed workloads by placing them in the `/etc/greenboot/check/required.d` directory. The following procedure provides an example of installing the busybox application and creating a health check script for busybox. You can use this example as a general guide for creating health check scripts for your applications. |
| 10 | +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. |
10 | 11 |
|
11 |
| -.Prerequisite |
| 12 | +[NOTE] |
| 13 | +==== |
| 14 | +Any script in the `/etc/greenboot/check/required.d` directory triggers a reboot if it exits with an error. |
| 15 | +==== |
12 | 16 |
|
13 |
| -* You have installed a workload. For this example, the busybox application is used as a workload. The "Additional resources" section that follows this procedure has a link to instructions on deploying workloads using manifests. |
| 17 | +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: |
14 | 18 |
|
15 |
| -.Procedure |
| 19 | +* You can also place shell scripts you want to run after a boot has been declared successful in `/etc/greenboot/green.d`. |
| 20 | +* 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.d` directory. |
16 | 21 |
|
17 |
| -. To create a health check script, run the following command: |
18 |
| -+ |
19 |
| -[source, terminal] |
20 |
| ----- |
21 |
| -$ SCRIPT_FILE=/etc/greenboot/check/required.d/50_busybox_running_check.sh |
22 |
| -sudo curl -s https://raw.githubusercontent.com/openshift/microshift/3b7f6025cd77bd1bf827416fd026783ead82b7c8/docs/config/busybox_running_check.sh \ |
23 |
| - -o ${SCRIPT_FILE} && echo SUCCESS || echo ERROR |
24 |
| -sudo chmod 755 ${SCRIPT_FILE} |
25 |
| ----- |
26 |
| -+ |
27 |
| -In this example, the script verifies that busybox is running as expected. You can replace `/etc/greenboot/check/required.d/50_busybox_running_check.sh` with your own workload details. |
28 |
| -+ |
29 |
| -[NOTE] |
| 22 | +[id="microshift-greenboot-about-workload-health-check-script-example_{context}"] |
| 23 | +== About the workload health check script example |
| 24 | + |
| 25 | +The following example uses the {product-title} 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. |
| 26 | + |
| 27 | +[id="microshift-greenboot-app-health-check-basic-prereqs_{context}"] |
| 28 | +=== Basic prerequisites for creating a health check script |
| 29 | + |
| 30 | +* The workload must be installed. |
| 31 | +* You must have root access. |
| 32 | + |
| 33 | +[id="microshift-greenboot-app-health-check-ex-reqs_{context}"] |
| 34 | +=== Example and functional requirements |
| 35 | + |
| 36 | +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: |
| 37 | + |
| 38 | +* Set the environment variables. |
| 39 | +* Define the user workload namespaces. |
| 40 | +* List the expected pod count. |
| 41 | + |
| 42 | +[IMPORTANT] |
30 | 43 | ====
|
31 |
| -In this example, the {product-title} core service health checks run before the user workload health checks. |
| 44 | +Choose a name prefix for your application that ensures it runs after the `40_microshift_running_check.sh` script, which implements the {product-title} health check procedure for its core services. |
32 | 45 | ====
|
33 | 46 |
|
34 |
| -. To test that your script is running as expected: |
35 |
| - |
36 |
| -.. Restart the system. |
| 47 | +.Example workload health check script |
37 | 48 |
|
38 |
| -.. Once the system has restarted, run the following command: |
39 |
| -+ |
40 |
| -[source, terminal] |
41 |
| ----- |
42 |
| -$ sudo journalctl -o cat -u greenboot-healthcheck.service |
| 49 | +[source, bash] |
43 | 50 | ----
|
44 |
| -+ |
45 |
| -.Example output for the busybox health check script |
46 |
| -+ |
47 |
| -[source, terminal] |
48 |
| ----- |
49 |
| -... |
50 |
| -... |
51 |
| -STARTED |
52 |
| -Waiting 300s for pod image(s) from the 'busybox' namespace to be downloaded |
53 |
| -Waiting 300s for 1 pod(s) from the 'busybox' namespace to be in 'Ready' state |
54 |
| -Checking pod restart count in the 'busybox' namespace |
55 |
| -FINISHED |
56 |
| -Script '50_busybox_running_check.sh' SUCCESS |
| 51 | +# #!/bin/bash |
| 52 | +set -e |
| 53 | +
|
| 54 | +SCRIPT_NAME=$(basename $0) |
| 55 | +PODS_NS_LIST=(<user_workload_namespace1> <user_workload_namespace2>) |
| 56 | +PODS_CT_LIST=(<user_workload_namespace1_pod_count> <user_workload_namespace2_pod_count>) |
| 57 | +# 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. |
| 58 | +
|
| 59 | +# Set greenboot to read and execute the workload health check functions library. |
| 60 | +source /usr/share/microshift/functions/greenboot.sh |
| 61 | +
|
| 62 | +# Set the exit handler to log the exit status. |
| 63 | +trap 'script_exit' EXIT |
| 64 | +
|
| 65 | +# Set the script exit handler to log a `FAILURE` or `FINISHED` message depending on the exit status of the last command. |
| 66 | +# args: None |
| 67 | +# return: None |
| 68 | +function script_exit() { |
| 69 | + [ "$?" -ne 0 ] && status=FAILURE || status=FINISHED |
| 70 | + echo $status |
| 71 | +} |
| 72 | +
|
| 73 | +# Set the system to automatically stop the script if the user running it is not 'root'. |
| 74 | +if [ $(id -u) -ne 0 ] ; then |
| 75 | + echo "The '${SCRIPT_NAME}' script must be run with the 'root' user privileges" |
| 76 | + exit 1 |
| 77 | +fi |
| 78 | +
|
| 79 | +echo "STARTED" |
| 80 | +
|
| 81 | +# Set the script to stop without reporting an error if the MicroShift service is not running. |
| 82 | +if [ $(systemctl is-enabled microshift.service 2>/dev/null) != "enabled" ] ; then |
| 83 | + echo "MicroShift service is not enabled. Exiting..." |
| 84 | + exit 0 |
| 85 | +fi |
| 86 | +
|
| 87 | +# Set the wait timeout for the current check based on the boot counter. |
| 88 | +WAIT_TIMEOUT_SECS=$(get_wait_timeout) |
| 89 | +
|
| 90 | +# Set the script to wait for the pod images to be downloaded. |
| 91 | +for i in ${!PODS_NS_LIST[@]}; do |
| 92 | + CHECK_PODS_NS=${PODS_NS_LIST[$i]} |
| 93 | +
|
| 94 | + echo "Waiting ${WAIT_TIMEOUT_SECS}s for pod image(s) from the ${CHECK_PODS_NS} namespace to be downloaded" |
| 95 | + wait_for ${WAIT_TIMEOUT_SECS} namespace_images_downloaded |
| 96 | +done |
| 97 | +
|
| 98 | +# Set the script to wait for pods to enter ready state. |
| 99 | +for i in ${!PODS_NS_LIST[@]}; do |
| 100 | + CHECK_PODS_NS=${PODS_NS_LIST[$i]} |
| 101 | + CHECK_PODS_CT=${PODS_CT_LIST[$i]} |
| 102 | +
|
| 103 | + echo "Waiting ${WAIT_TIMEOUT_SECS}s for ${CHECK_PODS_CT} pod(s) from the ${CHECK_PODS_NS} namespace to be in 'Ready' state" |
| 104 | + wait_for ${WAIT_TIMEOUT_SECS} namespace_pods_ready |
| 105 | +done |
| 106 | +
|
| 107 | +# Verify that pods are not restarting by running, which could indicate a crash loop. |
| 108 | +for i in ${!PODS_NS_LIST[@]}; do |
| 109 | + CHECK_PODS_NS=${PODS_NS_LIST[$i]} |
| 110 | +
|
| 111 | + echo "Checking pod restart count in the ${CHECK_PODS_NS} namespace" |
| 112 | + namespace_pods_not_restarting ${CHECK_PODS_NS} |
| 113 | +done |
57 | 114 | ----
|
0 commit comments