Creating a Virtual Machine in Google Cloud Platform

In this article I will be showing you how to create a basic virtual machine in Google Cloud platform. We will start by basic overview of Google Cloud Platform after that I will show you how to create virtual machine instance of various machine types using Google cloud Platform. Let’s get started

Google Cloud Platform overview

Google Cloud platform is large set of physical assets such as computers, hard disk, processors and virtual assets such as Virtual Machines, services that are contained in Google Cloud data centers. Each data center is present in a global region and each region is a collection of zones. Each zone is isolated from each other within the same region. 
This distribution provides us many facilities like data redundancy in case of zone failure, reduced latency by locating resources closer to client and also provided more control over resources and how they can be used to improve the service and reduce the cost.

As this is a cloud platform you don’t have to worry about physically buying the hardware, buying data centers and installing them. You can use all the hardware/software provided by google as services. In GCP these services provides you access to the underlying hardware. When you will start developing you will notice that you product is just a mix match of services provided by google and your code.

Prerequisites

Google account with billing enabled(If you are doing it for the first time just create a billing account using any credit/debit card with international transaction enables and you will get free credits from Google)

Let’s Start

First step will be to login to your Google cloud console and create a new project with any name you want. Now you might be thinking what is the role of project here I just want to create a virtual machine right?. So, think of project as an organizing entity. Any cloud platform resource that you allocate and use must belong to a project. After creating a project you will be able to see project info on your dashboard. This project info contains the project name which will help you to identify the project, Project ID which will help google cloud platform to identify your project and this will be unique for every project, Project Number which will be used by the support team incase you need any help with your project.

You have three ways to create a virtual machine

  1. Create using Google Cloud Console.
  2. Create using “gcloud” command line tool.
  3. Create using Compute Engine REST API

In this article I will show you it using Google Cloud console and gcloud

Creating Virtual Machine Using Google Cloud Console

Click menu icon on the top left of the screen

Then navigate to Compute engine and VM instances. It may take some time for the first time when you click on VM instances but once its 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

As you can see here you can specify the name of the instance that you want to create. Specify the zone in which you want to create this instance. You can select the machine type that you want to use for your instance. There are large number of machines available from which you can select depending upon your requirement. You can even customize and create your own machine type.

You can also choose the boot disk that you want to use to boot this virtual machine from.

You can select the service account which will be used by this instance. Don’t worry about service account right now just think of it as a way for Google Cloud Platform to identify and authorize your virtual machine in case if it wants to communicate with other services of GCP.

Now, to allow incoming and outgoing traffic we have firewall rules and those are applied to Virtual Machines using tags.

There are customizations available related to management, disks, SSH etc. We will look into them in future. For now just click on create.

This creation process is asynchronous but once the instance is created you will see a green checkmark with the name of the instance.

Congratulations you have successfully created a virtual machine. Hmm..
you might be thinking its very boring as you haven’t use what you have created. So, I will give you a little trailer of what you can do from here.

Click on the SSH link that you are seeing there. You will be able to see the console of your virtual instance and the cool thing is you have the root access for it without doing anything. So, without wasting any time just login as root user by running

sudo su -

this will give login you as root and you will see something like this

where “instance-1” is the name of your instance. Now the first and most important thing update your OS by running

apt-get update

This will update your instance with all the security patches and many other things and finally install the nginx server

apt-get install nginx -y

once installation is done check if nginx is running using

ps auwx | grep nginx

Awesome! Now go to your console and click on the external IP. You should see the default page

So, who thought having your own server will be that easy right?.

You can do all these things with “gcloud” also. Generally “gcloud” will be used in case when you want to create instances using scripts. But for now let’s see how can we 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 it in our systems or we can use Google command shell which have many packages preinstalled and provides a memory of about 5GB. For our purpose we will be using Google command shell. You can open cloud shell by clicking on the icon on the top right of the screen.

Once opened you can create a virtual machine instance using command

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

Virtual Machine is a zonal resource so we must specify the zone in which we want to create our instance and also we cannot have instances with same name in same zone under same project. You can also explore more options under compute using

gcloud compute instances create — help

It will ask you create passphrase for your vm instance. Just click enter if you want to create an empty passphrase.

In future we will be exploring more options to customize our virtual machines and how to modify the firewall rules, improve security for vm instances, grouping them etc. We will also explore the “gcloud” command line tool.