Google Cloud Platform: Creating a Virtual Machine

1Overview

Google Compute Engine lets you create and run virtual machines on Google infrastructure. Compute Engine offers scale, performance, and value that allows you to easily launch large compute clusters on Google’s infrastructure. There are no upfront investments and you can run thousands of virtual CPUs on a system that has been designed to be fast, and to offer strong consistency of performance.

You can create virtual machines running different operating systems, including multiple flavors of Linux (Debian, Ubuntu, Suse, Red Hat, CoreOS) and Windows Server!

Follow along this lab to learn about how to create virtual machine instances of various machine types.

What you’ll learn

  • Google Compute Engine
  • How to create a virtual machine
  • Predefined machine types and custom machine types

What you’ll need

  • A Google Cloud Platform Project
  • A Browser, such Chrome or Firefox

 

2Setup and Requirements

Self-paced environment setup

If you don’t already have a Google Account (Gmail or Google Apps), you must create one. Sign-in to Google Cloud Platform console (console.cloud.google.com) and create a new project:

Remember the project ID, a unique name across all Google Cloud projects (the name above has already been taken and will not work for you, sorry!). It will be referred to later in this codelab as PROJECT_ID.

Next, you’ll need to enable billing in the Cloud Console in order to use Google Cloud resources.

Running through this codelab shouldn’t cost you more than a few dollars, but it could be more if you decide to use more resources or if you leave them running (see “cleanup” section at the end of this document).

 

 

3Understanding Regions and Zones

Certain Compute Engine resources live in regions or zones. A region is a specific geographical location where you can run your resources. Each region has one or more zones. For example, the us-central1 region denotes a region in the Central United States that has zones us-central1-aus-central1-bus-central1-c, and us-central1-f.

Virtual machine Instances and persistent disks live in a zone, and these are referred to as zonal resources. For example, to attach a persistent disk to a virtual machine instance, both resources must be in the same zone.

 

 

4Create a new instance

In this section, you’ll learn how to create a new Google Compute Engine virtual machine instance from the Cloud Console.

Login into Google Cloud Console

Navigate to the the Google Cloud Console from another browser tab/window, to https://console.cloud.google.com. Use the login credential given to you by the lab proctor.

Compute Engine

In the Google Developer Console, click the Menu icon on the top left of the screen:

Then navigate to Compute Engine > VM Instances

This may take a minute to initialize for the first time. Once initialized, you should see the following if the project has no virtual machine instances:

Create a new instance

To create a new instance, click Create instance

There are many parameters you can configure when creating a new instance. Let’s use the following:

Name gcelab
Zone us-central1-c

Learn more about zones in Regions & Zones documentation.

Note: remember the zone that you selected, you’ll need it later.

Machine Type 1 vCPU (n1-standard-1)

This is a 1-CPU, 3.75GB RAM instance.

There are a number of machine types, ranging from micro instance types to 32-core/208GB RAM instance types. Learn more about this and custom machine types in the Machine Types documentation.

Note: A new project has a default resource quota, which may limit the number of CPU cores. You can request more when you work on projects outside of this lab.

Boot Disk New 10 GB standard persistent disk

Image: Debian GNU/Linux ...

There are a number of images to choose from, including: Debian, Ubuntu, CoreOS as well as premium images such as Red Hat Enterprise Linux and Windows Server. See Operating System documentation for more detail.

Note: You may optionally choose SSD Persistent Disk as well.

Firewall Check Allow HTTP traffic

Check this option so that we can access a webserver that we will install later.

Note: This will automatically create firewall rule to allow HTTP traffic on port 80.

Click on Create to create the new virtual machine instance!

Once finished, you should see the new virtual machine in the VM Instances page. Note down the External IP of the machine on the VM instances page – you will need this later.

To SSH into the virtual machine, click on SSH on the right hand side.

Install a NGINX web server

Once ssh’ed, get root access using sudo

$ sudo su -

As the root user, install NGINX:

$ apt-get update
Get:1 http://security.debian.org jessie/updates InRelease [63.1 kB]
Ign http://httpredir.debian.org jessie InRelease                                                          
Get:2 http://security.debian.org jessie/updates/main amd64 Packages [247 kB]
...
$ apt-get install nginx -y
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
...

Check that NGINX is running:

$ ps auwx | grep nginx
root      1373  0.0  0.0  91180  2960 ?        Ss   03:59   0:00 nginx: master process /usr/sbin/nginx -g daemon on
; master_process on;
www-data  1374  0.0  0.0  91544  3652 ?        S    03:59   0:00 nginx: worker process                           
www-data  1375  0.0  0.0  91544  3652 ?        S    03:59   0:00 nginx: worker process                           
www-data  1376  0.0  0.0  91544  3652 ?        S    03:59   0:00 nginx: worker process                           
www-data  1377  0.0  0.0  91544  3652 ?        S    03:59   0:00 nginx: worker process

Awesome! Let’s see the web page! Use the External IP of the virtual machine instance that you noted down before, and in a new browser window/tab, browse to http://EXTERNAL_IP/, you should see the default page.

 

 

5Create a new instance, with gcloud

Rather than using the Google Cloud Console to create a virtual machine instance, you can also use the command line tool called gcloud.

We’ll use Google Cloud Shell, a command line environment running in the Cloud. This Debian-based virtual machine is loaded with all the development tools you’ll need (gcloudgit and others) and offers a persistent 5GB home directory.

Open the Google Cloud Shell by clicking on the icon on the top right of the screen:

Once opened, you can create a new virtual machine instance from the command line by using gcloud (feel free to use another zone closer to you) :

$ gcloud compute instances create gcelab2 --zone us-central1-c
Created [...gcelab2].
NAME     ZONE           MACHINE_TYPE  PREEMPTIBLE INTERNAL_IP EXTERNAL_IP    STATUS
gcelab2  us-central1-c  n1-standard-1             10.240.X.X  X.X.X.X        RUNNING

The instance was created with some default values:

  • The latest Debian 8 (Jessie) image.
  • The n1-standard-1 machine type. In this lab you can select one of these other machine types if you’d like: n1-highmem-4 or n1-highcpu-4. When you’re working on a project outside of Qwiklabs, and none of the predefined machine types meet your needs, you can also specify a custom machine type. But to successfully complete this lab, you’ll need to stick to the predefined types above.
  • A root persistent disk with the same name as the instance; the disk is automatically attached to the instance.

Run gcloud compute instances create --help to see all the defaults.

Finally, you can SSH into your instance using gcloud as well (make sure you adjust your zone if you’ve used something else when creating the instance, or omit the --zone flag if you’ve set the option globally):

$ gcloud compute ssh gcelab2 --zone us-central1-c
WARNING: The public SSH key file for gcloud does not exist.
WARNING: The private SSH key file for gcloud does not exist.
WARNING: You do not have an SSH key for gcloud.
WARNING: [/usr/bin/ssh-keygen] will be executed to generate a key.
This tool needs to create the directory 
[/home/gcpstaging306_student/.ssh] before being able to generate SSH 
keys.

Do you want to continue (Y/n)? Y
...
username@gcelab2:~$

Disconnect from SSH by existing from the remote shell:

username@gcelab2:~$ exit



6Custom machine types

In the previous section, you learned that there are a number of predefined machine types that you can choose for your virtual machine. However, if predefined machine types do not meet your needs, you can create an instance with custom virtualized hardware settings. Specifically, you can create an instance with a custom number of vCPUs and amount of memory, effectively using a custom machine type.

Create another virtual machine instance using the Google Cloud Console, and choose a custom machine type by clicking Customize:

Then, use the sliders to pick the exact number of cores and memory to use:

Create a virtual machine instance with 2 vCPU and 5 GB of RAM.

 

 

7Clean up

It’s time to shut down those VM instances to save on cost and to be an overall good cloud citizen. You can either decide to delete the instances or simply stop them :

If you decide to delete the VM instances (after all they’re really easy to recreate), then remember that this will also delete the VM boot disks. If you’d like persistent disks, check out the Disks and Snapshots sections of the compute engine console :

 

8Congratulations!

Google Compute Engine is the foundation to Google Cloud Platform’s Infrastructure-as-a-Service. You can easily map your existing server infrastructure, load balancers, and network topology to Google Cloud Platform.

What we’ve covered

  • Google Compute Engine
  • How to create a virtual machine
  • Predefined machine types and custom machine types

Next Steps

  • Wondering how much it’ll cost to run your workloads? Check the Pricing Calculator – you can enter the number of instances, vCPU, and memory needs.