Google Cloud Series, Part 1: How To Create A Virtual Machine
December 9, 2018
What is Google Cloud Platform?
Google Cloud Platform (GCP) is a large set of physical assets. This includes computers, hard disks, processors, and virtual assets (such as Virtual Machines) in Google Cloud data centers. Each data center is present in a global region, and each of these regions is a collection of zones. Each zone occupies its own separate space within the same area. This organization structure provides facilities like data redundancy in case of zone failure, reduced latency by locating resources closer to the client, and more control over resources used to improve the service and reduce the cost.
With GCP, physically buying the hardware is a non-issue. Instead, you can use all the hardware/software Google provides as services. These services allow access to the underlying hardware. So when you begin development, you’ll notice your product is just a combination of services from Google–plus your code.
To Get Started…
You’ll need a Google account with billing enabled. If you are using GCP for the first time, create a billing account using any credit/debit card with international transaction enabled, and you will get free credits from Google.
The first step will be to log in to your Google Cloud console and create a new project (name it whatever you like). You might be curious about the role of this project in creating a virtual machine. But any cloud platform resource that you allocate and use must belong to a project.
After creating the project, you’ll see it on your dashboard. This project info contains the project name, ID (which will help GCP identify your project), and number, which the support team will use if you require assistance.
There are three ways to create a virtual machine:
First, create using Google Cloud Console.
Create using the “gcloud” command-line tool.
Create using Compute Engine REST API.
We’re going to focus on Google Cloud Console and Google Cloud Command Line Tool.
Creating Virtual Machine Using Google Cloud Console
Click the menu icon on the top left of the screen:
Navigate to Compute engine and VM instances. It may take some time the first time you click on VM instances, but once it's initialized, you will see something like this: (keep in mind you need to enable billing for your project to see this).
Click on “Create”, then you will see this page:
You can specify the name of the instance that you want to create. Next, specify the zone in which you want to create this instance. Next, you can select the machine type that you want to use for your example. There are a large number of machines available from which you can choose, depending upon your requirement. You can even customize and create your machine type.
You can also choose the boot disk from which you want to use to boot this virtual machine.
You can select the service account which this instance will use. The service account isn’t important right now–think of it as a way GCP identifies and authorizes your virtual machine to communicate with other services of GCP.
To allow incoming and outgoing traffic, there are firewall rules. These are applied to Virtual Machines using tags.
There are customizations available related to management, disks, SSH, etc., but, for now, click “Create”.
This creation process is asynchronous, but you will see a green checkmark with the instance’s name once the instance is created.
Congratulations! You have successfully created a virtual machine. So what’s next?
Click on the SSH link. You will be able to see the console of your virtual instance. Now you have the root access for it without doing anything else. Log in as root user by running:
sudo su -
This will log you in as root and you will see something like this:
Where “instance-1” is the name of your instance. Now, you need to update your OS by running:
This will update your instance with all the security patches, among other things, and finally, install the nginx server.
apt-get install nginx -y
Once installation is done, check if nginx is running with:
ps auwx | grep nginx
Now go to your console and click on the external IP. You should see the default page:
Having your own server can be just that easy.
You can do all these things with “gcloud” also. Generally, “gcloud” will be used when you want to create instances using scripts. But for now, let’s see how we can create a simple instance with all the default values.
Creating Virtual Machine Using gcloud
Gcloud is a command-line tool available from Google we can install in our systems or use Google command shell, which offers many pre-installed packages with a memory of about 5GB. For this purpose, we will be using the Google command shell. You can open the cloud shell by clicking on the icon on the top right of the screen.
Once opened, you can create a virtual machine instance using the command:
Virtual Machine is a zonal resource, so we must specify the zone to create our instance. You cannot have instances with the same name under the same project in the same zone. You can explore more options under compute using:
gcloud compute instances create — help
When prompted, create a passphrase for your vm instance. Click enter if you want to create an empty passphrase.