Creating a Virtual Machine in google cloud and running NGINX web server

Creating a Virtual Machine in google cloud and running NGINX web server

Overview

Compute Engine lets you create virtual machines that run different operating systems, including multiple flavors of Linux (Debian, Ubuntu, Suse, Red Hat, CoreOS) and Windows Server, on Google infrastructure. You can run thousands of virtual CPUs on a system that is designed to be fast and to offer strong consistency of performance.

What I'll do

  • Create a virtual machine with the Cloud Console.

  • Create a virtual machine with the gcloud command line

  • Deploy a web server and connect it to a virtual machine.

Step 1: Activating cloud shell

  • In the Google Cloud console dashboard, on the upper right side, there's a cloud shell editor button, click that button and activate the Cloud shell

  • after activating, open the editor and follow the commands to check that your cloud shell is working properly or not :

  • You can list the active account name with this command :

gcloud auth list

  • you can check the Project ID with this command as well :

gcloud config list project

Note: Resources that live in a zone are referred to as zonal resources. Virtual machine Instances and persistent disks live in a zone. To attach a persistent disk to a virtual machine instance, both resources must be in the same zone. Similarly, if you want to assign a static IP address to an instance, the instance must be in the same region as the static IP.

Task 1. Create a new instance from the Cloud Console

  • In the navigation menu, navigate to compute engine > VM instances .

  • Now to create a new instance , click CREATE INSTANCE.

  • There are many parameters you can configure when creating a new instance.

  • Fill up according to your needs. Here's an example of how it looks like :

image

  • You can check out this example for picking up the values:

  • I have Used the HTTP protocol to allow traffic.

  • Click CREATE - Now wait for a while and your VM will be created.

  • For further working in VM, you can use SSH.

  • To use SSH to connect to the virtual machine, in the row for your machine, click SSH, and a pop up window will appear.

Task 2. Install an NGINX web server

  • After opening the SSH, simply update your VM using normal linux commands i.e sudo apt-get update

  • Now install the NGINX using this command : sudo apt-get install -y nginx.

  • Now to confirm the nginx is running, use this command : ps auwx | grep nginx

    Expected output :

  • After installing the VM, look for the External IP of the VM in the Cloud console under the networking section of the VM.

  • copy it and use this to visit the finally deployed web server : http://http://EXTERNAL_IP/

  • Now you will see your NGINX web server is running.

https://cdn.qwiklabs.com/%2BOL6V6asztPiJZBeMO0xqc%2FiAX8iJfSTr4wDkJmKQu4%3D

Task 3. Create a new instance with gcloud

Instead of using the Cloud Console to create a virtual machine instance, you can use the command line tool gcloud, which is pre-installed in Google Cloud Shell. Cloud Shell is a Debian-based virtual machine loaded with all the development tools you'll need (gcloud, git, and others) and offers a persistent 5-GB home directory.
Follow these simple steps :

1. In the Cloud Shell, use gcloud to create a new virtual machine instance from the command line:

gcloud compute instances create gcelab2 --machine-type e2-medium --zone

You will see an expected output like:

https://user-images.githubusercontent.com/96974600/234121260-44fbbb29-cf60-4df9-97b8-4162254ad774.png

Note: The new instance has these default values:

  • The latest Debian 11 (bullseye) image.

- The e2-medium machine type.

- A root persistent disk with the same name as the instance; the disk is automatically attached to the instance.
When you're working in your project you can specify a custom machine type.

2. (opt) - You can also use SSH to connect to your instance via gcloud. Make sure to add your zone, or omit the --zone flag if you've set the option globally:

gcloud compute ssh gcelab2 --zone.

conclusion:

This is how you can create multiple VMs in google cloud using compute engine and can run multiple webservers/applications over that.