Creating a Virtual Machine | Overview
Creating a Virtual Machine is possible with compute engine that runs 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 is designed to be fast and to offer strong consistency of performance.
In this hands-on lab, you’ll be creating a virtual machine instance of various machine types using the Google Cloud Console and the gcloud command line. You’ll also learn how to connect an NGINX webserver to your virtual machine.
Although you can easily copy and paste commands from the lab to the appropriate place, we recommend that you type the commands yourself to reinforce your understanding of the core concepts.
What you’ll do
- Creating a virtual machine with the Cloud Console.
- Creating a virtual machine with the
- Deploy a web server and connect it to a virtual machine.
Familiarity with standard Linux text editors such as
nano will be helpful.
Task 1: Create a new instance from the Cloud Console
In this section, you’ll learn how to create new pre-defined machine types with Compute Engine from the Cloud Console.
- In the Cloud Console, on the Navigation menu (), click Compute Engine > VM Instances.This may take a minute to initialize for the first time.
- To create a new instance, click CREATE INSTANCE.
- There are many parameters you can configure when creating a new instance. Use the following for this lab:
|Name||gcelab||Name for the VM instance|
|Region||us-central1 (Iowa)||For more information about regions, see Regions and Zones.|
|Zone||us-central1-f||Note: Remember the zone that you selected: you’ll need it later. For more information about zones, see Regions and Zones.|
|Series||N1||Name of the series|
|Machine Type||2 vCPU||This is an (n1-standard-2), 2-CPU, 7.5GB RAM instance. Several machine types are available, ranging from micro instance types to 32-core/208GB RAM instance types. For more information, see Machine Types. 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 this lab.|
|Boot Disk||New 10 GB balanced persistent disk OS Image: Debian GNU/Linux 10 (buster)||Several images are available, including Debian, Ubuntu, CoreOS, and premium images such as Red Hat Enterprise Linux and Windows Server. For more information, see Operating System documentation.|
|Firewall||Allow HTTP traffic||Select this option in order to access a web server that you’ll install later. Note: This will automatically create a firewall rule to allow HTTP traffic on port 80.|
- 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.
Task 2: Install an NGINX web server
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
rootaccess, run the following command:
sudo su -
2. As the
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] ...
3. Install NGINX:
apt-get install nginx -y
Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: ...
4. Confirm 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
5. 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.
This default web page should open:
Task 3: Create a new instance with gcloud
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 (
git, and others) and offers a persistent 5-GB home directory.
- In the Cloud Shell, use
gcloudto create a new virtual machine instance from the command line:
gcloud compute instances create gcelab2 --machine-type n1-standard-2 --zone us-central1-f
Created [...gcelab2]. NAME ZONE MACHINE_TYPE ... STATUS gcelab2 us-central1-f n1-standard-2 ... RUNNING
The new instance has these default values:
The latest Debian 10 (buster) image.
n1-standard-2 machine type. In this lab, you can select one of these other machine types:
n1-highcpu-4. When you’re working on a project outside 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.
2. 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 ...
3. To exit
help, press CTRL + C.
4. In the Cloud Console, on the Navigation menu, click Compute Engine > VM instances. Your 2 new instances should be listed.
5. 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-f
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.
6. Type Y to continue.
Do you want to continue? (Y/n)
7. Press ENTER through the passphrase section to leave the passphrase empty.
Generating public/private rsa key pair. Enter passphrase (empty for no passphrase)
8. After connecting, disconnect from SSH by exiting from the remote shell:
Congratulations! You’re done with Creating a Virtual Machine
Complete More Qwiklabs:
Get Google Cloud Stickers: Get it Now