Para usar os exemplos de linha de comando deste guia, instale a
Google Cloud CLI.
Configure a autenticação, caso ainda não tenha feito isso.
A autenticação verifica sua identidade para acesso a serviços e APIs do Google Cloud . Para executar
códigos ou amostras de um ambiente de desenvolvimento local, autentique-se no
Compute Engine selecionando uma das seguintes opções:
Select the tab for how you plan to use the samples on this page:
gcloud
Instale a CLI do Google Cloud.
Após a instalação,
inicialize a CLI do Google Cloud executando o seguinte comando:
Para mais informações, consulte
Autenticar para usar REST
na documentação de autenticação do Google Cloud .
Usar um script para simular uma interrupção de zona
Neste script, o Apache é interrompido e iniciado como o cenário padrão. Caso isso não se aplique ao aplicativo, substitua os comandos que interrompem e iniciam o Apache pelo seu próprio cenário de falha e recuperação.
Implante e execute esse script continuamente em todas as VMs do grupo. Para fazer isso, adicione o script ao modelo da instância ou inclua esse script a uma imagem personalizada e use essa imagem no modelo.
#!/usr/bin/env bash# Copyright 2016 Google Inc. All Rights Reserved.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.set-onounset
set-oerrexit
set-opipefail
functionGetMetadata(){curl-s"$1"-H"Metadata-Flavor: Google"}PROJECT_METADATA_URL="http://metadata.google.internal/computeMetadata/v1/project/attributes"INSTANCE_METADATA_URL="http://metadata.google.internal/computeMetadata/v1/instance"ZONE=$(GetMetadata"$INSTANCE_METADATA_URL/zone"|cut-d'/'-f4)INSTANCE_NAME=$(hostname)# We keep track of the state to make sure failure and recovery is triggered only once.STATE="healthy"whiletrue;doif[["$ZONE"="$(GetMetadata$PROJECT_METADATA_URL/failed_zone)"]] && \[["$INSTANCE_NAME"=*"$(GetMetadata$PROJECT_METADATA_URL/failed_instance_names)"*]];thenif[["$STATE"="healthy"]];thenSTATE="failure"# Do something to simulate failure here.echo"STARTING A FAILURE"/etc/init.d/apache2stop
fielseif[["$STATE"="failure"]];thenSTATE="healthy"# Do something to recover here.echo"RECOVERING FROM FAILURE"/etc/init.d/apache2start
fifisleep5done
Simule uma falha de zona definindo estes dois campos de metadados do projeto:
failed_zone: define a zona em que simular a falha (limite a falha a apenas uma zona);
failed_instance_names: escolha as VMs que ficarão off-line por nome (para limitar a falha apenas a nomes de VM que contenham essa string).
É possível definir esses metadados usando a CLI gcloud. Por
exemplo, o comando a seguir
define a interrupção da zona como a zona europe-west1-b e afeta as VMs que
têm nomes que começam com base-instance-name:
Aqui estão algumas ideias de cenários de falha que podem ser executados com esse script:
Interrompa seu aplicativo completamente para ver como o MIG responde.
Faça com que suas VMs retornem como "não íntegras" nas verificações de integridade de balanceamento de carga.
Modifique o iptables para bloquear parte do tráfego de e para a VM.
Encerre as VMs. Por padrão, ele será recriado pelo MIG regional logo após, mas a nova encarnação será encerrada imediatamente assim que o script for executado e enquanto os valores de metadados forem definidos. Isso acarreta um loop de falha.
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 2025-09-04 UTC."],[[["\u003cp\u003eThis guide explains how to simulate a zonal failure to test if a regional managed instance group (MIG) is overprovisioned enough to handle outages.\u003c/p\u003e\n"],["\u003cp\u003eThe simulation involves using a script that can be deployed in each VM within the group to stop and start a service, such as Apache, to mimic failure and recovery scenarios.\u003c/p\u003e\n"],["\u003cp\u003eTo simulate a zone outage, two project metadata fields need to be set: \u003ccode\u003efailed_zone\u003c/code\u003e, specifying the zone to target, and \u003ccode\u003efailed_instance_names\u003c/code\u003e, which identifies the VMs to affect.\u003c/p\u003e\n"],["\u003cp\u003eYou can use the \u003ccode\u003egcloud\u003c/code\u003e CLI to set the metadata values for the simulated outage and then remove them to recover from the simulated failure.\u003c/p\u003e\n"],["\u003cp\u003eThe provided failure script can be used to test various failure scenarios, including stopping the application, causing VMs to return as "unhealthy," modifying iptables, and shutting down VMs.\u003c/p\u003e\n"]]],[],null,["*** ** * ** ***\n\nTo test that your\n[regional managed instance group (MIG)](/compute/docs/instance-groups#managed_instance_groups)\nis overprovisioned enough and can survive a zone outage, you can use the\nfollowing example to simulate a zonal failure.\n\nBefore you begin\n\n- If you want to use the command-line examples in this guide, install the [Google Cloud CLI](/compute/docs/gcloud-compute).\n- If you haven't already, set up [authentication](/compute/docs/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:\n\n Select the tab for how you plan to use the samples on this page: \n\n gcloud\n 1.\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n After installation,\n [initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command:\n\n ```bash\n gcloud init\n ```\n\n\n If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n | **Note:** If you installed the gcloud CLI previously, make sure you have the latest version by running `gcloud components update`.\n 2. [Set a default region and zone](/compute/docs/gcloud-compute#set_default_zone_and_region_in_your_local_client).\n\n REST\n\n\n To use the REST API samples on this page in a local development environment, you use the\n credentials you provide to the gcloud CLI.\n 1. [Install](/sdk/docs/install) the Google Cloud CLI. After installation, [initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command: \n\n ```bash\n gcloud init\n ```\n 2. If you're using an external identity provider (IdP), you must first [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n | **Note:** If you installed the gcloud CLI previously, make sure you have the latest version by running `gcloud components update`.\n\n\n For more information, see\n [Authenticate for using REST](/docs/authentication/rest)\n in the Google Cloud authentication documentation.\n\nUse a script to simulate a zone outage\n\nThis script stops and starts Apache as the default scenario. If\nthis doesn't apply to your application, replace the commands that\nstop and start Apache with your own failure and recovery scenario.\n\n1. Deploy and run this script continuously in every VM in the group. You can do\n this by adding the script to the\n instance template or by including the script in a\n [custom image](/compute/docs/images/create-delete-deprecate-private-images)\n and using the image in the instance template.\n\n #!/usr/bin/env bash\n\n # Copyright 2016 Google Inc. All Rights Reserved.\n #\n # Licensed under the Apache License, Version 2.0 (the \"License\");\n # you may not use this file except in compliance with the License.\n # You may obtain a copy of the License at\n #\n # http://www.apache.org/licenses/LICENSE-2.0\n #\n # Unless required by applicable law or agreed to in writing, software\n # distributed under the License is distributed on an \"AS IS\" BASIS,\n # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n # See the License for the specific language governing permissions and\n # limitations under the License.\n\n set -o nounset\n set -o errexit\n set -o pipefail\n\n function GetMetadata() {\n curl -s \"$1\" -H \"Metadata-Flavor: Google\"\n }\n\n PROJECT_METADATA_URL=\"http://metadata.google.internal/computeMetadata/v1/project/attributes\"\n INSTANCE_METADATA_URL=\"http://metadata.google.internal/computeMetadata/v1/instance\"\n ZONE=$(GetMetadata \"$INSTANCE_METADATA_URL/zone\" | cut -d '/' -f 4)\n INSTANCE_NAME=$(hostname)\n\n # We keep track of the state to make sure failure and recovery is triggered only once.\n STATE=\"healthy\"\n while true; do\n if [[ \"$ZONE\" = \"$(GetMetadata $PROJECT_METADATA_URL/failed_zone)\" ]] && \\\n [[ \"$INSTANCE_NAME\" = *\"$(GetMetadata $PROJECT_METADATA_URL/failed_instance_names)\"* ]]; then\n if [[ \"$STATE\" = \"healthy\" ]]; then\n STATE=\"failure\"\n # Do something to simulate failure here.\n echo \"STARTING A FAILURE\"\n /etc/init.d/apache2 stop\n fi\n else\n if [[ \"$STATE\" = \"failure\" ]] ; then\n STATE=\"healthy\"\n # Do something to recover here.\n echo \"RECOVERING FROM FAILURE\"\n /etc/init.d/apache2 start\n fi\n fi\n sleep 5\n done\n\n2. Simulate a zone failure by setting these two project metadata fields:\n\n - `failed_zone`: Sets the zone where you want to simulate the outage (limit the failure to just one zone).\n - `failed_instance_names`: Choose the VMs to take offline by name (to limit the failure to only VM names containing this string).\n\n You can set this metadata using the gcloud CLI. For\n example, the following command\n sets the zone outage to the `europe-west1-b` zone and affects VMs that\n have names starting with `base-instance-name`: \n\n ```\n gcloud compute project-info add-metadata --metadata failed_zone='europe-west1-b',failed_instance_names='base-instance-name-'\n ```\n3. After you are done simulating the outage, recover from the failure by\n removing the metadata keys:\n\n ```\n gcloud compute project-info remove-metadata --keys failed_zone,failed_instance_names\n ```\n\nHere are some ideas for failure scenarios you can run using this script:\n\n- Stop your application completely to see how the MIG responds.\n- Make your VMs return as \"unhealthy\" on load balancing health checks.\n- Modify iptables to block some of the traffic to and from the VM.\n- Shutdown the VMs. By default, it will be recreated by the regional MIG shortly after but the new incarnation will immediately shutdown itself as soon as the script runs and as long as the metadata values are set. This will result in a crash loop.\n\nWhat's next\n\n- Learn how to build [scalable and resilient web applications](/solutions/scalable-and-resilient-apps).\n- Learn about [disaster recovery on Google Cloud Platform](/solutions/disaster-recovery-cookbook)."]]