Creating a Linux Virtual Machine Compute Engine in Google Cloud Platform

Diana Moraa
6 min readMar 18, 2022

Google compute engine lets you create virtual machines running different operating systems including multiple flavors of Linux and Windows servers with public images that google cloud provides.

It offers a variety of different virtual machines depending on the workload you need to maintain. There are VMs for high memory workload; VMs for high CPU workload and VMs designed to be used for high I/O operations. The big advantage of using Compute Engine VMs is that whether you can’t find a VM type that suits your needs, you can create one with custom CPUs and memory size. Compute Engine also allows you to create a group of VMs to meet the scaling requirements.

You also have the option of creating custom images that you import from existing systems.

In this demo, I will walk you through how to create a virtual machine in Google Cloud. You can connect to it with SSH from your computer.

To get to the place where you can create a VM, go into the menu on the left and select “Compute Engine then VM instances”. Then click “Create insance”

First, you need to give the VM a name. Google generally refers to VMs as instances, so the default name is “instance-1”. For this demo, we can use the name “demo-Linux-instance". However, you can leave it at that default name if you wish but note that you can’t change the name later, so if you were creating a production instance, you’d probably want to name it something meaningful to the workloads you are running.

Labels allow you to organize your project, categorize your projects if you have multiple of them

Next, you need to set the region and the zone. Google divides its data centers into at least three independent zones so that if one of them goes down, it won’t affect the other zones. So, if you distribute your resources across the zones, your applications will keep running even if one of the zones fails. This region us-central1 has 4 zones.

I’m going to leave it with the default of us-central1, which is in Iowa.

Now we need to select the type of machine to create. You can choose from general-purpose, memory-optimized, and compute-optimized. For example, if you choose memory-optimized, then the VM will have way more memory per processor core than it normally would. We’ll leave it as a general-purpose machine, so it’ll have a balanced amount of compute power and memory.

Next, we select the processor series. If it matters to you whether your VM runs an Intel or an AMD CPU, then you can select the one you want here. Otherwise, you can just leave it as E2, which means it will pick whichever type of CPU is available.

Now we finally get to choose the size of the VM(machine type). Each option has a certain number of virtual CPUs and a certain amount of memory, although you can create a custom mix of CPUs and memory if you want. I will choose the e2-micro because it suits my workload now and it’s cheaper.

Next, we need to configure the boot disk. For the operating systems, we can choose from a variety of Linux distributions or Windows servers. I’ll leave it as ubuntu(Long Term Support). For the boot disk type, I will choose the standard persistent disk type. Then I’ll leave it at 10 gigs.

Because we will be running a web server on this instance, we could select the HTTP and HTTPS checkboxes to open the HTTP and HTTPS ports in the firewall. When you select one of these, Compute Engine adds a network tag to your VM, which associates the firewall rule with the VM. Then, Compute Engine creates the corresponding ingress firewall rule that allows all incoming traffic on tcp:80 (HTTP) or tcp:443 (HTTPS).

Also, notice the cost. You will notice an estimate of your current VM configuration cost. One thing about GCP is that you can get the cost estimate of the machine you are running as you are provisioning depending on the workloads. This is transparent as it helps you know the charges you will incur.

There are lots of other options for management, security, disks, networking, etc.

For example: When an instance boots up or restarts, a startup script can be specified to run to install software and updates and to ensure that services are running within the virtual machine. Startup scripts are used for things such as installing and configuring software, running operating system updates, enabling services, and more…

Adding a startup script: The startup script will install apache and print the messages below:

Startup script:

— — — — — — — — — — — — —

#! /bin/bash

apt update

apt -y install apache2

cat <<EOF > /var/www/html/index.html

<html><body><p>Linux startup script added directly.</p><p>The Apache server works!!.</p></body></html>

— — — — — — — — — — — — — — — — — — — — — — — —

Okay, now we can click Create. One of the great things about GCP is how quickly it spins up new instances.

Another thing I love about GCP is how easy it is to connect to VMs. All you have to do is click on this SSH button here. And we’re in. That’s way easier than on other platforms.

Click SSH in the row of the instance that you want to connect to.

2. USING THE COMMAND-LINE INTERFACE(CLOUD SHELL)TO CREATE A VIRTUAL MACHINE

While the console is powerful and flexible, you can do everything from the command line. For every action in the console, there is a gcloud equivalent. Gcloud is the scriptable CLI and it comes with google cloud SDK

When you open a cloud shell that opens the terminal you can usually create resources more quickly, change the configuration, and grant authorization, often with a single command. For example, to create a virtual machine called instance-1 in the us-central1-a zone, all you need to do is type, “gcloud compute instances create instance-1 — zone=us-central1-a”.

I want to launch a new Linux virtual machine using the gcloud command

gcloud compute instances create instance-1 — zone us-central1-c

You can also specify machine type, image project, image family, and network configurations, as in this command.

gcloud compute instances create “test-linux-instance2” \ — machine-type “n1-standard-1” \ — image-project “debian-cloud” \ — image-family “debian-10” \ — subnet “default”

CONCLUSION

Google Cloud offers many tools and services. One of these services is creating highly customizable virtual machines. If you are not familiar with what a virtual machine is, here is a brief definition :

A virtual machine is a computer file, typically called an image, that behaves like an actual computer. In other words, creating a computer within a computer. It runs in a window, much like any other program, giving the end user the same experience on a virtual machine as they would have on the host operating system itself. The virtual machine is sandboxed from the rest of the system, meaning that the software inside a virtual machine can’t escape or tamper with the computer itself.

Hope this helps!

--

--

Diana Moraa

Passionate and motivated about Cloud Computing technology because it continues to allow us to modernize, consolidate IT infrastructure and automate workloads.