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-a
, us-central1-b
, us-central1-c
, and us-central1-f
.
https://cdn.qwiklabs.com/BErmNT8ZIzd5yqxO0lEJj8lAlKT3jKC%2BtI%2Byj3OSKDA%3D
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.
In this section, you'll learn how to create new pre-defined machine types with Compute Engine from the Cloud Console.
This may take a minute to initialize for the first time.
Click Create.
It should take about a minute for the machine to be created. After that, the new virtual machine is listed on the VM Instances page.
To use SSH to connect to the virtual machine, in the row for your machine, click SSH.
This launches an SSH client directly from your browser.
Now you'll install an NGINX web server, one of the most popular web servers in the world, to connect your virtual machine to something.
In the SSH terminal, to get root
access, run the following command:
sudo su -
As the root
user, update your OS:
apt-get update
Expected output (Do not copy):
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]
...content_copy
Install NGINX:
apt-get install nginx -y
Expected output (Do not copy):
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
...content_copy
Confirm that NGINX is running:
ps auwx | grep nginx
Expected output (Do not copy):
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 nginxcontent_copy
To see the web page, return to the Cloud Console and click the External IP link in the row for your machine, or add the External IP value to http://EXTERNAL_IP/
in a new browser window or tab.
https://cdn.qwiklabs.com/wlVj1uhGB30qhpJOPf3ovbIMPPA%2By4OnvKeCqRbj0U0%3D
This default web page should open:
https://cdn.qwiklabs.com/%2BOL6V6asztPiJZBeMO0xqc%2FiAX8iJfSTr4wDkJmKQu4%3D
Instead of using the Cloud 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 (gcloud
, git,
and others) and offers a persistent 5-GB home directory.
Note: If you want to try this on your own machine, read the gcloud command line tool guide.
In the Cloud Shell, use gcloud
to create a new virtual machine instance from the command line:
gcloud compute instances create gcelab2 --machine-type n1-standard-2 --zone us-central1-c
Expected output (Do not copy):
Created [...gcelab2].
NAME ZONE MACHINE_TYPE ... STATUS
gcelab2 us-central1-c n1-standard-2 ... RUNNING
The new instance has these default values:
n1-standard-2
machine type. In this lab, you can select one of these other machine types: n1-highmem-4
or n1-highcpu-4
. When you're working on a project outside Qwiklabs, you can also specify a custom machine type.To see all the defaults, run:
gcloud compute instances create --help
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. To do this, run these commands:
gcloud config set compute/zone ...
gcloud config set compute/region ...
To exit help
, press CTRL + C.
In the Cloud Console, on the Navigation menu, click Compute Engine > VM instances. Your 2 new instances should be listed.
https://cdn.qwiklabs.com/162OtnL0LAR6EnPSMqNojHEw9nPfAh9SU4ZPRzwK%2B%2Fw%3D
You can also use SSH to connect to your instance via gcloud
. Make sure to add your zone, or omit the -zone
flag if you've set the option globally:
gcloud compute ssh gcelab2 --zone us-central1-c
Expected output (Do not copy):
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.content_copy
Type Y to continue.
Do you want to continue? (Y/n)
Press ENTER through the passphrase section to leave the passphrase empty.
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase)content_copy
After connecting, disconnect from SSH by exiting from the remote shell:
exit