Virtual Machine Fundamental Tasks (Beginner Level) in GCP

·

10 min read

Task 1: Create a Simple VM (Compute Engine Instance)

Launch a basic VM using default settings

Step 1: Sign in to Google Cloud Console

  1. Go to Google Cloud Console.

  2. Sign in with your Google account or create one if you don't have it yet.

  3. Ensure your project is selected in the "Project" dropdown at the top of the page.

Step 2: Enable the Compute Engine API

  1. In the left-hand navigation panel, click on APIs & ServicesLibrary.

  2. Search for Compute Engine API.

  3. Click Enable to activate the Compute Engine API for your project.

Step 3: Open the Compute Engine Section

  1. In the left-hand navigation panel, go to Compute EngineVM instances.

  2. If it’s your first time using Compute Engine, click Enable to turn on the service.

Step 4: Create a New Virtual Machine (VM)

  1. Click Create Instance at the top of the VM instances page.

  2. 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

  1. Under Boot Disk, click Change.

  2. Select your preferred Operating System (e.g., Ubuntu, Debian, CentOS, etc.).

  3. Choose the disk type and size (the default size is usually 10GB).

  4. Click Select.

Step 6: Configure Networking (Optional)

  1. Choose the default settings under the Network Interfaces section unless you have specific network requirements.

  2. If you want to allow HTTP/HTTPS traffic, check Allow HTTP traffic and HTTPS traffic.

Step 7: Additional Configurations (Optional)

  1. 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

  1. Click Create to launch your VM instance.

  2. Wait a few seconds for the instance to be created.

Step 9: Connect to Your VM

  1. 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

  1. Go to the Google Cloud Console: console.cloud.google.com.

  2. In the Navigation Menu, go to Compute EngineDisks.

  3. Click on Create Disk to make a new persistent disk.

  4. 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).

  5. Encryption (Optional): You can choose default Google-managed encryption or bring your encryption keys.

  6. Click Create to provision the disk.

Step 2: Create a Virtual Machine (VM) with the Attached Disk

  1. In the Navigation Menu, go to Compute EngineVM Instances.

  2. Click Create Instance to create a new virtual machine.

  3. 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.

  4. Machine Configuration:

    • Choose a Machine Type that suits your requirements (e.g., e2-micro or n1-standard-1 for general use).
  5. 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

      1. Scroll down to the Additional Disks section and click Add New Disk.

      2. Attach Existing Disk:

        • In the Disk Source dropdown, choose Select Existing Disk.

        • Choose the disk you created earlier (e.g., my-attached-disk).

      3. Disk Mode: Leave the default mode as Read/Write unless you want the disk to be read-only.

      4. Click Done to confirm the disk attachment

  6. Step 5: Create the VM

    1. 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 EngineVM Instances.

Task 3: Create a VM using a Predefine image

Step 1: Upload/Prepare a Custom Image (if not already created)

  1. Before launching a VM from a custom image, ensure that you have a custom image available in Compute EngineImages:

    • 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

      1. In the Navigation Menu, go to Compute EngineVM Instances.

      2. Click Create Instance.


Step 3: Configure Basic VM Settings

  1. Name: Enter a name for the VM (e.g., my-custom-vm).

  2. Region and Zone: Select a Region and Zone for the VM to run in.


Step 4: Select a Custom Image (Boot Disk)

  1. In the Boot Disk section, click Change.

  2. In the Operating System dropdown, click Custom Images instead of selecting from predefined images.

  3. Choose the Custom Image that you previously created or uploaded.

  4. Confirm the Disk Type (Standard or SSD) and Size for your boot disk.

Step 5: Configure the Machine Type and Firewall

  1. Select the Machine Type based on your resource requirements (e.g., CPU, RAM).

  2. Enable Allow HTTP traffic and Allow HTTPS traffic if needed.

Step 6: Advanced Settings and VM Creation

  1. Expand the Management, Security, Disks, Networking, and Sole Tenancy sections to add SSH keys, set up service accounts, and configure other advanced settings.

  2. After configuring everything, click Create to launch the VM using the custom image.

Step 7: Connect to the VM

  1. Once the VM is created, it will appear in the VM Instances list.

  2. 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

  1. Go to the Google Cloud Console: console.cloud.google.com.

  2. Sign in with your Google account.

  3. 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

  1. In the Navigation Menu on the left, navigate to Compute EngineVM Instances.

  2. 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

  1. Ensure that you have a snapshot of the disk you want to use.

    • To create a snapshot, go to Compute EngineSnapshotsCreate Snapshot.

    • Choose the disk to snapshot, provide a name, and create the snapshot.

  2. If you already have the snapshot, proceed to the next step.

Step 4: Create a New VM Instance

  1. In Compute EngineVM Instances, click Create Instance.

  2. Name: Enter a name for your new VM (e.g., my-vm-from-snapshot).

  3. 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

  1. In the Boot Disk section, click Change to configure the boot disk.

  2. In the Boot Disk popup window, click on the Snapshots tab.

  3. 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.

  4. 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).
  5. Click Select to finalize the boot disk from the snapshot.


Step 6: Configure Machine Type and Networking

  1. 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.

  2. Firewall: Check Allow HTTP traffic and Allow HTTPS traffic if your VM will host web applications.


Step 7: (Optional) Configure Additional Settings

  1. 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

  1. Once configuring the VM and boot disk from the snapshot, click Create at the bottom of the page.

  2. Google Cloud will now create and initialize the virtual machine using the disk snapshot.

  3. Step 9: Verify the VM and Access it

    1. Once the VM is up and running, it will appear in the VM Instances list.

    2. 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

  1. Go to the Google Cloud Console.

  2. Log in with your Google account if prompted.

Step 2: Select or Create a Project

  1. In the top navigation bar, click on the project dropdown.

  2. Select an existing project or click New Project to create a new one.

Step 3: Navigate to the Compute Engine

  1. In the left-hand menu, hover over Compute Engine and click on VM instances.

  2. If this is your first time using Compute Engine, you may need to enable the API.

Step 4: Create a New VM Instance

  1. Click on the Create Instance button.

Step 5: Configure Your VM Instance

  1. Name: Enter a name for your VM.

  2. Region and Zone: Choose the region and zone where you want to deploy the VM.

  3. Machine Configuration:

    • Select the machine type based on your requirements (e.g., e``2-medium, n1-standard-1, etc.).
  4. Boot Disk:

    • Choose an operating system and version for your VM (e.g., Ubuntu, CentOS, etc.).

Step 6: Add a Startup Script

  1. Scroll down to the Management, security, disks, networking, and sole tenancy section.

  2. Click on Management to expand it.

  3. 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)

      1. Firewall Check the boxes to allow HTTP/HTTPS traffic if needed.

      2. Identity and API access: Configure service account permissions as necessary.

Step 8: Review and Create

  1. Review your configuration settings.

  2. Click on the Create button to deploy your VM with the start-up script.

Step 9: Access Your VM

  1. Once the VM is created, you’ll see it listed in the VM instances page.

  2. 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 EngineVM 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 and sudo apt install apache2 -y to install the Apache web server.
  • Deploy Website Files:

    • Navigate to /var/www/html/, delete the default index.html, and create your own index.html file.
  • 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.