Stopping and Starting an Instance

This page describes how to stop and start a VM instance. To learn more about stopping an instance, read about the Instance Life Cycle.

You can stop an instance temporarily if you no longer need it and restart it at a later time. A stopped instance retains its persistent disks, its internal IPs, and its MAC addresses. However, the instance shuts down the guest OS and loses its application state. Essentially, a stopped instance resets to its power-on state and no data is saved. Stop an instance if you want to change the machine type, add or remove attached disks, change the minimum CPU platform, add or remove GPUs, or apply sizing recommendations.

Stopping an instance causes Compute Engine to send the ACPI Power Off signal to the instance. Modern guest operating systems are configured to perform a clean shutdown before powering off in response to the power off signal. Compute Engine waits a short time for the guest to finish shutting down and then transitions the instance to the TERMINATED state.

A stopped instance does not incur charges, but all of the resources that are attached to the instance will still be charged. For example, you are charged for persistent disks and external IP addresses according to the price sheet, even if an instance is stopped. To stop being charged for attached resources, you can reconfigure a stopped instance to not use those resources, and then delete the resources.

Before you begin

Restrictions

You cannot stop an instance with a local SSD attached. Compute Engine does not prevent you from shutting down an instance from inside the guest operating system if the instance has a local SSD, so take extra precautions.

Local SSDs

You cannot stop an instance that has a local SSD attached. Instead, you must migrate your critical data off of the local SSD to a persistent disk or to another instance before you delete the instance completely. Compute Engine does not prevent you from shutting down the guest operating system on an instance with a local SSD, so take extra precautions if you shut down the guest operating systems on your instances.

Billing

Google does not bill VM resources once the VMs are in a TERMINATED state.

Specifically, when a VM is RUNNING and is in the process of being stopped (but is not yet TERMINATED), Google bills for vCPUs, memory, GPUs, and local SSD usage. Once the STOPPING state ends and a VM enters TERMINATED state, then Google stops billing for the above-mentioned resources.

Keep in mind that any other resources that are still attached to a TERMINATED VM will still incur charges, such as persistent disks, static external IP addresses, and so on.

For more information on instance states, read the Instance life cycle documentation.

Stopping an instance

To stop an instance, use the Google Cloud Platform Console, the gcloud tool, or the API.

Console

  1. Go to the VM instances page in the GCP Console.

    Go to the VM instances page

  2. Select one or more instances that you want to stop.
  3. At the top of the VM instances page, click Stop.

gcloud

Use the instances stop command and specify one or more instances that you want to stop.

gcloud compute instances stop example-instance-1 example-instance-2

API

In the API, construct a POST request to stop an instance.

POST https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/instances/example-instance/stop

A TERMINATED instance still exists with its configuration settings and instance metadata but loses its in-memory data and virtual machine state. Any resources that are still attached to the terminated instance will remain attached until you manually detach those resources or delete the instance.

After the instance is in the TERMINATED state, you can restart the instance or delete it. You can also leave an instance in a TERMINATED state indefinitely. However, if you do not plan to restart the instance, delete it instead.

Stopping an instance through the OS

Optionally, you can stop an instance through the guest operating system.

Linux

Use the sudo shutdown -h now or sudo poweroff commands. Execute one of these commands while you are logged into the virtual machine:

me@example-instance:~$ sudo shutdown -h now
me@example-instance:~$ sudo poweroff

Windows

You can reboot a Windows instance, similar to sudo reboot above, using the Start menu. In the Start menu, click on the arrow next to Log off and click Restart.

Starting a stopped instance

To start a stopped instance, use the instances().start method. This boots up a stopped virtual machine instance that is currently in TERMINATED state.

The start method restarts an instance in a TERMINATED state, whereas methods such as reset() and sudo reboot only work on instances that are currently running. Most instances can be restarted, as long as the instance is in a TERMINATED state, with the exception of instances with local SSDs attached, which cannot be restarted.

Console

  1. Go to the VM Instances page in the Google Cloud Platform Console.

    Go to the VM instances page

  2. Select the instances that you want to restart.
  3. At the top right-hand of the page, click the Start button to restart the instances.

gcloud

To reset your instance using gcloud compute:

gcloud compute instances start example-instance

API

In the API, make a POST request to the following URI, replacing the project, zone, and instance name appropriately:

https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/instances/example-instance/start

To restart your instance using the client libraries, construct a request to the instances().start method:

def restartInstance(auth_http, gce_service):
  request = gce_service.instances().start(project="myproject", zone="us-central1-a", instance="example-instance")
  response = request.execute(auth_http)

  print response

For more information on this method, see the instances().start reference documentation.

Restarting an instance that has encrypted disks

If the instance you want to restart uses customer-supplied encryption keys, you must provide those keys when trying to restart the instance.

Console

  1. Go to the VM instances page.

    Go to the VM Instances page

  2. Click the name of the instance that you want to start. This opens the instance details page.
  3. Click the Start button. A window opens where you can specify encryption keys for the devices that are attached to this instance.
  4. Specify encryption keys for each of the encrypted disks that are attached to this instance.
  5. Click Start to start the instance.

gcloud

Provide the key using the --csek-key-file flag and the name of the disk when you start the instance. If you are using an RSA-wrapped key, use the gcloud beta component:

gcloud compute instances start [INSTANCE_NAME] \
  --csek-key-file [ENCRYPTION_KEY]

where:

  • [INSTANCE_NAME] is the name of the instance.
  • [ENCRYPTION_KEY] is the encryption key that you use to encrypt persistent disks that are attached to the instance.

API

In the API, construct a POST request to start the instance using an encryption key. If you are using an RSA-wrapped key, make the request to the Beta API instead of the v1 API.

POST https://www.googleapis.com/compute/v1/projects/[PROJECT_ID]/zones/[ZONE]/instances/[INSTANCE_NAME]/startWithEncryptionKey
{
  "instanceEncryptionKey": {
    "rsaEncryptedKey": "[ENCRYPTION_KEY]"
  },
  "disk": [
    {
       "source": "[DISK_NAME]",
       "diskEncryptionKey": {
         "rsaEncryptedKey": "[ENCRYPTION_KEY]"
       }
    }
  ]
}

where:

  • [INSTANCE_NAME] is the name of the instance.
  • [PROJECT_ID] is your project ID.
  • [ZONE] is the zone for this instance.
  • [MACHINE_TYPE] is the machine type of the instance.
  • [DISK_NAME] is the attached disk that is encrypted with a customer-supplied encryption key.
  • [ENCRYPTION_KEY] is the encryption key that you use to encrypt persistent disks that are attached to the instance.

Resetting an instance

Performing a reset on your instance is similar to doing a hard reset on your computer where you might press a reset button or press and hold the power button. Resetting an instance forcibly wipes the memory contents of the machine and resets the virtual machine to its initial state. The instance does not perform a clean shutdown of the guest OS. Throughout this process, the instance remains in RUNNING state.

When you reset an instance, none of the instance properties will change. The instance retains its ephemeral IP address, its persistent disk, its machine type, and so on. However, any data stored in memory will be reset unless you back up the data.

You can perform a reset on a running instance by using the Reset button in the GCP Console, the instances reset command in gcloud, or by making a POST request in the API.

Console

  1. Go to the VM Instances page in the Google Cloud Platform Console.

    Go to the VM instances page

  2. Check the boxes next to one or more instances that you want to reset.
  3. At the top right-hand of the page, click the Reset button to reset the instances.

gcloud

To reset your instance using gcloud compute:

gcloud compute instances reset example-instance

API

In the API, make a POST request to the following URI, replacing the project, zone, and instance name appropriately:

https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/instances/example-instance/reset

To reset your instance using the client libraries, construct a request to the instances().reset method:

def resetInstance(auth_http, gce_service):
  request = gce_service.instances().reset(project="myproject", zone="us-central1-a", instance="example-instance")
  response = request.execute(auth_http)

  print response

For more information on this method, see the instances().reset reference documentation.

After running the reset command, the subsequent zone operation will return as DONE once the instance has completely shut down and started to reboot.

Using other restart methods

Alternatively, you can reset your instance using the following methods:

  • sudo reboot (Linux only) - Called from within the instance. Wipes the memory and re-initializes the instance with the original metadata, image, and persistent disks. It will not pick up any updated versions of the image, and the instance will retain the same ephemeral IP address. This is similar to restarting your computer.
  • gcloud compute instances delete followed by gcloud compute instances create - This is a completely destructive restart, and will initialize the instance with any information passed into gcloud compute instances create. You can then select any new images or other resources you'd like to use. The restarted instance will probably have a different IP address. This method potentially swaps the physical machine hosting the instance.

What's next

هل كانت هذه الصفحة مفيدة؟ يرجى تقييم أدائنا:

إرسال تعليقات حول...

Compute Engine Documentation