7.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 目录中。

7.3.1. 工作负载 max 持续时间或超时脚本示例

以下示例使用 MicroShift 核心服务健康检查脚本作为模板。

7.3.1.1. 创建健康检查脚本的基本先决条件

  • 必须安装工作负载。
  • 您必须有 root 访问权限。

7.3.1.2. 示例和功能要求

您可以使用以下示例健康检查脚本开始。根据您的用例,将其添加到其中。在自定义工作负载健康检查脚本中,您必须定义相关的命名空间、部署、daemonsetstatefulset

重要

为您的应用程序选择一个名称前缀,以确保它在 40_microshift_running_check.sh 脚本后运行,该脚本为其核心服务实施 MicroShift 健康检查过程。

greenboot 健康检查脚本示例

#!/bin/bash
set -e

SCRIPT_NAME=$(basename $0)

# Load the workload health check functions library
source /usr/share/microshift/functions/greenboot.sh

# 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 wait timeout for the current check based on the boot counter
WAIT_TIMEOUT_SECS=$(get_wait_timeout)

/usr/bin/microshift healthcheck -v=2 --timeout="${WAIT_TIMEOUT_SECS}s" --namespace busybox --deployments busybox-deployment
Copy to Clipboard Toggle word wrap

重要

与检查之前包含在 /usr/share/microshift/functions/greenboot.sh 脚本文件中的工作负载健康状况相关的功能已弃用。您可以编写自定义脚本,或使用带有各种选项的 microshift healthcheck 命令。如需更多信息,请参阅"工作负载健康检查脚本的工作方式"。

7.3.2. 测试工作负载健康检查脚本

greenboot 工作负载健康检查脚本的输出因主机系统类型而异。包括 Red Hat Enterprise Linux (RHEL)系统类型的输出示例仅用于参考。

先决条件

  • 有 root 访问权限。
  • 已安装工作负载。
  • 为工作负载创建了健康检查脚本。
  • MicroShift 服务已启用。

流程

  1. 要测试 greenboot 是否在运行健康检查脚本文件,请运行以下命令重启主机:

    $ sudo reboot
    Copy to Clipboard Toggle word wrap
  2. 运行以下命令,检查 greenboot 健康检查的输出:

    $ sudo journalctl -o cat -u greenboot-healthcheck.service
    Copy to Clipboard Toggle word wrap
    注意

    MicroShift 核心服务健康检查在工作负载健康检查前运行。

    RHEL 系统的镜像模式输出示例

    Starting greenboot Health Checks Runner...
    Running Required Health Check Scripts...
    Script '00_required_scripts_start.sh' SUCCESS
    Running Wanted Health Check Scripts...
    Script '00_wanted_scripts_start.sh' SUCCESS
    Running Required Health Check Scripts...
    --------------------
    DEPRECATION NOTICE:
    /usr/share/microshift/functions/greenboot.sh is now deprecated and will be removed in future release.
    Planned removal: MicroShift 4.21
    As a replacement consider using 'microshift healthcheck' command
    --------------------
    STARTED
    GRUB boot variables:
    boot_success=0
    Greenboot variables:
    GREENBOOT_WATCHDOG_CHECK_ENABLED=true
    MICROSHIFT_GREENBOOT_FAIL_MARKER=/run/microshift-greenboot-healthcheck-failed
    System installation type:
    bootc
    System installation status:
    bootcHost
    ??? I0403 11:54:30.526488     979 service.go:29] microshift.service is enabled
    ??? I0403 11:54:30.527145     979 service.go:31] Waiting 10m0s for microshift.service to be ready
    ??? I0403 11:58:52.530299     979 service.go:38] microshift.service is ready
    ??? I0403 11:58:52.532292     979 net.go:79] host gateway IP address: 192.168.112.125
    ??? I0403 11:58:52.555077     979 microshift_core_workloads.go:71] vgs reported: {"report":[{"vg":[{"vg_name":"rhel"}]}],"log":[]}
    ??? I0403 11:58:52.555138     979 microshift_core_workloads.go:93] Detected 1 volume group (rhel) - LVMS is expected
    ??? I0403 11:58:52.555143     979 microshift_core_workloads.go:126] Configured optional CSI components: []
    ??? I0403 11:58:52.555147     979 microshift_core_workloads.go:117] At least one CSI Component is enabled
    ??? I0403 11:58:52.555770     979 utils.go:34] Waiting for 9 goroutines
    ??? I0403 11:58:52.555791     979 workloads.go:94] Waiting 10m0s for deployment/service-ca in openshift-service-ca
    ??? I0403 11:58:52.555890     979 workloads.go:58] Waiting 10m0s for daemonset/ovnkube-master in openshift-ovn-kubernetes
    ??? I0403 11:58:52.555999     979 workloads.go:94] Waiting 10m0s for deployment/router-default in openshift-ingress
    ??? I0403 11:58:52.556096     979 workloads.go:58] Waiting 10m0s for daemonset/dns-default in openshift-dns
    ??? I0403 11:58:52.556244     979 workloads.go:58] Waiting 10m0s for daemonset/ovnkube-node in openshift-ovn-kubernetes
    ??? I0403 11:58:52.556330     979 workloads.go:94] Waiting 10m0s for deployment/lvms-operator in openshift-storage
    ??? I0403 11:58:52.556382     979 workloads.go:58] Waiting 10m0s for daemonset/vg-manager in openshift-storage
    ??? I0403 11:58:52.556425     979 workloads.go:94] Waiting 10m0s for deployment/csi-snapshot-controller in kube-system
    ??? I0403 11:58:52.556474     979 workloads.go:58] Waiting 10m0s for daemonset/node-resolver in openshift-dns
    ??? I0403 11:58:52.574284     979 workloads.go:89] Daemonset/ovnkube-node in openshift-ovn-kubernetes is ready
    ??? I0403 11:58:52.574344     979 workloads.go:89] Daemonset/dns-default in openshift-dns is ready
    ??? I0403 11:59:12.871058     979 workloads.go:89] Daemonset/node-resolver in openshift-dns is ready
    ??? I0403 11:59:12.871621     979 workloads.go:89] Daemonset/ovnkube-master in openshift-ovn-kubernetes is ready
    ??? I0403 11:59:12.871748     979 workloads.go:132] Deployment/csi-snapshot-controller in kube-system is ready
    ??? I0403 11:59:25.175015     979 workloads.go:132] Deployment/service-ca in openshift-service-ca is ready
    ??? I0403 11:59:42.559264     979 workloads.go:132] Deployment/lvms-operator in openshift-storage is ready
    ??? I0403 11:59:52.557786     979 workloads.go:132] Deployment/router-default in openshift-ingress is ready
    ??? I0403 11:59:52.558489     979 workloads.go:89] Daemonset/vg-manager in openshift-storage is ready
    ??? I0403 11:59:52.558505     979 healthcheck.go:28] MicroShift is ready
    Script '40_microshift_running_check.sh' SUCCESS
    --------------------
    DEPRECATION NOTICE:
    /usr/share/microshift/functions/greenboot.sh is now deprecated and will be removed in future release.
    Planned removal: MicroShift 4.21
    As a replacement consider using 'microshift healthcheck' command
    --------------------
    STARTED
    GRUB boot variables:
    boot_success=0
    Greenboot variables:
    GREENBOOT_WATCHDOG_CHECK_ENABLED=true
    MICROSHIFT_GREENBOOT_FAIL_MARKER=/run/microshift-greenboot-healthcheck-failed
    System installation type:
    bootc
    System installation status:
    bootcHost
    ??? I0403 11:59:52.750474    4059 service.go:29] microshift.service is enabled
    ??? I0403 11:59:52.750873    4059 service.go:31] Waiting 10m0s for microshift.service to be ready
    ??? I0403 11:59:52.752273    4059 service.go:38] microshift.service is ready
    ??? I0403 11:59:52.753263    4059 utils.go:34] Waiting for 1 goroutines
    ??? I0403 11:59:52.753393    4059 workloads.go:94] Waiting 10m0s for deployment/kserve-controller-manager in redhat-ods-applications
    ??? I0403 12:00:02.755475    4059 workloads.go:132] Deployment/kserve-controller-manager in redhat-ods-applications is ready
    ??? I0403 12:00:02.755605    4059 healthcheck.go:75] Workloads are ready
    Script '41_microshift_running_check_ai_model_serving.sh' SUCCESS
    --------------------
    DEPRECATION NOTICE:
    /usr/share/microshift/functions/greenboot.sh is now deprecated and will be removed in future release.
    Planned removal: MicroShift 4.21
    As a replacement consider using 'microshift healthcheck' command
    --------------------
    STARTED
    GRUB boot variables:
    boot_success=0
    Greenboot variables:
    GREENBOOT_WATCHDOG_CHECK_ENABLED=true
    MICROSHIFT_GREENBOOT_FAIL_MARKER=/run/microshift-greenboot-healthcheck-failed
    System installation type:
    bootc
    System installation status:
    bootcHost
    ??? I0403 12:00:02.896949    4128 service.go:29] microshift.service is enabled
    ??? I0403 12:00:02.897208    4128 service.go:31] Waiting 10m0s for microshift.service to be ready
    ??? I0403 12:00:02.899492    4128 service.go:38] microshift.service is ready
    ??? I0403 12:00:02.900279    4128 utils.go:34] Waiting for 2 goroutines
    ??? I0403 12:00:02.900363    4128 workloads.go:94] Waiting 10m0s for deployment/istiod-openshift-gateway-api in openshift-gateway-api
    ??? I0403 12:00:02.900948    4128 workloads.go:94] Waiting 10m0s for deployment/servicemesh-operator3 in openshift-gateway-api
    ??? I0403 12:00:42.913338    4128 workloads.go:132] Deployment/servicemesh-operator3 in openshift-gateway-api is ready
    ??? I0403 12:01:12.902297    4128 workloads.go:132] Deployment/istiod-openshift-gateway-api in openshift-gateway-api is ready
    ??? I0403 12:01:12.902418    4128 healthcheck.go:75] Workloads are ready
    Script '41_microshift_running_check_gateway_api.sh' SUCCESS
    --------------------
    DEPRECATION NOTICE:
    /usr/share/microshift/functions/greenboot.sh is now deprecated and will be removed in future release.
    Planned removal: MicroShift 4.21
    As a replacement consider using 'microshift healthcheck' command
    --------------------
    STARTED
    GRUB boot variables:
    boot_success=0
    Greenboot variables:
    GREENBOOT_WATCHDOG_CHECK_ENABLED=true
    MICROSHIFT_GREENBOOT_FAIL_MARKER=/run/microshift-greenboot-healthcheck-failed
    System installation type:
    bootc
    System installation status:
    bootcHost
    ??? I0403 12:01:13.057998    4772 service.go:29] microshift.service is enabled
    ??? I0403 12:01:13.058107    4772 service.go:31] Waiting 10m0s for microshift.service to be ready
    ??? I0403 12:01:13.059839    4772 service.go:38] microshift.service is ready
    ??? I0403 12:01:13.060617    4772 utils.go:34] Waiting for 2 goroutines
    ??? I0403 12:01:13.060644    4772 workloads.go:58] Waiting 10m0s for daemonset/dhcp-daemon in openshift-multus
    ??? I0403 12:01:13.060686    4772 workloads.go:58] Waiting 10m0s for daemonset/multus in openshift-multus
    ??? I0403 12:01:13.069341    4772 workloads.go:89] Daemonset/multus in openshift-multus is ready
    ??? I0403 12:01:13.069450    4772 workloads.go:89] Daemonset/dhcp-daemon in openshift-multus is ready
    ??? I0403 12:01:13.069503    4772 healthcheck.go:75] Workloads are ready
    Script '41_microshift_running_check_multus.sh' SUCCESS
    --------------------
    DEPRECATION NOTICE:
    /usr/share/microshift/functions/greenboot.sh is now deprecated and will be removed in future release.
    Planned removal: MicroShift 4.21
    As a replacement consider using 'microshift healthcheck' command
    --------------------
    STARTED
    GRUB boot variables:
    boot_success=0
    Greenboot variables:
    GREENBOOT_WATCHDOG_CHECK_ENABLED=true
    MICROSHIFT_GREENBOOT_FAIL_MARKER=/run/microshift-greenboot-healthcheck-failed
    System installation type:
    bootc
    System installation status:
    bootcHost
    ??? I0403 12:01:13.206381    4804 service.go:29] microshift.service is enabled
    ??? I0403 12:01:13.206583    4804 service.go:31] Waiting 10m0s for microshift.service to be ready
    ??? I0403 12:01:13.207979    4804 service.go:38] microshift.service is ready
    ??? I0403 12:01:13.208717    4804 utils.go:34] Waiting for 2 goroutines
    ??? I0403 12:01:13.208779    4804 workloads.go:94] Waiting 10m0s for deployment/catalog-operator in openshift-operator-lifecycle-manager
    ??? I0403 12:01:13.209285    4804 workloads.go:94] Waiting 10m0s for deployment/olm-operator in openshift-operator-lifecycle-manager
    ??? I0403 12:01:13.215578    4804 workloads.go:132] Deployment/catalog-operator in openshift-operator-lifecycle-manager is ready
    ??? I0403 12:01:13.215673    4804 workloads.go:132] Deployment/olm-operator in openshift-operator-lifecycle-manager is ready
    ??? I0403 12:01:13.215684    4804 healthcheck.go:75] Workloads are ready
    Script '50_microshift_running_check_olm.sh' SUCCESS
    Running Wanted Health Check Scripts...
    Finished greenboot Health Checks Runner.
    Copy to Clipboard Toggle word wrap

    RHEL for Edge 系统的部分输出示例

    #...
    GRUB boot variables:
    boot_success=0
    boot_indeterminate=0
    Greenboot variables:
    GREENBOOT_WATCHDOG_CHECK_ENABLED=true
    MICROSHIFT_WAIT_TIMEOUT_SEC=600
    System installation type:
    ostree
    System installation status:
    * rhel 19619bd269094510180c845c44d0944fd9aa15925376f249c4d680a3355e51ae.0
        Version: 9.6
        origin refspec: edge:rhel-9.6-microshift-4.19
    #...
    Copy to Clipboard Toggle word wrap

    RPM 系统的部分输出示例

    #...
    GRUB boot variables:
    boot_success=1
    boot_indeterminate=0
    Greenboot variables:
    GREENBOOT_WATCHDOG_CHECK_ENABLED=true
    System installation type:
    RPM
    System installation status:
    Not an ostree / bootc system
    #...
    Copy to Clipboard Toggle word wrap

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat