How to create a Linux VM Instance with Nginx in GCP with the Cloud SDK

tonyette

Feyisayo Famakinde

Posted on June 3, 2021

How to create a Linux VM Instance with Nginx in GCP with the Cloud SDK

My first interaction with the Google Cloud Platform was through its graphical Console: easy to use and navigate. Gradually I became acquainted with the Shell available within the GCP console but nothing feels like being able to connect to the cloud resources directly from the command-line interface of your local computer. So I decide to document how to create the most basic of the cloud resources available on the GCP platform which is the VM instance. This article shows how to create a Linux VM in the Google Cloud platform with the Cloud SDK which is the gcloud command line tool that helps you connect with Google Cloud products and services. In this tutorial we will be creating an Ubuntu 18.04 Linux machine in one of the GCP zones located in the US central region and then install an Nginx web-server on it. I will be creating this from my local machine which is a WSL Ubuntu distribution.

Prerequisites

  1. A Google cloud platform account- You can create one Here at a free trial of $300 valid for 90-days and make sure you set up a billing account and project in the console by providing your card details- you don't get charged once the free trial is activated.

  2. Basic Linux knowledge.

STEPS

  1. Download the SDK kit.
  2. {Install SDK} (install-SDK).
  3. {Create firewall rule} (create-firewall-rule)
  4. Create VM instance.
  5. Install Nginx.

Download the SDK kit

Google has provided this kit for Linux, MacOS and Windows operating systems and you can find them here download based on the operating system of your local system. Since I use the Ubuntu distro I will be downloading the Debian ubuntu package. Use the code below to add the cloud SDK distribution URI as a package source.



echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list


Enter fullscreen mode Exit fullscreen mode

Import the Google cloud public key so you have access to the package



curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -


Enter fullscreen mode Exit fullscreen mode

Install SDK

Then update your machine and download the SDK package



sudo apt-get update && sudo apt-get install google-cloud-sdk


Enter fullscreen mode Exit fullscreen mode

Run



gcloud init


Enter fullscreen mode Exit fullscreen mode

to initialize gcloud.

You will get prompted to configure gcloud; follow the instructions and go to the link provided to go through the authentication and authorization process;

  1. In the browser, fill in the email you used for your account and project setup then copy the authentication code provided and paste in your terminal.
  2. At account creation and billing setup you probably created a project or the project was automatically setup for you select this project in the terminal to get into the cloud environment.

Before we start our VM creation it is important that we follow some best practices for an effective process. First we need to set our project ID as an environment variable to avoid errors. You need to get your project ID before we continue get the ID by running



gcloud projects list


Enter fullscreen mode Exit fullscreen mode

You should see something similar to this showing your project-id and project-name
image
copy your project ID and let's save it in an env variable



export (YOUR PROJECT_ID)=PROJECT_ID


Enter fullscreen mode Exit fullscreen mode

confirm it was saved by running



echo $PROJECT_ID


Enter fullscreen mode Exit fullscreen mode

This command should show your project id as a response.

Create firewall rule

Because Google cloud has all ingress traffic blocked except new firewall rules are created since we will be creating a VM that will host the Nginx server which runs on port 80 on http it is vital that we create a firewall rule that will allow traffic on port 80 for the default network associated with our instance. You can do this by running. Be sure to replace (firewall-rule-id) with your choice of ID.



gcloud compute firewall rules create (firewall-rule-id) --action=ALLOW --destination=INGRESS --rules=http:80 --target-tags=http


Enter fullscreen mode Exit fullscreen mode

confirm that the firewall rule was created by running



gcloud compute firewall-instances list


Enter fullscreen mode Exit fullscreen mode

You should see the created firewall-rule in the list.

Create VM Instance

Creating an instance in compute engine takes certain flags such as the VM location, the machine type, image, disk size and network.
The location is where the VM will be it is important to consider the target ingress traffic for the VM here will your visitors be coming from somewhere close to any of google's point of service.
As at the time of this article writing there are 76 zones available, you can find where they are here.
*The machine type is the processor size.
*Image refers to your choice of operating system.
*Network is the virtual private network available in your project, every project created in gcp comes with a default network that has 27 subnets.

We will be creating an instance located in the us-central1-f zone with a n1-standard-2 machine type and a debian image within our default network and a default disk size of 10GB.

Create the VM instance with the command and replacing (instance-id) with your choice of instance ID



gcloud compute instances create (instance-id) --machine-type=n1-standard-1 --zone=us-central1-f --image-project=debian-cloud --image=debian-9-stretch-v20190213 --subnet=default --tags=http


Enter fullscreen mode Exit fullscreen mode

Confirm that your instance was created with the command below. Ensure you copy out the external IP created because we will be using it soon.



gcloud compute-instances list


Enter fullscreen mode Exit fullscreen mode

Install Nginx

Now we install the Nginx web server, since we are working from our local terminal it is time to ssh into our VM's terminal. We do this by running



gcloud compute ssh (instance-id)


Enter fullscreen mode Exit fullscreen mode

This works because gcloud propagates an ssh key automatically by updating it to the meta-data of your instance and provisioning it to your local system's too only because you have IAM permissions to do so.

Now we should be inside the terminal of our vm instance,you can confirm this with the terminal name change. If you have confirmed terminal change you can now install Nginx, you do this by running



sudo apt-get update
sudo apt install nginx -y


Enter fullscreen mode Exit fullscreen mode

Confirm that nginx started by visiting the external IP of the instance you copied previously
**HTTP://YOUR-EXTERNAL-IP:80
You should see the Nginx default homepage.
Nginx-default-home-page
That is all, I hope this was simple enough to understand. If you had any problem while following any of the steps above please do let me know.

💖 💪 🙅 🚩
tonyette
Feyisayo Famakinde

Posted on June 3, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related