Set up an application-based health check and autohealing
Stay organized with collections
Save and categorize content based on your preferences.
This document describes how to set up an application-based health check to
autoheal VMs in a managed instance group (MIG). It also describes how to do the
following: use a health check without autohealing, remove a health check,
view autohealing policy, and check the health state of each VM.
You can configure an application-based health check to verify that your
application on a VM is responding as expected. If the health check that you
configure detects that your application on a VM isn't responding, then the MIG
marks that VM as unhealthy and repairs it by default. Repairing a VM based on
an application-based health check is called autohealing.
You can also turn off autohealing in a MIG so that you can
use a health check without triggering the repairs for unhealthy VMs.
If you haven't already, set up authentication.
Authentication verifies your identity for access to Google Cloud services and APIs. To run
code or samples from a local development environment, you can authenticate to
Compute Engine by selecting one of the following options:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and
APIs, you don't need to set up authentication.
gcloud
Install the Google Cloud CLI.
After installation,
initialize the Google Cloud CLI by running the following command:
To use the Terraform samples on this page in a local development environment, install and
initialize the gcloud CLI, and then set up Application Default Credentials with
your user credentials.
When you set up an application-based health check, whenever a VM's health
state changes, by default Compute Engine writes a log entry in
Cloud Logging.
Cloud Logging provides a
free allotment per month
after which logging is priced by data volume. To avoid costs, you can
disable
the health state change logs.
Set up an application-based health check and autohealing
To set up an application-based health check and autohealing in a MIG, you must
do the following:
You can apply a single health check to a maximum of 50 MIGs. If you have more
than 50 groups, create multiple health checks.
The following example shows how to create a health check for autohealing. You
can create either a regional
or a global health check for
autohealing in MIGs. In this example, you create a global health check that
looks for a web server
response on port 80. To enable the health check probes to reach the web
server, configure a firewall rule.
Permissions required for this task
To perform this task, you must have the following
permissions:
compute.healthChecks.create on the project if creating a
health check.
compute.healthChecks.use on the health check to use.
compute.firewalls.create on the project if creating a
firewall.
compute.networks.updatePolicy on the network if creating a
firewall.
The permissions are available in the following preconfigured
roles.
compute.networkAdmin for creating health checks.
compute.securityAdmin for configuring firewall rules to
let health checking connect.
In a MIG, you can set up only one autohealing policy to apply a health check.
Before configuring an autohealing policy, if you don't have a health check
already, then create one. You can use either a
regional or a
global health check
for autohealing in MIGs. A regional health check reduce cross-region
dependencies and help to achieve data residency whereas a global health check is
convenient if you want to use the same health check for MIGs in multiple
regions.
If you want to prevent inadvertently triggering autohealing while setting up a
new health check or want to use a health check without autohealing, then see
Configure health check without autohealing.
You can also turn off autohealing
after you configure a health check in the MIG.
To configure an autohealing policy, select one of the following options:
Permissions required for this task
To perform this task, you must have the following
permissions:
compute.instanceGroupManagers.update on the MIG.
The permissions are available in the following preconfigured
roles.
compute.instanceAdmin.v1 for creating and updating
autohealing policies in MIGs.
To configure autohealing policy in an existing MIG, use the
update command. For example, use the following command to configure
autohealing policy in an
existing zonal MIG:
To configure autohealing policy when creating a MIG, use the
create command.
For example, use the following command to configure autohealing policy when
creating a zonal MIG:
MIG_NAME: the name of the MIG in which you want to
set up autohealing.
SIZE: the number of VMs in the group.
INSTANCE_TEMPLATE_URL: the URL of the instance template that you want to
use to create instances in the MIG. The URL can contain either the
ID
or name of the instance template. Specify one of the following values:
For a regional instance template: projects/PROJECT_ID/regions/REGION/instanceTemplates/INSTANCE_TEMPLATE_ID
For a global instance template: INSTANCE_TEMPLATE_ID
HEALTH_CHECK_URL: the partial URL of the health
check that you want to set up for autohealing. For example:
Regional health check: projects/example-project/regions/us-central1/healthChecks/example-health-check.
Global health check: projects/example-project/global/healthChecks/example-health-check.
INITIAL_DELAY:
the number of seconds that a new VM takes to initialize and run its startup script. During a VM's
initial delay period, the MIG ignores unsuccessful health checks because the VM might be in the
startup process. This prevents the MIG from prematurely recreating a VM. If the health check
receives a healthy response during the initial delay, it indicates that the startup process is
complete and the VM is ready. The initial delay timer starts when the VM's
currentAction field changes to VERIFYING. The timer stops when the set
time completes or when a health check succeeds. The value of initial delay must be between
0 and 3600 seconds.
The default value is 0.
ZONE: the zone where the MIG is located. For a
regional MIG, use the --region flag.
Terraform
To configure an autohealing policy in a MIG, use the auto_healing_policies
block.
MIG_NAME: the name of the MIG in which you want to
set up autohealing.
SIZE: the number of VMs in the group.
INSTANCE_TEMPLATE_URL: the URL of the instance template that you want to
use to create instances in the MIG. The URL can contain either the
ID
or name of the instance template. Specify one of the following values:
For a regional instance template: projects/PROJECT_ID/regions/REGION/instanceTemplates/INSTANCE_TEMPLATE_ID
For a global instance template: INSTANCE_TEMPLATE_ID
HEALTH_CHECK_URL: the partial URL of the health
check that you want to set up for autohealing. For example:
Regional health check: projects/example-project/regions/us-central1/healthChecks/example-health-check.
Global health check: projects/example-project/global/healthChecks/example-health-check.
INITIAL_DELAY:
the number of seconds that a new VM takes to initialize and run its startup script. During a VM's
initial delay period, the MIG ignores unsuccessful health checks because the VM might be in the
startup process. This prevents the MIG from prematurely recreating a VM. If the health check
receives a healthy response during the initial delay, it indicates that the startup process is
complete and the VM is ready. The initial delay timer starts when the VM's
currentAction field changes to VERIFYING. The timer stops when the set
time completes or when a health check succeeds. The value of initial delay must be between
0 and 3600 seconds.
The default value is 0.
ZONE: the zone where the MIG is located. For a
regional MIG, use regions/REGION in the URL.
After the autohealing setup is complete, it can
take 10 minutes before autohealing begins monitoring VMs in the group.
After the monitoring begins, Compute Engine begins to mark VMs as
healthy (or else recreates them) based on your autohealing configuration. For
example, if you configure an initial delay of 5 minutes, a health check interval
of 1 minute, and a healthy threshold of 1 check, the timeline looks like the
following:
10 minute delay before autohealing begins monitoring VMs in the group
+ 5 minutes for the configured initial delay
+ 1 minute for the check interval * healthy threshold (60s * 1)
= 16 minutes before the VM is either marked as healthy or is recreated
Configure a health check without autohealing
You can turn off autohealing in a MIG and use the configured health check for
monitoring your application health or
you can implement your own repair logic. Turning off autohealing in a MIG
doesn't affect the functioning of the health check. The health check continues
to probe the application and provides the VM health states. However, the MIG
will no longer repair unhealthy VMs.
To configure a health check without autohealing, select one of the following
options.
Permissions required for this task
To perform this task, you must have the following
permissions:
compute.instanceGroupManagers.update on the MIG.
The permissions are available in the following preconfigured
roles.
compute.instanceAdmin.v1 for creating and updating
autohealing policies in MIGs.
After configuring the health check, you can monitor the VM health states to confirm that the health check is
working as expected. If you want the MIG to repair unhealthy VMs, you can
turn on autohealing.
Remove a health check
You can remove a health check configured in an autohealing policy as follows:
After you set up an application-based health check in a MIG,
you can verify that a VM is running and its application is responding using the
following ways:
If you have configured an application-based health check in your MIG, you can
review the health state of each managed instance.
Inspect your managed instance health states to:
Identify unhealthy VMs that are not being repaired. A VM might not
be repaired immediately even if it has been diagnosed as unhealthy in the
following situations:
The VM is still booting, and its initial delay has not passed.
A significant share of unhealthy instances is being repaired.
The MIG delays further autohealing to ensure that the group keeps
running a subset of instances.
Detect health check configuration errors. For example, you can detect
misconfigured firewall rules or an invalid application health checking
endpoint if the instance reports a health state of TIMEOUT.
Determine the initial delay value to configure by measuring the amount of time
between when the VM transitions to a RUNNINGstatus
and when the VM transitions to a HEALTHY health state. You can measure this
gap by polling the list-instances
method or by observing the time
between instances.insert operation and the first healthy signal received.
Use the
console, the
gcloud
command-line tool, or
REST
to view health states.
Permissions required for this task
To perform this task, you must have the following
permissions:
HEALTHY: The VM is reachable, a connection to the application health
checking endpoint can be established, and the response conforms to the
requirements defined by the health check.
DRAINING: The VM is being drained. Existing connections to the
VM have time to complete, but new connections are being refused.
UNHEALTHY: The VM is reachable, but does not conform to the
requirements defined by the health check.
TIMEOUT: The VM is unreachable, a connection to the application
health checking endpoint cannot be established, or the server on a VM
does not respond within the specified timeout. For example, this may be caused
by misconfigured firewall rules
or an overloaded server application on a VM.
UNKNOWN: The health checking system is not aware of the VM or its
health is not known at the moment. It can take 10 minutes for monitoring to
begin on new VMs in a MIG.
New VMs return an UNHEALTHY state until they are verified by the
health checking system.
Whether a VM is repaired depends on its health state:
If a VM has a health state of UNHEALTHY or TIMEOUT, and it
has passed its initialization period, then the
MIG immediately attempts to repair it.
If a VM has a health state of UNKNOWN, then the MIG doesn't
repair it immediately. This is to prevent an unnecessary repair of a VM
for which the health checking signal is temporarily unavailable.
Autohealing attempts can be delayed if:
A VM remains unhealthy after multiple consecutive repairs.
A significant overall share of unhealthy VMs exists in the group.
We want to learn about your use cases, challenges, or feedback about VM
health state values. You can share your feedback with our team at
mig-discuss@google.com.
Check current actions on VMs
When a MIG is in the process of creating a VM instance, the MIG sets
that instance's read-only currentAction field to CREATING. If an autohealing
policy is attached to the group, after the VM is created and running, the MIG
sets the instance's current action to VERIFYING and the health checker
begins to probe the VM's application. If the application passes this initial
health check within the time that it takes for the application to start, then
the VM is verified and the MIG changes the VM's currentAction field to NONE.
At the group level, Compute Engine populates a read-only field called
status
that contains an isStable flag.
If all VMs in the group are running and healthy (that is, the
currentAction
field for each managed instance is set to NONE), then the MIG sets the
status.isStable field to true. Remember that the stability of a MIG depends
on group configurations beyond the autohealing policy; for example, if your
group is autoscaled, and if it is being scaled in or out, then the MIG sets
the status.isStable field to false due to the autoscaler operation.
Health checks used for autohealing should be conservative so they don't
preemptively delete and recreate your instances. When an autohealer health check
is too aggressive, the autohealer might mistake busy instances for failed
instances and unnecessarily restart them, reducing availability.
unhealthy-threshold. Should be more than 1. Ideally, set this value to
3 or more. This protects against rare failures like a network packet
loss.
healthy-threshold. A value of 2 is sufficient for most apps.
timeout. Set this time value to a generous amount (five times or more
than the expected response time). The timeout must be less than or equal to
the check-interval. This protects against unexpected delays
like busy instances or a slow network connection.
check-interval. This value should be between 1 second and two times the
timeout (not too long nor too short). When a value is too long, a
failed instance is not caught soon enough. When a value is too short,
the instances and the network can become measurably busy, given the
high number of health check probes being sent every second.
Potential causes for delays in autohealing
To maintain the stability of your MIG during autohealing, Compute Engine
applies the following limits. Due to these limits, you might see delays in
autohealing.
Maximum concurrent repairs: When instances in a MIG fail health checks,
the MIG attempts to autoheal a maximum of 30% of the instances in each zone
(rounded up) at any given time. For example, if a zonal MIG has 10 VMs and 4
of them fail health checks, then the MIG attempts to repair at most 3 VMs
(30% of 10 VMs). If it is a regional MIG, then the MIG attempts to repair at
most 30% of the instances in each zone where VMs have failed—for example, if
you have 3 instances in a zone, then the MIG can repair at most 1 instance in
that zone.
Delay between subsequent repair attempts: If an instance repeatedly fails
the health check after being autohealed, then the MIG gradually increases the
time between subsequent repair attempts. This delay can extend to a maximum of
about 60 minutes between retries.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-07-17 UTC."],[],[]]