Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Google Cloud: Compute Engine Tutorial

Introduction to Compute Engine

Google Compute Engine (GCE) is part of Google Cloud Platform which provides virtual machines (VMs) that run on Google's infrastructure. With GCE, you can create and run VMs on Google’s infrastructure, which is suitable for workloads of any size.

Creating a Compute Engine Instance

To create a Compute Engine instance, follow these steps:

  1. Go to the Google Cloud Console.
  2. Navigate to the "Compute Engine" section from the left-hand menu.
  3. Click on "Create Instance".
  4. Fill in the necessary details such as the instance name, region, zone, machine type, and boot disk.
  5. Click "Create" to launch your instance.

Example:

Creating a VM instance named my-instance in the us-central1-a zone with the n1-standard-1 machine type.

Connecting to Your Instance

Once your instance is created, you can connect to it using SSH. There are a few ways to do this:

  • Using the Google Cloud Console’s built-in SSH button.
  • Using the gcloud command-line tool.
  • Using a third-party SSH client.

Example:

Using the gcloud command-line tool to connect to your instance:

gcloud compute ssh my-instance --zone=us-central1-a

Managing Your Instance

After creating your instance, you might want to manage it by starting, stopping, or deleting it. Here are some basic commands:

Example:

Start an instance:

gcloud compute instances start my-instance --zone=us-central1-a

Stop an instance:

gcloud compute instances stop my-instance --zone=us-central1-a

Delete an instance:

gcloud compute instances delete my-instance --zone=us-central1-a

Scaling and Load Balancing

Compute Engine allows you to scale your applications by adding more instances and distributing traffic among them using load balancers.

To create a managed instance group:

  1. Navigate to the "Instance groups" section in the Google Cloud Console.
  2. Click on "Create instance group".
  3. Choose "Managed instance group".
  4. Configure the instance template and autoscaling policies.
  5. Click "Create" to launch the managed instance group.

Example:

Creating a managed instance group with autoscaling:

gcloud compute instance-groups managed create my-instance-group --base-instance-name=my-instance --template=my-instance-template --size=1 --zone=us-central1-a

Setting up autoscaling for the instance group:

gcloud compute instance-groups managed set-autoscaling my-instance-group --max-num-replicas=10 --target-cpu-utilization=0.75 --cool-down-period=90 --zone=us-central1-a

Networking and Firewalls

Compute Engine provides robust networking capabilities, including Virtual Private Cloud (VPC) networks, subnets, and firewall rules.

To create a firewall rule:

  1. Navigate to the "VPC network" section in the Google Cloud Console.
  2. Click on "Firewall rules".
  3. Click "Create firewall rule".
  4. Specify the name, network, and targets for the rule.
  5. Define the allowed protocols and ports.
  6. Click "Create" to apply the firewall rule.

Example:

Creating a firewall rule to allow HTTP traffic:

gcloud compute firewall-rules create allow-http --allow=tcp:80 --target-tags=http-server --direction=INGRESS

Storage Options

Compute Engine offers various storage options such as persistent disks, local SSDs, and Cloud Storage buckets.

To attach a new persistent disk to an instance:

  1. Navigate to the "Compute Engine" section in the Google Cloud Console.
  2. Click on "Disks".
  3. Click "Create disk".
  4. Specify the name, type, and size of the disk.
  5. Click "Create" to create the disk.
  6. Attach the disk to your instance by editing the instance and adding the disk under "Additional disks".

Example:

Creating a new persistent disk:

gcloud compute disks create my-disk --size=100GB --zone=us-central1-a

Attaching the disk to an instance:

gcloud compute instances attach-disk my-instance --disk=my-disk --zone=us-central1-a

Security Best Practices

When using Compute Engine, it is important to follow security best practices to protect your instances and data:

  • Use IAM roles and permissions to control access to your resources.
  • Regularly update your instances to apply security patches.
  • Use firewall rules to restrict access to your instances.
  • Enable logging and monitoring to detect and respond to security incidents.
  • Encrypt sensitive data at rest and in transit.

Conclusion

Google Compute Engine provides powerful virtual machines for running your applications in the cloud. By following this tutorial, you should now have a good understanding of how to create, manage, and secure your Compute Engine instances. Experiment with different configurations and explore more advanced features to fully leverage the capabilities of Google Cloud.