Virtual Machine Creation in Google Cloud Platform

Girish V P
ADIBI Technologies, Basavanagudi, Blore.
5 min readJan 6, 2019

In Google Cloud Platform (GCP), Virtual Machine(VM) Instances comprised of Operating System, CPU, Memory, Disk, Networking etc. VM instances can be created by using the GCP console, or with the command line. Let us see how to configure a Linux VM as a web server and to enable a unique SSH key for the VM with Google Cloud Platform web console.

The Experiment

We are going to configure a VM instance. By default, ingress traffic for SSH is enabled while for webserver (port 80)it is not. Also we wanted a unique SSH key for the VM which GCP doesn't provide by default. We generate a SSH public key and attach to the VM. Let us how we will do it.

Cloud Platform : GCP 
Virtual Machine OS : Centos 6
Role : Webserver, SSH Server
Machine type : f1.micro
Client Machine OS : Linux

Steps involved are given below,

  1. Create Firewall rule to allow port 80

2) Create SSH public/private key pair

3) Create Virtual Machine

4) Configure Web server

5) Apply the Firewall rule to the VM

6) Attach SSH public key to VM

  1. Create Firewall Rule to allow port 80: First create firewall rule to allow port 80 for ingress communication. In the Navigation Menu icon select VPC Network. Select Firewall rules. Click “ CREATE FIREWALL RULE”.

I have assigned the name of the firewall rule as myweb-rule. keep the default parameters below which are self explanatory. For this custom rule you have to choose Specified Target tags option in Targets dropdown list . In the Target Tags column enter a Tag name as a future reference. This is the tag(myweb in this case) you will attach to the VMs you wants this rule to be applicable. Source IP range is 0.0.0.0/0 since wewant everyone from the internet to access the web service running in the VM. In the Protocol and ports section enter TCP and 80 like below. Click Create.

2) Create public/private SSH Key Pair ( from your Linux Client Machine): You have to create SSH public/private key pair by executing the command below. The command creates two files, one for the public key and the other for the private key.

# ssh-keygen -C yourusername@gmail.com

You may have to copy the public key as VM’s property once it is created, while keeping the private key with you.

3) Create Virtual Machine: In the GCP web console click the Navigation Menu icon and select Compute Engine. Click Create like below.

You can select Region, Zone, Machine type, Boot disk (OS) etc. like below screen shots or your own. To make simple I have used basic configuration with a f1.micro VM . Click Create.

4) Configure Web server: Wait till the VM is created. Select the VM and in the SSH dropdown list onthe right side, click Open in browser window . Once you are launched into the OS console, execute the following commands for installing and configuring the web server. ( I have used Centos 6). Execute the sudo command below so that you can access root user login. We configure a basic web server which has the content “my test webserver”.

$ sudo su — 
# setenforce 0 (When you reboot, SElinux blocks your web server again. If you want to disable SElinux disable permanently edit /etc/sysconfig/selinux file and reboot).
# yum install httpd -y
# echo “my test webserver” > /var/www/html/index.html
# service httpd start
# chkconfig httpd on

Now type the public IP of the above VM in your web browser .You are not able to access since port 80 is not opened for you.

5) Apply Firewall Rules to VM: From the Compute Engine interface of GCP web console display the VM details by clicking the VM name. Click EDIT and under Network tags enter the name of the tag(myweb) you created in earlier step. Don’t click Save at this time.

6) Attach SSH public key to VM: Scroll down the above window. Check “Block project-wide SSH keys” . Below click show and edit, copy the SSH public key from client machine, you created earlier. and click Save. Your webserver Configuration is completed.

Testing the Configuration

Now try to access the web service by copying the public IP of the VM into the web browser. You are able to access the webserver(VM) successfully. You execute the command below with appropriate changes in the client Linux system. This also works.

# ssh -i your_ssh_privatekey_filename your_username@public_ip_of_vm

Disclaimer: This experiment is accomplished in test environment. You are requested to verify your setup thoroughly before you implement in a production environment. Some of the screenshots may not reflect exactly as the original GCP web console due to mangled copying and joining of multiple screenshots to form a single one. You may have to refer the original GCP console.

Other Cloud Related Blogs:

--

--