Using autohealing for highly available apps Stay organized with collections Save and categorize content based on your preferences.
This interactive tutorial shows how to useautohealing to build highlyavailable apps on Compute Engine.
Highly available apps are designed to serve clients with minimal latencyand downtime. Availability is compromised when an app crashes orfreezes. Clients of a compromised app can experience high latency ordowntime.
Autohealing lets you automatically restart apps that are compromised. Itpromptly detects failed virtual machine (VM) instances and recreates themautomatically, so clients can be served again. With autohealing, you no longerneed to manually bring an app back to service after a failure.
Objectives
- Configure a health check and an autohealing policy.
- Set up a demo web service on a managed instance group (MIG).
- Simulate health check failures and witness the autohealing recovery process.
Costs
This tutorial uses billable components of Google Cloud including:- Compute Engine
Before you begin
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project: To create a project, you need the Project Creator role (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission.Learn how to grant roles. - Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project: To create a project, you need the Project Creator role (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission.Learn how to grant roles.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
Verify that billing is enabled for your Google Cloud project.
Enable the Compute Engine API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enable permission.Learn how to grant roles.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
Verify that billing is enabled for your Google Cloud project.
Enable the Compute Engine API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enable permission.Learn how to grant roles.
If you prefer to work from the command line, install the Google Cloud CLI.
Install the Google Cloud CLI. After installation,initialize the Google Cloud CLI by running the following command:
gcloudinit
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Note: You can run the gcloud CLI in the Google Cloud console without installing the Google Cloud CLI. To run the gcloud CLI in the Google Cloud console,use Cloud Shell.App architecture
The app includes the following Compute Engine components:
- Health check:An HTTP health check policy used by the autohealer to detect failed VM.
- Firewall rules:Google Cloud firewall rules let you allow or deny traffic toyour VMs.
- Managed instance group:A group of VMs running the same demo web service.
- Instance template:A template used to create each VM in the instance group.

How the health check probes the demo webservice
A health check sends probe requests to a VM using a specified protocol,such as HTTP(S), SSL, or TCP. For more information, seehow health checks work andhealth check categories, protocols, and ports.
The health check in this tutorial is an HTTP health check that probes the HTTPpath/health on port 80. For an HTTP health check, the probe request passesonly if the path returns anHTTP 200 (OK) response. For this tutorial, thedemo web server defines the path/health to return anHTTP 200 (OK) responsewhen healthy or anHTTP 500 (Internal Server Error) response when unhealthy.For more information, seesuccess criteria for HTTP, HTTPS, and HTTP/2.
Create the health check
To set up autohealing, create a custom health check and configure the networkfirewall to allow health check probes.
In this tutorial, you create a regional health check. For autohealing, you canuse either aregional or aglobal health check. Regionalhealth checks reduce cross-region dependencies andhelp to achieve data residency. Global health checks are convenient if you wantto use the same health check for MIGs in multiple regions.
Pro Tip: Use separate health checks for load balancing and forautohealing. Health checks for load balancing detect unresponsive VMs and direct traffic away from them. Health checks for autohealing detect and recreate failed VMs, so they should be less aggressive than load balancing health checks. Using the same health check for these services would remove the distinction between unresponsive VMs and failed VMs, causing unnecessary latency and unavailability for your users.For more information, seeWhat makes a good autohealing health check.
Console
Create a health check.
In the Google Cloud console, go to theCreate health check page.
In theName field, enter
autohealer-check.Set theScope to
Regional.In theRegion drop-down, select
europe-west1.ForProtocol select
HTTP.SetRequest path to
/health. This indicates what HTTP path thehealth check uses. For this tutorial, the demo web server defines thepath/healthto return either anHTTP 200 (OK)response whenhealthy or anHTTP 500 (Internal Server Error)response whenunhealthy.Set theHealth criteria:
- SetCheck interval to
10. This defines the amount of timefrom the start of one probe to the start of the next one. - SetTimeout to
5. This defines the amount of time thatGoogle Cloud waits for a response to a probe. This valuemust be less than or equal to the check interval. - SetHealthy threshold to
2. This defines the number ofsequential probes that must succeed for the VMto be considered healthy. - SetUnhealthy threshold to
3. This defines the number ofsequential probes that must fail for the VM to beconsidered unhealthy.
- SetCheck interval to
Leave default values for the other options.
ClickCreate at the bottom.
Create a firewall rule to allow health check probes to make HTTPrequests.
In the Google Cloud console, go to theCreate firewall rule page.
ForName, enter
default-allow-http-health-check.ForNetwork, select
default.ForTargets, select
All instances in the network.ForSource filter, select
IPv4 ranges.ForSource IPv4 ranges, enter
Note: Health check probes come from addresses in the ranges130.211.0.0/22, 35.191.0.0/16.130.211.0.0/22and35.191.0.0/16. For this tutorial, your healthcheck uses theHTTPprotocol, so make sure the firewall rule allowsconnections to port 80. For more information, seesetting up health checking and autohealing for managed instance groups.InProtocols and ports, selectTCP and enter
80.Leave default values for the other options.
ClickCreate.
gcloud
Create a health check using the
health-checks create httpcommand.gcloud compute health-checks create http autohealer-check \ --region europe-west1 \ --check-interval 10 \ --timeout 5 \ --healthy-threshold 2 \ --unhealthy-threshold 3 \ --request-path "/health"
check-intervaldefines the amount of time from the start of oneprobe to the start of the next one.timeoutdefines the amount of time that Google Cloudwaits for a response to a probe. This value must be less than orequal to the check interval.healthy-thresholddefines the number of sequential probes that mustsucceed for the VM to be considered healthy.unhealthy-thresholddefines the number of sequential probes thatmust fail for the VM to be considered unhealthy.request-pathindicates what HTTP path the health check uses. Forthis tutorial, the demo web server defines the path/healthtoreturn either anHTTP 200 (OK)response when healthy or anHTTP 500 (Internal Server Error)response when unhealthy.
Create a firewall rule to allow health check probes to make HTTPrequests.
Note: Health check probes come from addresses in the rangesgcloud compute firewall-rules create default-allow-http-health-check \ --network default \ --allow tcp:80 \ --source-ranges 130.211.0.0/22,35.191.0.0/16
130.211.0.0/22and35.191.0.0/16. For this tutorial, your healthcheck uses theHTTPprotocol, so make sure the firewall rule allowsconnections to port 80. For more information, seesetting up health checking and autohealing for managed instance groups.
What makes a good autohealing health check
Health checks used for autohealing should be conservative so they don'tpreemptively delete and recreate your instances. When an autohealer health checkis too aggressive, the autohealer might mistake busy instances for failedinstances and unnecessarily restart them, reducing availability.
unhealthy-threshold. Should be more than1. Ideally, set this value to3or more. This protects against rare failures like a network packet loss.healthy-threshold. A value of2is 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 thecheck-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.
Set up the web service
This tutorial uses a web app that is stored on GitHub. If you wouldlike learn more about how the app was implemented, see theGoogleCloudPlatform/python-docs-samples GitHub repository.
To set up the demo web service, create an instance template that launches thedemo web server on startup. Then, use this instance template to deploy a managedinstance group and enable autohealing.
Console
Create an instance template. Include a startup script that starts up thedemo web server.
In the Google Cloud console, go to theCreate instance template page.
Set theName to
webserver-template.In theLocation section, from theRegion drop-down, selecteurope-west1.
In theMachine configuration section, for theMachine typedrop-down, selecte2-medium.
In theFirewall section, select theAllow HTTP trafficcheckbox.
Expand theAdvanced options sectionto reveal advanced settings. Several sub-sections appear.
In theManagement section, findAutomation and enter thefollowingStartup script:
apt-get updateapt-get -y install git python3-pip python3-venvgit clone https://github.com/GoogleCloudPlatform/python-docs-samples.gitpython3 -m venv venv./venv/bin/pip3 install -Ur ./python-docs-samples/compute/managed-instances/demo/requirements.txt./venv/bin/pip3 install gunicorn./venv/bin/gunicorn --bind 0.0.0.0:80 app:app --daemon --chdir ./python-docs-samples/compute/managed-instances/demo
Leave default values for the other options.
ClickCreate.
Deploy the web server as a managed instance group.
In the Google Cloud console, go to theCreate instance group page.
Set theName to
webserver-group.ForInstance template, select
webserver-template.ForRegion, select
europe-west1.ForZone, select
europe-west1-b.In theAutoscaling section, forAutoscaling mode, selectOff: do not autoscale.
Scroll back to theNumber of instances field and set it to
3.In theAutohealing section, do the following:
Leave default values for the other options.
ClickCreate.
Create a firewall rule that allows HTTP requests to the web servers.
In the Google Cloud console, go to theCreate firewall rule page.
ForName, enter
default-allow-http.ForNetwork, select
default.ForTargets, select
Specified target tags.ForTarget Tags, enter
http-server.ForSource filter, select
IPv4 ranges.ForSource IPv4 ranges, enter
0.0.0.0/0to allow access for allIP addresses.InProtocols and ports, selectTCP and enter
80.Leave default values for the other options.
ClickCreate.
gcloud
Create an instance template. Include a startup script that starts thedemo web server.
gcloud compute instance-templates create webserver-template \ --instance-template-region europe-west1 \ --machine-type e2-medium \ --tags http-server \ --metadata startup-script=' apt-get update apt-get -y install git python3-pip python3-venv git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git python3 -m venv venv ./venv/bin/pip3 install -Ur ./python-docs-samples/compute/managed-instances/demo/requirements.txt ./venv/bin/pip3 install gunicorn ./venv/bin/gunicorn --bind 0.0.0.0:80 app:app --daemon --chdir ./python-docs-samples/compute/managed-instances/demo'
Create a managed instance group.
Note: Theinitial delay is the number of seconds that a new VM takes to initialize and run its startup script. During a VM'sinitial delay period, the MIG ignores unsuccessful health checks because the VM might be in thestartup process. This prevents the MIG from prematurely recreating a VM. If the health checkreceives a healthy response during the initial delay, it indicates that the startup process iscomplete and the VM is ready. The initial delay timer starts when the VM'sgcloud compute instance-groups managed create webserver-group \ --zone europe-west1-b \ --template projects/PROJECT_ID/regions/europe-west1/instanceTemplates/webserver-template \ --size 3 \ --health-check projects/PROJECT_ID/regions/europe-west1/healthChecks/autohealer-check \ --initial-delay 300
currentActionfield changes toVERIFYING. The timer stops when the settime completes or when a health check succeeds. The value of initial delay must be between0and3600seconds. The default value is0.Create a firewall rule that allows HTTP requests to the web servers.
gcloud compute firewall-rules create default-allow-http \ --network default \ --allow tcp:80 \ --target-tags http-server
Wait a few minutes for the managed instance group to create and verify its VMs.
Note: If your health checks fail, your VMs may be taking longer toinitialize than expected. Consider adjusting your initial delay:- In the Google Cloud console, go to theInstance groups page.
- Click
webserver-group. - ClickEdit.
- UnderInitialization period, increase the number of seconds. You may input a number from 0 to 3600.
- ClickSave and wait for the managed instance group to update.
Simulate health check failures
To simulate health check failures, the demo web server provides ways for you toforce a health check failure.
Console
Navigate to a web server VM.
In the Google Cloud console, go to theVM instances page.
For any
webserver-groupVM, under theExternal IP column,click the IP address. A new tab opens in your webbrowser. If the request times out or the web page is not available,wait a minute to let the server finish setting up and try again.
The demo web server displays a page similar to the following:

On the demo web page, clickMake unhealthy.
This causes the web server to fail the health check. Specifically,the web server makes the
/healthpath return anHTTP 500 (InternalServer Error). You can verify this yourself by quickly clicking theCheck health button (this stops working after the autohealer hasstarted rebooting the VM).Wait for the autohealer to take action.
In the Google Cloud console, go to theVM instances page.
Wait for the status of the web server VM to change. The greencheckmark next to the VM name should change to a grey square,indicating that the autohealer has started rebooting the unhealthyVM.
ClickRefresh at the top of the page periodically to get the mostrecent status.
The autohealing process is finished when the grey square changes backto a green checkmark, indicating the VM is healthy again.
gcloud
Monitor the status of the managed instance group. (When you have finished,stop by pressing
Ctrl+C.)while : ; do gcloud compute instance-groups managed list-instances webserver-group \ --zone europe-west1-b sleep 5 # Wait for 5 secondsdone
NAME: webserver-group-0zx6 ZONE: europe-west1-b STATUS: RUNNING HEALTH_STATE: HEALTHY ACTION: NONE INSTANCE_TEMPLATE: webserver-template VERSION_NAME: LAST_ERROR: NAME: webserver-group-4qbx ZONE: europe-west1-b STATUS: RUNNING HEALTH_STATE: HEALTHY ACTION: NONE INSTANCE_TEMPLATE: webserver-template VERSION_NAME: LAST_ERROR: NAME: webserver-group-m5v5 ZONE: europe-west1-b STATUS: RUNNING HEALTH_STATE: HEALTHY ACTION: NONE INSTANCE_TEMPLATE: webserver-template VERSION_NAME: LAST_ERROR:
All VMs in the group must show
STATUS: RUNNINGandACTION: NONE.If not, wait a few minutes to let the VMs finish setting up andtry again.Open a new Cloud Shell session with the Google Cloud CLI installed.
Note: If you're using Cloud Shell, you canopen multiple sessions.Get the address of a web server VM.
gcloud compute instances list --filter webserver-group
Under the
EXTERNAL_IPcolumn, copy the IP address of any web serverVM and save it as a local bash variable.export IP_ADDRESS=EXTERNAL_IP_ADDRESS
Verify the web server has finished setting up. The server returns an
HTTP 200 OKresponse.curl --head $IP_ADDRESS/health
HTTP/1.1 200 OKServer: gunicorn...
If you get a
Connection refusederror, wait a minute to let the serverfinish setting up and try again.Make the web server unhealthy.
curl $IP_ADDRESS/makeUnhealthy > /dev/null
This causes the web server to fail the health check. Specifically,the web server makes the
/healthpath return anHTTP 500 INTERNALSERVER ERROR. You can verify this yourself by quickly making a requestto/health(this stops working after the autohealer hasstarted rebooting the VM).curl --head $IP_ADDRESS/health
HTTP/1.1 500 INTERNAL SERVER ERRORServer: gunicorn...
Return to your first shell session to monitor the managed instance groupand wait for the autohealer to take action.
When the autohealing process has started, the
STATUSandACTIONcolumns update, indicating that the autohealer has started rebootingthe unhealthy VM.NAME: webserver-group-0zx6 ZONE: europe-west1-b STATUS: STOPPING HEALTH_STATE: UNHEALTHY ACTION: RECREATING INSTANCE_TEMPLATE: webserver-template VERSION_NAME: LAST_ERROR: ...
The autohealing process has finished when the VM againreports a
STATUSofRUNNINGand anACTIONofNONE, indicatingthe VM is successfully restarted.NAME: webserver-group-0zx6 ZONE: europe-west1-b STATUS: RUNNING HEALTH_STATE: HEALTHY ACTION: NONE INSTANCE_TEMPLATE: webserver-template VERSION_NAME: LAST_ERROR: ...
When you have finished monitoring the managed instance group, stop bypressing
Ctrl+C.
Feel free to repeat this exercise. Here are some ideas:
What happens if you make all VMs unhealthy at one time? For more information about autohealing behavior during concurrent failures, seeautohealing behavior.
Can you update the health check configuration to heal VMs as fast as possible? (In practice, you should set the health check parameters to use conservative values as explained in this tutorial. Otherwise, you may risk VMs being mistakenly deleted and restarted when there is no real problem.)
The managed instance group has an
initial delayconfiguration setting. Can you determine the minimum delay needed for this demo web server? (In practice, you should set the delay to somewhat longer (10%–20%) than it takes for a VM to boot and start serving app requests. Otherwise, you risk the VM getting stuck in an autohealing boot loop.)
View autohealer history (optional)
To view a history of autohealer operations use the followinggcloud command:
gcloud compute operations list --filter='operationType~compute.instances.repair.*'
For more information, seeviewing historical autohealing operations
Clean up
After you finish the tutorial, you can clean up the resources that you created so that they stop using quota and incurring charges. The following sections describe how to delete or turn off these resources.
If you created a separate project for this tutorial, delete the entire project.Otherwise, if the project has resources that you want to keep, only delete thespecific resources created in this tutorial.
Deleting the project
Deleting specific resources
If you can't delete the project used for this tutorial, delete the tutorialresources individually.
Deleting the instance group
console
- In the Google Cloud console, go to theInstance groups page.
- Select the checkbox for your
webserver-groupinstance group. - To delete the instance group, clickDelete.
gcloud
gcloud compute instance-groups managed delete webserver-group --zone europe-west1-b -q
Deleting the instance template
Note: You must delete the managed instance group before deleting the instancetemplate. You can't delete an instance template if a managed instance group uses it.console
In the Google Cloud console, go to theInstance templates page.
Click the checkbox next to the instance template.
ClickDelete atthe top of the page. In the new window, clickDelete to confirm thedeletion.
gcloud
gcloud compute instance-templates delete webserver-template -q \ --region=europe-west1
Deleting the health check
Note: You must delete the managed instance group before deleting the healthcheck. You can't delete a health check if other resources use it.console
In the Google Cloud console, go to theHealth checks page.
Click the checkbox next to the health check.
ClickDelete atthe top of the page. In the new window, clickDelete to confirm thedeletion.
gcloud
gcloud compute health-checks delete autohealer-check -q \ --region=europe-west1
Deleting the firewall rules
console
In the Google Cloud console, go to theFirewall rules page.
Click the checkboxes next to the firewall rules named
default-allow-httpanddefault-allow-http-health-check.ClickDelete atthe top of the page. In the new window, clickDelete to confirm thedeletion.
gcloud
gcloud compute firewall-rules delete default-allow-http default-allow-http-health-check -q
What's next
- Try another tutorial:
- Learn more aboutmanaged instance groups.
- Learn more aboutdesigning robust systems.
- Learn more aboutbuilding scalable and resilient web apps on Google Cloud.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-02-18 UTC.