Google Compute Engine lets you create virtual machines running different operating systems, including multiple flavors of Linux (Debian, Ubuntu, Suse, Red Hat, CoreOS) and Windows Server, on Google infrastructure. You can run thousands of virtual CPUs on a system that has been designed to be fast and to offer strong consistency of performance.
Here, you’ll learn how to create virtual machine instances of various machine types using the Google Cloud Platform (GCP) Console and using the
gcloud command line. You’ll also learn how to connect an NGINX web server to your virtual machine.
You should type the commands to reinforce their understanding of the core concepts .
- Create a virtual machine with the GCP Console
- Create a virtual machine with
- Deploy a web server and connect it to a virtual machine
After login into your cloud console click on upper right cloud-shell page
You can list the active account name with this command:
gcloud auth list
Credentialed accounts: - <myaccount>@<mydomain>.com (active)
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
Resources that live in a zone are referred to as zonal resources. Virtual machine Instances and persistent disks live in a zone. To attach a persistent disk to a virtual machine instance, both resources must be in the same zone. Similarly, if you want to assign a static IP address to an instance, the instance must be in the same region as the static IP.
Create a new instance from the Cloud Console
In this section, you’ll learn how to create new pre-defined machine types with Google Compute Engine from the Cloud Console.
In the GCP Console, on the top left of the screen, select Navigation menu > Compute Engine > VM Instances:
This may take a minute to initialize for the first time.
To create a new instance, click Create.
There are many parameters you can configure when creating a new instance. Use the following for this lab:
|Name||Name for the VM instance|
|Region||Learn more about regions in Regions & Zones documentation.|
|Zone||Learn more about zones in Regions & Zones documentation.|
|Machine Type||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||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.|
|Firewall||Check ||Note: This will automatically create firewall rule to allow HTTP traffic on port 80.|
Wait for it to finish – it shouldn’t take more than a minute.
Once finished, you should see the new virtual machine in the VM Instances page.
To SSH into the virtual machine, click on SSH on the right hand side. This launches a SSH client directly from your browser.
Install a NGINX web server
Now you’ll install NGINX web server, one of the most popular web servers in the world, to connect your virtual machine to something.
Once SSH’ed, get
root access using
sudo su -
root user, update your OS:
Get:1 http://security.debian.org stretch/updates InRelease [94.3 kB] Ign http://deb.debian.org strech InRelease Get:2 http://deb.debian.org strech-updates InRelease [91.0 kB] ...
apt-get install nginx -y
Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: ...
Check that NGINX is running:
ps auwx | grep nginx
root 2330 0.0 0.0 159532 1628 ? Ss 14:06 0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; www-data 2331 0.0 0.0 159864 3204 ? S 14:06 0:00 nginx: worker process www-data 2332 0.0 0.0 159864 3204 ? S 14:06 0:00 nginx: worker process root 2342 0.0 0.0 12780 988 pts/0 S+ 14:07 0:00 grep nginx
Awesome! To see the web page, go to the Cloud Console and click the
External IP link of the virtual machine instance. You can also see the web page by adding the
External IP to
http://EXTERNAL_IP/ in a new browser window or tab.
You should see this default web page:
To check your progress in this lab, click Check my progress below. A checkmark means you’re on track.
Create a Compute Engine instance and add Nginx Server to your instance with necessary firewall rules.Check my progress
Create a new instance with gcloud
Rather than using the GCP Console to create a virtual machine instance, you can use the command line tool
gcloud, which is pre-installed in Google Cloud Shell. Cloud Shell is a Debian-based virtual machine loaded with all the development tools you’ll need (
git, and others) and offers a persistent 5GB home directory.
If you want to try this on your own machine in the future, read the gcloud command line tool guide.
In the Cloud Shell, create a new virtual machine instance from the command line using
gcloud, replacing [YOUR_ZONE] with one of the zone choices given earlier:
gcloud compute instances create gcelab2 --machine-type n1-standard-2 --zone [your_zone]
Created [...gcelab2]. NAME ZONE MACHINE_TYPE ... STATUS gcelab2 us-central1-c n1-standard-2 ... RUNNING
Create a new instance with gcloud.Check my progress
The instance created has these default values:
- The latest Debian 9 (stretch) image.
n1-standard-2machine type. In this lab you can select one of these other machine types if you’d like:
n1-highcpu-4. When you’re working on a project outside of Qwiklabs, you can also specify a custom machine type.
- A root persistent disk with the same name as the instance; the disk is automatically attached to the instance.
gcloud compute instances create --help to see all the defaults.
Note: You can set the default region and zones that
gcloud uses if you are always working within one region/zone and you don’t want to append the
--zone flag every time. Do this by running these commands :
gcloud config set compute/zone ...
gcloud config set compute/region ...
help, press Ctrl+c.
Check out your instances. Select Navigation menu > Compute Engine > VM instances. You should see the 2 instances you created in this lab.
Finally, you can SSH into your instance using
gcloud as well. Make sure you add your zone, or omit the
--zone flag if you’ve set the option globally:
gcloud compute ssh gcelab2 --zone [YOUR_ZONE]
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.
Now you’ll type Y to continue.
Do you want to continue? (Y/n)
Enter through the passphrase section to leave the passphrase empty.
Generating public/private rsa key pair. Enter passphrase (empty for no passphrase)
After connecting, you disconnect from SSH by exiting from the remote shell: