Creating Virtual Machines and NGINX Servers on Google Cloud Platform for Go Web Apps
Introduction
This article teaches the reader the concept of virtual machines that run on the cloud, their uses, and how to set them up on the Google Cloud Platform.
The reader might be a web developer looking to get started with Cloud or DevOps engineering on the Google Cloud Platform.
Prerequisites for this course
This article is a practical one, requiring the reader to follow along with the procedures while working on their own computer. Hence, the following are prerequisites for this article:
- a personal computer
- a free tier or normal account on GCP with Billing enabled
- working knowledge of Go programming language
Virtual Machines
The concept of virtual machines is similar to launching a secondary operating system on your computer. Think of it as creating a virtualized version of a computer. The idea is to recreate or mimic everything you can do with your physical computer relating to specific computing functions.
Example uses of virtual machines include:
- creating remote operating systems on your local computer
- connecting temporarily to a network, platform, or system to perform tasks solely exclusive to that platform
- practicing unsafe concepts like pwning a machine, etc
- development and testing environments
- supporting DevOps practices
- enabling workload migrations, etc
Let’s look at the Go language that we will use for the project in this article.
Go
The Go programming language was created in 2007 by Google to solve problems in the area of multicore processors, networking systems, large codebases, computation clusters, and general web development. It has intense dependency management, supports scalable software architecture, and maintains cross-platform robustness.
Google developed the language to simplify development, eliminate clumsiness during development, control dependency, ease of writing script automation tools, boost code reuse, etc.
It is logical to work on the Google Cloud Platform with Google’s Go programming language 🙂. You can learn more about Go at Google here).
Starting the Cloud Shell
First, navigate to the Google Cloud Platform on your browser. Next, create an account (This article assumes you know how to set up an account comfortably).
Upon login on the cloud platform, you will be greeted with the following or similar window:
After the search icon in the navigation bar, you will see another icon that symbolizes “Code” or “Terminal,” depending on your familiarity. It has a greater than sign and an underscore: [>_
].
Click the icon, and it will spawn a remote terminal just below your current page.
What You Can Do On The Google Cloud Shell
The Google Cloud Shell, or gcloud
CLI
, is a useful tool similar to your Git Bash or standard shell. The difference is that it has its own set of commands and functionalities. You can do the following on gcloud CLI
:
- creating and managing Compute Engine virtual machine instances and other resources
- deploying App Engine applications
- creating and managing Cloud SQL instances
- managing authentication
- creating and managing Google Kubernetes Engine clusters
- creating and managing Dataproc clusters and jobs
- for Cloud DNS managed zones and record sets
- for Cloud Deployment Manager deployments
- customizing local configuration, etc
You can download the gcloud CLI
locally or remotely use it on the Compute Engine shell. If you decide to download the CLI to your local computer — especially if you’re going to be using it for long — you should Install the CLI with these instructions.).
Basic Commands
gcloud CLI
commands can be run on the Cloud Shell or cloud automation platforms. For example, you could run Cloud Compute commands on Kubernetes Engine to automate it. The four categories of commands for gcloud include:
gcloud compute
gcloud compute instances
gcloud beta compute
gcloud alpha app
These categories support commands that are product- or feature-specific for the Google Cloud Platform.
Let’s take a look at some basic commands.
- Initialize the
gcloud CLI
with the following command:
$ gcloud init
- you can display the version of the CLI with the following command:
$ gcloud version
- The
gcloud CLI
supports the installation of specific components. You can install and update components with the following commands:
$ gcloud components install
$ gcloud components update
- List active user account with the following command:
$ gcloud auth list
- Check the current project ID with the following command:
$ gcloud config list project
Like other CLI you may have used before, the gcloud CLI
support options and flags too.
To learn more about the CLI commands on gcloud
, click here).
Starting a new project on Google Cloud Platform with the gcloud CLI
You may be launching the cloud shell only when you want to start a new VM for a project. This way, you can start a VM with the command below:
$ gcloud compute instances create <project_name> --machine-type <type_of_machine> --zone <a_preferred_zone>
For example, let’s create a project named projtest
. The entire command will be:
$ gcloud compute instances create projtest --machine-type n1-standard-2 --zone us-central1-f
This command spins an instance with the following output:
Created [...projtest].
NAME ZONE MACHINE_TYPE ... STATUS
projtest us-central1-f n1-standard-2 ... RUNNING
On Machine Types
When you create a virtual machine instance on the Google Cloud Platform, you are required to choose a machine type that will determine the resources available to your new virtual machine.
This article created an instance that uses the n1-standard-2
machine type. Nevertheless, you can choose an alternative like nq-highmem-4 or n1-highcpu-4 machine types. You can even create your machine type). There are machine series and family categories in the Google Cloud Platform, and you can learn about them here).
Also, you can use the GUI to create a new VM instance. by following these procedures:
- Click the Navigation Menu
- Click on Compute Engine and select VM Instances
- Click Create Instance
- Configure every parameter in your preferred way
It is advisable to setup an instance using ssh)
Next, let’s see what an NGINX web server is and how to install it on the Google Cloud Platform.
Installing an NGINX web server on Google Cloud Platform
The NGINX (pronounced “Engine X”) webserver is among the most popular and widely used web servers in cloud/web development. Nginx runs about 30% of the web servers in the world and is seeing more adoption. Like every other web server software, NGINX runs on top of an operating system. The operating system runs on physical hardware. You can have NGINX installed on your local machine, or on the Google Cloud Platform.
Installing an NGINX webserver
Navigate to the console and run the following command to get root access:
$ sudo su -
Next, update your selected operating system with the following command:
$ apt-get update
Now, install NGINX on your console with this command:
apt-get install nginx -y
You can confirm that NGINX is appropriately set up on your console with the command:
$ ps auwx | grep nginx
The following or similar output will be generated:
root 2320 0.0 0.0 175532 1748 ? Ss 14:06 0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 2321 0.0 0.0 159924 3004 ? S 14:06 0:00 nginx: worker process
www-data 2322 0.0 0.0 154874 3114 ? S 14:06 0:00 nginx: worker process
root 2332 0.0 0.0 19680 758 pts/0 S+ 14:07 0:00 grep nginx
You have successfully installed an NGINX web server on your GCP VM instance.
Next, let’s create a basic Go web app on a Google Cloud Platform Virtual Machine instance.
Creating a Basic Web App with Go on a Virtual Machine
To build a Go web app on top of Google Cloud Platform, we will utilize our just learned knowledge of the gcloud CLI
and others.
First, open the Google Cloud Shell).
Next, create a project in the Project Selector) page on GCP.
After this, you need to enable the GCP Cloud Build API using the following procedure:
- Open the Navigation Menu
- Click on the APIs & Services, select Library, then search for the Cloud Build API.
- Click Enable
To enable billing, you need to have a free tier account if you’re not using GCP professionally.
Afterward, you will install and initialize the gcloud CLI
if you didn’t before. Running the following command will create an app engine that will provide the needed resources to develop and maintain your app.
To start the web app, you need to run the following command:
$ gcloud app create
After this, you can start your web app directly by creating a directory that will contain a yaml file and your Go source file.
The yaml file is what will initialize the Go environment to run your application. Without a yaml file, no application project will run.
Thus, we create an app.yaml
file in the directory and the main.go
source file. Your basic web application code will reside in the source file.
To deploy the web application on the App engine, run the following command:
$ gcloud app deploy
You can view your new web application if it is done right by running the following command:
$ gcloud app browse
Conclusion
In this article, we learned about Virtual Machines and their uses. Next, we looked at the Go programming language and why it was created by Google. We further learned how to start a Cloud Shell session on the Google Compute Engine as a Virtual Machine instance. Furthermore, we looked at NGINX web servers and how to install and spawn them in the Cloud.
In a future article, we will build a web app in Go, putting all we have learned here into practice. We will build a proper Go application, learn how to structure a complete codebase for a project on GCP, and finally deploy the web app on the App Engine.
See you soon!