Virtual Machine Fundamental Tasks (Beginner Level) in GCP
Task 1: Create a Simple VM (Compute Engine Instance)
Launch a basic VM using default settings
Step 1: Sign in to Google Cloud Console
Go to Google Cloud Console.
Sign in with your Google account or create one if you don't have it yet.
Ensure your project is selected in the "Project" dropdown at the top of the page.
Step 2: Enable the Compute Engine API
In the left-hand navigation panel, click on APIs & Services → Library.
Search for Compute Engine API.
Click Enable to activate the Compute Engine API for your project.
Step 3: Open the Compute Engine Section
In the left-hand navigation panel, go to Compute Engine → VM instances.
If it’s your first time using Compute Engine, click Enable to turn on the service.
Step 4: Create a New Virtual Machine (VM)
Click Create Instance at the top of the VM instances page.
Fill out the instance details:
Name: Choose a name for your instance (e.g., my-vm).
Region and Zone: Select a region and zone close to your users. (e.g., us-central1, us-central1-a).
Machine Family: Use the default General-purpose.
Machine type: Choose a machine type (e.g., e2-micro or n1-standard-1 for basic needs).
Step 5: Choose Boot Disk
Under Boot Disk, click Change.
Select your preferred Operating System (e.g., Ubuntu, Debian, CentOS, etc.).
Choose the disk type and size (the default size is usually 10GB).
Click Select.
Step 6: Configure Networking (Optional)
Choose the default settings under the Network Interfaces section unless you have specific network requirements.
If you want to allow HTTP/HTTPS traffic, check Allow HTTP traffic and HTTPS traffic.
Step 7: Additional Configurations (Optional)
- You can configure additional options like SSH keys, Service Accounts, etc., under the Management, Security, Disks, Networking, and Sole Tenancy sections.
Step 8: Create the VM Instance
Click Create to launch your VM instance.
Wait a few seconds for the instance to be created.
Step 9: Connect to Your VM
- Once the instance is created, you will see it in the list of VM instances.
Click on SSH next to the instance name to open an SSH terminal and access your virtual machine.
Task 2: Create a VM with Attached Disk
Create a VM with an additional persistent disk for storage.
Step 1: Create a New Persistent Disk
In the Navigation Menu, go to Compute Engine → Disks.
Click on Create Disk to make a new persistent disk.
Configure the Disk:
Name: Choose a name for the disk (e.g.,
my-attached-disk
).Region and Zone: Select the same region and zone where your VM will be created.
Disk Type: Choose either a Standard Persistent Disk or an SSD Persistent Disk (SSD offers faster performance).
Size: Specify the disk size in GB (e.g., 20 GB).
Encryption (Optional): You can choose default Google-managed encryption or bring your encryption keys.
Click Create to provision the disk.
Step 2: Create a Virtual Machine (VM) with the Attached Disk
In the Navigation Menu, go to Compute Engine → VM Instances.
Click Create Instance to create a new virtual machine.
Configure the VM:
Name: Provide a name for your VM (e.g.,
my-vm-with-disk
).Region and Zone: Make sure to select the same Region and Zone as the disk you just created.
Machine Configuration:
- Choose a Machine Type that suits your requirements (e.g.,
e2-micro
orn1-standard-1
for general use).
- Choose a Machine Type that suits your requirements (e.g.,
Boot Disk:
In the Boot Disk section, select an operating system image (e.g., Ubuntu, Debian, etc.) or leave the default boot disk settings.
Step 3: Attach the Existing Disk to the VM
Scroll down to the Additional Disks section and click Add New Disk.
Attach Existing Disk:
In the Disk Source dropdown, choose Select Existing Disk.
Choose the disk you created earlier (e.g.,
my-attached-disk
).
Disk Mode: Leave the default mode as Read/Write unless you want the disk to be read-only.
Click Done to confirm the disk attachment
Step 5: Create the VM
- Once everything is configured, click Create to launch the virtual machine with the attached disk.
Wait for the VM to initialize. Once it’s running, you will see it listed under Compute Engine → VM Instances.
Task 3: Create a VM using a Predefine image
Step 1: Upload/Prepare a Custom Image (if not already created)
Before launching a VM from a custom image, ensure that you have a custom image available in Compute Engine → Images:
Create an image from a disk snapshot, another VM, or an image file (such as a
.tar
or.img
file).Alternatively, upload an image file to a Cloud Storage bucket and create an image from that file.
-
Step 2: Open Compute Engine and Start the VM Creation Process
In the Navigation Menu, go to Compute Engine → VM Instances.
Click Create Instance.
Step 3: Configure Basic VM Settings
Name: Enter a name for the VM (e.g.,
my-custom-vm
).Region and Zone: Select a Region and Zone for the VM to run in.
Step 4: Select a Custom Image (Boot Disk)
In the Boot Disk section, click Change.
In the Operating System dropdown, click Custom Images instead of selecting from predefined images.
Choose the Custom Image that you previously created or uploaded.
Confirm the Disk Type (Standard or SSD) and Size for your boot disk.
Step 5: Configure the Machine Type and Firewall
Select the Machine Type based on your resource requirements (e.g., CPU, RAM).
Enable Allow HTTP traffic and Allow HTTPS traffic if needed.
Step 6: Advanced Settings and VM Creation
Expand the Management, Security, Disks, Networking, and Sole Tenancy sections to add SSH keys, set up service accounts, and configure other advanced settings.
After configuring everything, click Create to launch the VM using the custom image.
Step 7: Connect to the VM
Once the VM is created, it will appear in the VM Instances list.
Click SSH next to the instance to connect via an in-browser terminal.
Task 4: Create a VM from a snapshot
Step 1: Sign in to Google Cloud Console
Sign in with your Google account.
Ensure your project is selected, or create a new project using the project selector at the top of the page.
Step 2: Open Compute Engine
In the Navigation Menu on the left, navigate to Compute Engine → VM Instances.
If it’s your first time using Compute Engine, you may need to click Enable to activate the Compute Engine service.
Step 3: Prepare the Snapshot
Ensure that you have a snapshot of the disk you want to use.
To create a snapshot, go to Compute Engine → Snapshots → Create Snapshot.
Choose the disk to snapshot, provide a name, and create the snapshot.
If you already have the snapshot, proceed to the next step.
Step 4: Create a New VM Instance
In Compute Engine → VM Instances, click Create Instance.
Name: Enter a name for your new VM (e.g.,
my-vm-from-snapshot
).Region and Zone: Select a Region and Zone where you want your VM to be created. Make sure the snapshot is available in the same zone or is a multi-region snapshot.
Step 5: Configure the Boot Disk from Snapshot
In the Boot Disk section, click Change to configure the boot disk.
In the Boot Disk popup window, click on the Snapshots tab.
Select a Snapshot:
From the list of available snapshots, choose the snapshot you previously created.
If you have multiple snapshots, ensure you select the correct one based on the name and timestamp.
Configure Disk Type and Size:
- You can customize the disk size (increase if necessary) and choose the disk type (either Standard Persistent Disk or SSD Persistent Disk).
Click Select to finalize the boot disk from the snapshot.
Step 6: Configure Machine Type and Networking
Machine Type: Select the machine type based on your VM’s CPU and memory requirements. For example:
e2-micro
for small workloads.n1-standard-1
for general purposes.
Firewall: Check Allow HTTP traffic and Allow HTTPS traffic if your VM will host web applications.
Step 7: (Optional) Configure Additional Settings
Expand the Management, Security, Disks, Networking, Sole Tenancy sections to configure additional settings like:
SSH keys for secure access.
Service accounts if your VM needs to access other GCP services.
Metadata for start-up scripts or other custom settings.
Step 8: Create the VM
Once configuring the VM and boot disk from the snapshot, click Create at the bottom of the page.
Google Cloud will now create and initialize the virtual machine using the disk snapshot.
Step 9: Verify the VM and Access it
Once the VM is up and running, it will appear in the VM Instances list.
Click SSH next to the VM instance name to open a terminal and access the VM.
Task 5: Create a VM with a startup script
Step 1: Log in to Google Cloud Console
Go to the Google Cloud Console.
Log in with your Google account if prompted.
Step 2: Select or Create a Project
In the top navigation bar, click on the project dropdown.
Select an existing project or click New Project to create a new one.
Step 3: Navigate to the Compute Engine
In the left-hand menu, hover over Compute Engine and click on VM instances.
If this is your first time using Compute Engine, you may need to enable the API.
Step 4: Create a New VM Instance
- Click on the Create Instance button.
Step 5: Configure Your VM Instance
Name: Enter a name for your VM.
Region and Zone: Choose the region and zone where you want to deploy the VM.
Machine Configuration:
- Select the machine type based on your requirements (e.g.,
e``2-medium
,n1-standard-1
, etc.).
- Select the machine type based on your requirements (e.g.,
Boot Disk:
- Choose an operating system and version for your VM (e.g., Ubuntu, CentOS, etc.).
Step 6: Add a Startup Script
Scroll down to the Management, security, disks, networking, and sole tenancy section.
Click on Management to expand it.
In the Startup script field, you can enter your startup script directly. You can also use the following options:
Inline script: Directly paste your script here.
File from Cloud Storage: Reference a script file stored in a Google Cloud Storage bucket (e.g.,
gs://your-bucket/
your-script.sh
).Step 7: Configure Additional Options (Optional)
Firewall Check the boxes to allow HTTP/HTTPS traffic if needed.
Identity and API access: Configure service account permissions as necessary.
Step 8: Review and Create
Review your configuration settings.
Click on the Create button to deploy your VM with the start-up script.
Step 9: Access Your VM
Once the VM is created, you’ll see it listed in the VM instances page.
Click on the SSH button next to your VM to connect to it through the browser.
You can check if the start-up script ran successfully by viewing logs in the VM or checking /var/log/syslog
.
Task 6: Host a Simple Website on a VM
Create a VM Instance:
- Go to Compute Engine → VM Instances and create a new VM, making sure to allow HTTP/HTTPS traffic.
Connect to the VM via SSH:
- Use the SSH button in the console to connect to your VM.
Install Apache:
- Run
sudo apt update
andsudo apt install apache2 -y
to install the Apache web server.
- Run
Deploy Website Files:
- Navigate to
/var/www/html/
, delete the defaultindex.html
, and create your ownindex.html
file.
- Navigate to
Configure Firewall (if needed):
- Ensure ports 80 (HTTP) and 443 (HTTPS) are open in your firewall rules.
Access Your Website:
- Use the external IP of your VM to view your website in a browser.