Hands-On Machine Learning on Google Cloud Platform
上QQ阅读APP看书,第一时间看更新

Managing the VM

There are several operations that you will want to do as you start working with a VM on Google Compute, such as starting instances, stopping instances, resizing and modifying disks, and taking snapshots. We go over the most important ones:

  1. Start and shut down the VM:
$ gcloud compute instances start sparrow --project packt-gcp 
$ gcloud compute instances stop sparrow --project packt-gcp
  1. Check the VM status:
$ gcloud compute instances list --project packt-gcp

The instance we started with is an f1-micro with not-enough CPU, RAM, or disk space for a real-world data science project. We want to change the underlying machine and augment its disk space. But, before that, we should take a snapshot of our current machine as a backup. If anything goes wrong, we'll be able to restore the instance from the snapshot:

  1. Taking a snapshot of a VM:
$ gcloud compute disks snapshot [DISK_NAME]
  1. In our case, let's call our disk sparrow-backup as we run:
$ gcloud compute disks snapshot sparrow-backup --project packt-gcp
  1. Changing the machine type, you first need to stop your instance with $ gcloud compute instances stop sparrow --project packt-gcp. Once that's done, changing the machine type is doable with the generic command:
$ gcloud compute instances set-machine-type INSTANCE --machine-type MACHINE-TYPE
  1. In our case, if we want to change the type to n1-standard-1 (3.75 GB memory and 1 vCPU), we should run this:
$ gcloud compute instances set-machine-type sparrow --machine-type n1-standard-1
  1. While we're at it, we would also like to resize the underlying disk from 10 GB to 100 GB:
$ gcloud compute disks resize sparrow --size 100
  1. Another important setting is to make sure that the disk will not be deleted when the instance is deleted:
$ gcloud compute instances set-disk-auto-delete

This is an important parameter that can also be set in the compute engine console by unselecting Delete boot disk when instance is deleted when creating or editing an instance:

  1. Instance configuration: The entire instance configuration is available via $ gcloud compute instances describe sparrow.
  2. Creating the right VM from scratch: in this all these parameters are available when you create a VM from scratch. Running the following command will create a new n1-standard-1 instance named hummingbird in the europe-west1-c zone, when running on Ubuntu 17.04, with a 100 GB disk also named hummingbird. Note that this instance is pre-emptible (--preemptible) and the disk will persist once the instance is deleted (--no-boot-disk-auto-delete):
$ gcloud compute --project packt-gcp instances create hummingbird \
--zone europe-west1-c --machine-type n1-standard-1 \
--image ubuntu-1704-zesty-v20171011 --image-project ubuntu-os-cloud \
--boot-disk-size 100 --boot-disk-type "pd-standard" \
--boot-disk-device-name hummingbird \
--preemptible --no-boot-disk-auto-delete

We can verify that we now have two instances in our project:

To keep our resources under control, we should delete this new instance with:

$ gcloud compute instances stop hummingbird --zone europe-west1-c --project packt-gcp

Note that if you have set up a different zone as default either in the config setup or as an environment variable, you need to specify the zone of the instance before you can delete it; otherwise, a resource not found error message will be generated.