Intermediate Tasks

Task 1: Create a Custom Image and Use It for VM Creation

Create a custom image from a configured VM and launch new instances from it.

Step 1: Create a VM for Image

  1. Go to the Google Cloud Console

  2. In Nevigation Menu Click Compute Engine → VM Instance

  3. Find the Instance you want to create in image from.

  4. Go the () Dots in right side of Instance

  5. Then Click Stop.

This vm is already has apache web server running on it

Step 2: Create a Custom Image

  1. Go to Compute EngineImages.

  2. Select a Create image.

  3. Fill the all Info Name, Source, Source Disk and Encryption.

  4. Then click Create.

You can find the new image listed in the Custom Images section of Compute Engine.

Step 3: Create a New Instances from the Custom Image

  1. Create a New VM Instance

  2. Use the Custom Image

  3. In a Boot disk section, click Change.

  4. And go to Custom Image, and select the image you created.

  5. Then click Select.

Step 4: Configure the Instance

  1. Configure VM Instance with additional options like region, machine type, networking, etc.

  2. Then click a Create to launch the instance from custom image.

    In new Instance apache server is also running in instance that you created.

Task 2 : Setting Up Firewall Rules

How to Configure firewall rules to control traffic to/from VM instances.

Step 1: Navigate to VPC Network:

  1. In left-side menu, click on VPC Network.

  2. select Create Firewall rules from submenu.

Step 2: Configure the Rule Basics:

  • Enter Name a descriptive name for the firewall rule.

  • Select Network where the rule will apply (e.g., you not Created any VPC network then use default network.

  • Assign priority value. Lower numbers indicate higher priority.

  • In Direction of Traffic select Ingress (incoming traffic) or Egress (outgoing traffic).

  • In Action on Match select Allow or Deny, depending on whether you want to permit or block the traffic.

Step 3: Specify the VMs Target :

  • In Targets section, select which VM instances rule will apply to:

  • In Source filter you can select IP ranges (e.g., 0.0.0.0/0 for all IPs, or a specific IP range).

  • For Egress rules, specify the Destination IP ranges to control where traffic can sent to.

Step 4: Protocols Ports and Enforcement:

  • In Protocols and Ports section, you can select Allow all checkbox if you want to allow all protocols and ports (e.g., TCP:80 for HTTP, TCP:443 for HTTPS).

  • if your rule is enforced on associated targets the you can Choose Enabled or Disabled.

Step 5: Create Firewall Rule:

  • Review the firewall rule settings.

  • Then click Create to apply the firewall rule.

Task 3 : Automate VM Management with Google Cloud SDK

Use gcloud commands to create, manage, and delete VMs programmatically.

Step 1: Access the Google Cloud Shell:

  • Click Cloud Shell in the top right of the console.

  • Open the terminal window at the bottom of your browser.

Step 2: Create a VM Instance using shell:

Once you open terminal, you can immediately start running commands to manage your Google Cloud resources.

gcloud auth list
  • You can create a VM instance using the gcloud compute instances create command.
gcloud compute instances create sdk-vm-1 \
    --zone=asia-south1-a \
    --machine-type=e2-medium \
    --image-family=debian-11 \
    --image-project=debian-cloud \
    --boot-disk-size=20GB \
    --boot-disk-type=pd-standard

Step 3: List All VM Instances

You can list all VM instances in your project using this command.

gcloud compute instances list

Step 4: Stop the VM Instance

stop the running VM instance, using this command.

Step 5: Start the VM Instance

Start the VM instance that is stopped, use this command.

gcloud compute instances start sdk-vm-1 --zone=asia-south1-a

Step 6: Delete the VM Instance

Delete the VM instance, using this command. this VM instance was permanently delete.

gcloud compute instances delete sdk-vm-1 --zone=asia-south1-a

Task 4: Configure Load Balancer for High Availability

Set up a load balancer to distribute traffic across multiple VM instances.

Step 1: Set the VM Instances:

  1. Ensure you have multiple VM instances having web servers running in GCP.

  1. Make sure you configured a instance group. This will be backend for your load balancer.

  1. you can see on VM instacne , a VM is automatically created and it is being used by instance-group-1.

Step 2: Go to Load Balancing Page:

  1. Navigate the Load Balancing page.
  • Click navigation menu (≡) in top-left corner.

  • Go to Network Services > Load Balancing.

  1. Click the Create Load Balancer.

  2. Choose a Load Balancer Type.

Select a HTTP(S) Load Balancer to distribute the web traffic across your instances.

  • In a Type of load balancer Select Application LB (HTTP/HTTPS).

  • In a Public facing or internal Select Public facing (external)

  • In a Global or single region deployment Select global workload.

  • In a Load balancer generation Select Global external Application LB.

Step 3: Load Balancer Name Type and Frontend Configuration

  1. Load Balancer Name and Type.
  • Name: Give your LB name, such as web-load-balancer.

  • Then click Continue.

  1. Configure the Frontend

    IP Address: Select an existing external IP address or create a new one.

    Port: Specify port that users will connect to (typically port 80 for HTTP or 443 for HTTPS).

  2. Protocol: Select HTTP or HTTPS.

  3. Finalize Frontend: Once frontend configuration is done, Then click Done.

Step 4: Backend Configuration

  1. Create the Backend Service:
  • Select a Create Backend Service.

  • Name: Enter the name for backend service.

  • Add the Instance groups to containing your VM instances.

  • Specify the Port Number your application runs on (e.g., port 80 for HTTP or 443 for HTTPS).

  • Select or create the Health Check to ensure that only healthy VMs receive traffic.

  • Set a Balancing Mode based on your needs (e.g., Rate, Utilization, or Connection balancing).

  1. Once a backend service is configured, then click Create.

  • scroll down and then Select Instance and Port.

  • Scroll down the again

  • Click save and create.

Step 5: Create and Review the Load Balancer:

  • Including the backend and frontend configurations, Review all settings.

  • Click Create to finalize load balancer setup.

  1. Test a Load Balancer:

  • After load balancer is created, you will get public IP address.

  • Copy IP address in your browser and paste to test if traffic is being properly routed to backend VMs.

Task 5: Schedule a Task on VM (Using Cronjob)

Set up a cron job to automate recurring tasks on your VM.

Step 1: Create a VM instance

  • SSH into your VM and then click Authorize.

Step 2: Open the Crontab File

  • Open the crontab file to editor and use following Command.

      crontab -e
      crontab -l
    

Step 3: Add a Cron Job:

  • At bottom of crontab file, add the desired schedule and command. For e.g., run “my first cronjob” every minute.

  • Save and then Exit:

Task 6: Monitor RAM and CPU Usage of VM

Use tools or monitoring agents to track and log CPU and memory usage.

Step 1: Create a New Instances and SSH into your VM.

  • Use tools like top or htop to view CPU and memory usage interactively.
top
htop

Task 7: Attach a Disk to a VM During/After Creation

Attach a new persistent disk to a VM, either at creation or after it is running.

Step 1: Create a New Instance

  1. Click Create New Instance at the top of the page.

  2. Then Go to Advanced options and Add New Disk.

  3. Enter a name and Save.

  4. Click Create.

  1. Instance is Created Click a Name.

Step 2: Attach a Existing Disk in Running Instance

  1. Click a Attach Existing Disk then Add new persistent.

  2. Click Save.

Task 8: Take a Backup of the VM (Snapshots)

Create and manage VM snapshots for backup and disaster recovery.

Step 1: Create a New Instance

  1. Click Create New Instance at the top of the page.

Step 2: Create a Snapshot using VM Disk

  1. Navigate to Google Cloud Console.

  2. Click on Navigation Menu (☰) and select Compute Engine > Snapshot.

  3. Click the Create a Snapshot